summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Lewis2020-03-08 13:35:20 -0500
committerDenys Vlasenko2020-06-09 01:55:59 +0200
commit9b4a9d96b89f06355ad9551d782d34506699aac8 (patch)
tree5e7ad4eb04b8e7c42864f985c5bd8dd525b78e2b
parent726d0d148b5a7a21eccaf8b17d2726f56ce2ce8f (diff)
downloadbusybox-9b4a9d96b89f06355ad9551d782d34506699aac8.zip
busybox-9b4a9d96b89f06355ad9551d782d34506699aac8.tar.gz
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>
-rw-r--r--libbb/xfuncs_printf.c19
1 files changed, 5 insertions, 14 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c
index 93f325c..f1cf7ae 100644
--- a/libbb/xfuncs_printf.c
+++ b/libbb/xfuncs_printf.c
@@ -93,26 +93,17 @@ char* FAST_FUNC xstrdup(const char *s)
// the (possibly truncated to length n) string into it.
char* FAST_FUNC xstrndup(const char *s, int n)
{
- int m;
char *t;
if (ENABLE_DEBUG && s == NULL)
bb_simple_error_msg_and_die("xstrndup bug");
- /* We can just xmalloc(n+1) and strncpy into it, */
- /* but think about xstrndup("abc", 10000) wastage! */
- m = n;
- t = (char*) s;
- while (m) {
- if (!*t) break;
- m--;
- t++;
- }
- n -= m;
- t = xmalloc(n + 1);
- t[n] = '\0';
+ t = strndup(s, n);
- return memcpy(t, s, n);
+ if (t == NULL)
+ bb_die_memory_exhausted();
+
+ return t;
}
void* FAST_FUNC xmemdup(const void *s, int n)