From: Bruno Haible Date: Wed, 19 Jun 2024 12:57:40 +0000 (+0200) Subject: u*-vasnprintf: Fix a rare memory leak. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=4dd9d8a7fa85e9e9223a6c175b4a3fd46d222ff6;p=gnulib.git u*-vasnprintf: Fix a rare memory leak. * lib/vasnprintf.c (VASNPRINTF): Use ENSURE_ALLOCATION_ELSE instead of ENSURE_ALLOCATION, to free temporary allocations before failing due to out-of-memory. --- diff --git a/ChangeLog b/ChangeLog index a449920c84..34e01291ae 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2024-06-19 Bruno Haible + + u*-vasnprintf: Fix a rare memory leak. + * lib/vasnprintf.c (VASNPRINTF): Use ENSURE_ALLOCATION_ELSE instead of + ENSURE_ALLOCATION, to free temporary allocations before failing due to + out-of-memory. + 2024-06-19 Bruno Haible u*-vasnprintf: Improve comments. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index c49beddd26..ed2052c0ae 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -2580,7 +2580,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), - { free (converted); goto out_of_memory; }); + { free (converted); goto out_of_memory; }); DCHAR_CPY (result + length, converted, converted_len); free (converted); } @@ -2683,7 +2683,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), - { free (converted); goto out_of_memory; }); + { free (converted); goto out_of_memory; }); DCHAR_CPY (result + length, converted, converted_len); free (converted); } @@ -2786,7 +2786,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), - { free (converted); goto out_of_memory; }); + { free (converted); goto out_of_memory; }); DCHAR_CPY (result + length, converted, converted_len); free (converted); } @@ -3361,7 +3361,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (w < width && !(flags & FLAG_LEFT)) { size_t n = width - w; +# if DCHAR_IS_TCHAR ENSURE_ALLOCATION (xsum (length, n)); +# else + ENSURE_ALLOCATION_ELSE (xsum (length, n), + { free (tmpdst); goto out_of_memory; }); +# endif DCHAR_SET (result + length, ' ', n); length += n; } @@ -3420,7 +3425,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } # else ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len), - { free (tmpdst); goto out_of_memory; }); + { free (tmpdst); goto out_of_memory; }); DCHAR_CPY (result + length, tmpdst, tmpdst_len); free (tmpdst); length += tmpdst_len; @@ -3586,7 +3591,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (w < width && !(flags & FLAG_LEFT)) { size_t n = width - w; +# if DCHAR_IS_TCHAR ENSURE_ALLOCATION (xsum (length, n)); +# else + ENSURE_ALLOCATION_ELSE (xsum (length, n), + { free (tmpdst); goto out_of_memory; }); +# endif DCHAR_SET (result + length, ' ', n); length += n; } @@ -3630,7 +3640,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, } # else ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len), - { free (tmpdst); goto out_of_memory; }); + { free (tmpdst); goto out_of_memory; }); DCHAR_CPY (result + length, tmpdst, tmpdst_len); free (tmpdst); length += tmpdst_len; @@ -4025,7 +4035,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { size_t n = xsum (length, count); - ENSURE_ALLOCATION (n); + ENSURE_ALLOCATION_ELSE (n, + { if (tmp != tmpbuf) free (tmp); goto out_of_memory; }); } /* Append the result. */ @@ -4517,7 +4528,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { size_t n = xsum (length, count); - ENSURE_ALLOCATION (n); + ENSURE_ALLOCATION_ELSE (n, + { if (tmp != tmpbuf) free (tmp); goto out_of_memory; }); } /* Append the result. */ @@ -5702,7 +5714,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, { size_t n = xsum (length, count); - ENSURE_ALLOCATION (n); + ENSURE_ALLOCATION_ELSE (n, + { if (tmp != tmpbuf) free (tmp); goto out_of_memory; }); } /* Append the result. */ @@ -6809,7 +6822,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, goto fail_with_errno; # endif ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len), - { free (tmpdst); goto out_of_memory; }); + { free (tmpdst); goto out_of_memory; }); DCHAR_CPY (result + length, tmpdst, tmpdst_len); free (tmpdst); count = tmpdst_len;