From: Bruno Haible Date: Sat, 3 Nov 2007 14:22:52 +0000 (+0100) Subject: Fix detection of overflow: don't assume that snprintf is C99 compliant. X-Git-Tag: v0.1~8011 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=6c48d0159096088da3d62b4dce62838bc3f8607c;p=gnulib.git Fix detection of overflow: don't assume that snprintf is C99 compliant. --- diff --git a/ChangeLog b/ChangeLog index 15cd4c705..6c965e410 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2007-11-03 Bruno Haible + * lib/vasnprintf.c (VASNPRINTF): Don't assume that snprintf's return + value is C99 compliant. + Needed for OSF/1 5.1. + +2007-11-03 Bruno Haible + Fix out-of-memory handling of vasnprintf. * lib/printf-parse.c: Include . (PRINTF_PARSE): When failing, set errno to EINVAL or ENOMEM. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index f081e3ade..6b4d6c6a5 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -3658,8 +3658,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } #if USE_SNPRINTF - /* Handle overflow of the allocated buffer. */ - if (count >= maxlen) + /* Handle overflow of the allocated buffer. + If such an overflow occurs, a C99 compliant snprintf() + returns a count >= maxlen. However, a non-compliant + snprintf() function returns only count = maxlen - 1. To + cover both cases, test whether count >= maxlen - 1. */ + if ((unsigned int) count + 1 >= maxlen) { /* If maxlen already has attained its allowed maximum, allocating more memory will not increase maxlen.