From: Ian Beckwith Date: Sat, 24 Apr 2010 18:00:13 +0000 (+0100) Subject: vasnprintf: Correct errno value in case of out-of-memory. X-Git-Tag: stable/20100424~6 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=528ab01;p=gnulib.git vasnprintf: Correct errno value in case of out-of-memory. (cherry picked from commit e51d12c10630dc3c7a1b0f4c54dd0739ccbcaaa7) --- diff --git a/ChangeLog b/ChangeLog index 64e0e6394..eb140456f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2010-04-24 Bruno Haible + + vasnprintf: Correct errno value in case of out-of-memory. + * lib/vasnprintf.c (VASNPRINTF): Set errno to 0 before calling SNPRINTF + or sprintf. Use the errno value from SNPRINTF or sprintf. + Reported by Ian Beckwith . + 2010-04-20 Andreas Gruenbacher * build-aux/bootstrap: Use "git -h" for testing for supported options diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 6c11b7249..836bf10c5 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -4953,6 +4953,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } #endif + errno = 0; switch (type) { case TYPE_SCHAR: @@ -5147,15 +5148,21 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Attempt to handle failure. */ if (count < 0) { + /* SNPRINTF or sprintf failed. Save and use the errno + that it has set, if any. */ + int saved_errno = errno; + if (!(result == resultbuf || result == NULL)) free (result); if (buf_malloced != NULL) free (buf_malloced); CLEANUP (); errno = - (dp->conversion == 'c' || dp->conversion == 's' - ? EILSEQ - : EINVAL); + (saved_errno != 0 + ? saved_errno + : (dp->conversion == 'c' || dp->conversion == 's' + ? EILSEQ + : EINVAL)); return NULL; }