[PATCH] xstrndup: Use strndup instead of implementing it.
Denys Vlasenko
vda.linux at googlemail.com
Mon Jun 8 23:57:29 UTC 2020
Applied, thanks!
On Sun, Mar 8, 2020 at 1:56 PM Martin Lewis <martin.lewis.x84 at gmail.com> wrote:
>
> Used the same function instead of strndup by mistake... Here's the correct version:
>
> Signed-off-by: Martin Lewis <martin.lewis.x84 at gmail.com>
> ---
> libbb/xfuncs_printf.c | 19 +++++--------------
> 1 file 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)
> // 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)
> --
> 2.11.0
>
> _______________________________________________
> busybox mailing list
> busybox at busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
More information about the busybox
mailing list