vasnprintf: Fix memory leak (regression from yesterday).
authorBruno Haible <bruno@clisp.org>
Sat, 27 Mar 2021 00:12:07 +0000 (01:12 +0100)
committerBruno Haible <bruno@clisp.org>
Sat, 27 Mar 2021 00:12:07 +0000 (01:12 +0100)
* lib/vasnprintf.c (VASNPRINTF): Restore omitted CLEANUP invocation.
Some more simplifications of errno around free().

ChangeLog
lib/vasnprintf.c

index cac41b7957cef5e6df1f46275918bf28b41a89d4..670bfd10c70656e2a80d1f53a4dc321228a63f17 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2021-03-26  Bruno Haible  <bruno@clisp.org>
+
+       vasnprintf: Fix memory leak (regression from yesterday).
+       * lib/vasnprintf.c (VASNPRINTF): Restore omitted CLEANUP invocation.
+       Some more simplifications of errno around free().
+
 2021-03-26  Akim Demaille  <akim@lrde.epita.fr>
 
        fprintf-posix: fix typo
index 3fc9be5611128a71813b890e258a22163c2ed757..272fe2a8572e2fb54edbd303ddc50856fa0b40cf 100644 (file)
@@ -1859,6 +1859,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
     /* errno is already set.  */
     return NULL;
 
+  /* Frees the memory allocated by this function.  Preserves errno.  */
 #define CLEANUP() \
   if (d.dir != d.direct_alloc_dir)                                      \
     free (d.dir);                                                       \
@@ -2187,6 +2188,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                               free (result);
                             if (buf_malloced != NULL)
                               free (buf_malloced);
+                            CLEANUP ();
                             return NULL;
                           }
                         if (converted != result + length)
@@ -2306,13 +2308,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 #  endif
                         if (converted == NULL)
                           {
-                            int saved_errno = errno;
                             if (!(result == resultbuf || result == NULL))
                               free (result);
                             if (buf_malloced != NULL)
                               free (buf_malloced);
                             CLEANUP ();
-                            errno = saved_errno;
                             return NULL;
                           }
                         if (converted != result + length)
@@ -2432,13 +2432,11 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 #  endif
                         if (converted == NULL)
                           {
-                            int saved_errno = errno;
                             if (!(result == resultbuf || result == NULL))
                               free (result);
                             if (buf_malloced != NULL)
                               free (buf_malloced);
                             CLEANUP ();
-                            errno = saved_errno;
                             return NULL;
                           }
                         if (converted != result + length)
@@ -2849,14 +2847,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                                               NULL, &tmpdst_len);
                   if (tmpdst == NULL)
                     {
-                      int saved_errno = errno;
                       free (tmpsrc);
                       if (!(result == resultbuf || result == NULL))
                         free (result);
                       if (buf_malloced != NULL)
                         free (buf_malloced);
                       CLEANUP ();
-                      errno = saved_errno;
                       return NULL;
                     }
                   free (tmpsrc);
@@ -5444,15 +5440,14 @@ 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 (saved_errno == 0)
+                        /* SNPRINTF or sprintf failed.  Use the errno that it
+                           has set, if any.  */
+                        if (errno == 0)
                           {
                             if (dp->conversion == 'c' || dp->conversion == 's')
-                              saved_errno = EILSEQ;
+                              errno = EILSEQ;
                             else
-                              saved_errno = EINVAL;
+                              errno = EINVAL;
                           }
 
                         if (!(result == resultbuf || result == NULL))
@@ -5461,7 +5456,6 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
                           free (buf_malloced);
                         CLEANUP ();
 
-                        errno = saved_errno;
                         return NULL;
                       }