diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2006-08-11 17:42:19 +0000 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2006-08-11 17:42:19 +0000 |
commit | 897f4d069bc7de018639d6937a005fd711a82e5c (patch) | |
tree | 50f9dd439e6c3c1d36a569c9c3b62c994798c624 /lib/snprintf.c | |
parent | 01f23950084e087fae83d027dc0661561daf8246 (diff) | |
download | gnulib-897f4d069bc7de018639d6937a005fd711a82e5c.tar.gz |
* snprintf.c (snprintf): memcpy LEN bytes, not SIZE - 1, when
LEN is smaller than SIZE. Suggested by Bruno Haible.
Also, help the compiler to keep LEN in a register.
Diffstat (limited to 'lib/snprintf.c')
-rw-r--r-- | lib/snprintf.c | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/lib/snprintf.c b/lib/snprintf.c index c281f7c583..ef00f55b41 100644 --- a/lib/snprintf.c +++ b/lib/snprintf.c @@ -45,11 +45,12 @@ snprintf (char *str, size_t size, const char *format, ...) { char *output; size_t len; + size_t lenbuf = size; va_list args; va_start (args, format); - len = size; output = vasnprintf (str, &len, format, args); + len = lenbuf; va_end (args); if (!output) @@ -59,8 +60,9 @@ snprintf (char *str, size_t size, const char *format, ...) { if (size) { - memcpy (str, output, size - 1); - str[size - 1] = '\0'; + size_t pruned_len = (len < size ? len : size - 1); + memcpy (str, output, pruned_len); + str[pruned_len] = '\0'; } free (output); |