vasnprintf: Correct errno value in case of out-of-memory.
authorBruno Haible <bruno@clisp.org>
Sat, 24 Apr 2010 15:18:48 +0000 (17:18 +0200)
committerBruno Haible <bruno@clisp.org>
Sat, 24 Apr 2010 15:18:48 +0000 (17:18 +0200)
ChangeLog
lib/vasnprintf.c

index c88ea6a7755889ed3f8c2ceb9cc4fa304e2c2490..2d4a2316fc0aa7bb17e9804ee044c871e965208a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2010-04-24  Bruno Haible  <bruno@clisp.org>
+
+       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 <ianb@erislabs.net>.
+
 2010-04-24  Bruno Haible  <bruno@clisp.org>
 
        ansi-c++-opt: Find correct compiler when cross-compiling.
index 6c11b72490e75317ad7b580e989aadf38e0f2611..836bf10c5e9f54ae6d553a5164996fd0d1a76d3c 100644 (file)
@@ -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;
                       }