diff options
author | Martin Lewis <martin.lewis.x84@gmail.com> | 2020-03-08 13:35:20 -0500 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2020-06-09 01:55:59 +0200 |
commit | 9b4a9d96b89f06355ad9551d782d34506699aac8 (patch) | |
tree | 5e7ad4eb04b8e7c42864f985c5bd8dd525b78e2b /libbb/xfuncs_printf.c | |
parent | 726d0d148b5a7a21eccaf8b17d2726f56ce2ce8f (diff) | |
download | busybox-w32-9b4a9d96b89f06355ad9551d782d34506699aac8.tar.gz busybox-w32-9b4a9d96b89f06355ad9551d782d34506699aac8.tar.bz2 busybox-w32-9b4a9d96b89f06355ad9551d782d34506699aac8.zip |
xstrndup: Use strndup instead of implementing it.
Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'libbb/xfuncs_printf.c')
-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) |