diff options
-rw-r--r-- | libbb/xfuncs_printf.c | 19 |
1 files changed, 5 insertions, 14 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 93f325c62..f1cf7aeed 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c | |||
@@ -93,26 +93,17 @@ char* FAST_FUNC xstrdup(const char *s) | |||
93 | // the (possibly truncated to length n) string into it. | 93 | // the (possibly truncated to length n) string into it. |
94 | char* FAST_FUNC xstrndup(const char *s, int n) | 94 | char* FAST_FUNC xstrndup(const char *s, int n) |
95 | { | 95 | { |
96 | int m; | ||
97 | char *t; | 96 | char *t; |
98 | 97 | ||
99 | if (ENABLE_DEBUG && s == NULL) | 98 | if (ENABLE_DEBUG && s == NULL) |
100 | bb_simple_error_msg_and_die("xstrndup bug"); | 99 | bb_simple_error_msg_and_die("xstrndup bug"); |
101 | 100 | ||
102 | /* We can just xmalloc(n+1) and strncpy into it, */ | 101 | t = strndup(s, n); |
103 | /* but think about xstrndup("abc", 10000) wastage! */ | ||
104 | m = n; | ||
105 | t = (char*) s; | ||
106 | while (m) { | ||
107 | if (!*t) break; | ||
108 | m--; | ||
109 | t++; | ||
110 | } | ||
111 | n -= m; | ||
112 | t = xmalloc(n + 1); | ||
113 | t[n] = '\0'; | ||
114 | 102 | ||
115 | return memcpy(t, s, n); | 103 | if (t == NULL) |
104 | bb_die_memory_exhausted(); | ||
105 | |||
106 | return t; | ||
116 | } | 107 | } |
117 | 108 | ||
118 | void* FAST_FUNC xmemdup(const void *s, int n) | 109 | void* FAST_FUNC xmemdup(const void *s, int n) |