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-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6c48d0159096088da3d62b4dce62838bc3f8607c;p=pspp Fix detection of overflow: don't assume that snprintf is C99 compliant. --- diff --git a/ChangeLog b/ChangeLog index 15cd4c7059..6c965e4108 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +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. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index f081e3adef..6b4d6c6a51 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.