From 78bc0d8366023bf0706af3d49d00495e7968879f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Fri, 11 Aug 2006 17:41:11 +0000 Subject: (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. --- lib/ChangeLog | 6 ++++++ lib/snprintf.c | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'lib') diff --git a/lib/ChangeLog b/lib/ChangeLog index 7f0ed7435..802bde813 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2006-08-11 Paul Eggert + + * 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. + 2006-08-10 Paul Eggert Import the following changes from libc: diff --git a/lib/snprintf.c b/lib/snprintf.c index c281f7c58..ef00f55b4 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); -- cgit v1.2.3-54-g00ecf