Hi Paul, I wrote: > I'll rework your patch; thanks for the ideas!
I pushed these three patches in your name. (I hope that's fine with you.) Bruno
From da95c0389f5e8c668d6c8c16462669135f02fb38 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Sun, 1 May 2022 22:38:50 +0200 Subject: [PATCH 1/3] vasnprintf: Simplify a free() call. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lib/vasnprintf.c (divide): Just call free (x) instead of doing ‘if (x != NULL) free (x);’. --- ChangeLog | 6 ++++++ lib/vasnprintf.c | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c7ed13141..808b3756e0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2022-05-01 Paul Eggert <egg...@cs.ucla.edu> + + vasnprintf: Simplify a free() call. + * lib/vasnprintf.c (divide): Just call + free (x) instead of doing ‘if (x != NULL) free (x);’. + 2022-04-30 Bruno Haible <br...@clisp.org> string: Avoid syntax error on glibc systems with GCC 11. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 485745243f..23933806df 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -915,8 +915,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q) q_ptr[q_len++] = 1; } keep_q: - if (tmp_roomptr != NULL) - free (tmp_roomptr); + free (tmp_roomptr); q->limbs = q_ptr; q->nlimbs = q_len; return roomptr; -- 2.25.1
From eb4e77666b2c540f77328156d4c43681aa1f99b0 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Sun, 1 May 2022 22:48:07 +0200 Subject: [PATCH 2/3] vasnprintf: Simplify 'result' variable. * lib/vasnprintf.c (VASNPRINTF): Simplify initialization and test of 'result' variable. --- ChangeLog | 4 ++++ lib/vasnprintf.c | 57 +++++++++++++++++++++--------------------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/ChangeLog b/ChangeLog index 808b3756e0..8f6d862263 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ 2022-05-01 Paul Eggert <egg...@cs.ucla.edu> + vasnprintf: Simplify 'result' variable. + * lib/vasnprintf.c (VASNPRINTF): Simplify initialization and test of + 'result' variable. + vasnprintf: Simplify a free() call. * lib/vasnprintf.c (divide): Just call free (x) instead of doing ‘if (x != NULL) free (x);’. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index 23933806df..da9abe4b74 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1912,19 +1912,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, buf_malloced = buf; } - if (resultbuf != NULL) - { - result = resultbuf; - allocated = *lengthp; - } - else - { - result = NULL; - allocated = 0; - } + result = resultbuf; + allocated = (resultbuf != NULL ? *lengthp : 0); length = 0; /* Invariants: - result is either == resultbuf or == NULL or malloc-allocated. + result is either == resultbuf or malloc-allocated. + If result == NULL, resultbuf is == NULL as well. If length > 0, then result != NULL. */ /* Ensures that allocated >= needed. Aborts through a jump to @@ -1941,7 +1934,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ if (size_overflow_p (memory_size)) \ oom_statement \ - if (result == resultbuf || result == NULL) \ + if (result == resultbuf) \ memory = (DCHAR_T *) malloc (memory_size); \ else \ memory = (DCHAR_T *) realloc (result, memory_size); \ @@ -2112,7 +2105,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2137,7 +2130,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2191,7 +2184,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif if (converted == NULL) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2237,7 +2230,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2262,7 +2255,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2316,7 +2309,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif if (converted == NULL) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2362,7 +2355,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2387,7 +2380,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; if (count < 0) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2441,7 +2434,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, # endif if (converted == NULL) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2591,7 +2584,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count < 0) { /* Invalid or incomplete multibyte character. */ - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2627,7 +2620,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count < 0) { /* Invalid or incomplete multibyte character. */ - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2753,7 +2746,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count < 0) { /* Cannot convert. */ - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2794,7 +2787,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count < 0) { /* Cannot convert. */ - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2858,7 +2851,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (tmpdst == NULL) { free (tmpsrc); - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -2939,7 +2932,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count <= 0) { /* Cannot convert. */ - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -3083,7 +3076,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, NULL, &tmpdst_len); if (tmpdst == NULL) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -5462,7 +5455,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, errno = EINVAL; } - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -5603,7 +5596,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, NULL, &tmpdst_len); if (tmpdst == NULL) { - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -5834,7 +5827,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #if USE_SNPRINTF overflow: - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); @@ -5844,7 +5837,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #endif out_of_memory: - if (!(result == resultbuf || result == NULL)) + if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); -- 2.25.1
>From bd11400942d63de12371988dca8144925de9e2c3 Mon Sep 17 00:00:00 2001 From: Paul Eggert <egg...@cs.ucla.edu> Date: Sun, 1 May 2022 23:24:02 +0200 Subject: [PATCH 3/3] vasnprintf: Simplify. Reduce binary code size. * lib/vasnprintf.c (VASNPRINTF): Coalesce cleanup code. --- ChangeLog | 3 + lib/vasnprintf.c | 217 ++++++++++------------------------------------- 2 files changed, 50 insertions(+), 170 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8f6d862263..5749e2dc69 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2022-05-01 Paul Eggert <egg...@cs.ucla.edu> + vasnprintf: Simplify. Reduce binary code size. + * lib/vasnprintf.c (VASNPRINTF): Coalesce cleanup code. + vasnprintf: Simplify 'result' variable. * lib/vasnprintf.c (VASNPRINTF): Simplify initialization and test of 'result' variable. diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c index da9abe4b74..285c674b9c 100644 --- a/lib/vasnprintf.c +++ b/lib/vasnprintf.c @@ -1872,11 +1872,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, free (a.arg); if (PRINTF_FETCHARGS (args, &a) < 0) - { - CLEANUP (); - errno = EINVAL; - return NULL; - } + goto fail_1_with_EINVAL; { size_t buf_neededlength; @@ -2104,15 +2100,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2129,15 +2117,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2183,14 +2163,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, converted, &converted_len); # endif if (converted == NULL) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; - } + goto fail_with_errno; if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), @@ -2229,15 +2202,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2254,15 +2219,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2308,14 +2265,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, converted, &converted_len); # endif if (converted == NULL) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; - } + goto fail_with_errno; if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), @@ -2354,15 +2304,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2379,15 +2321,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (count == 0) break; if (count < 0) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2433,14 +2367,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, converted, &converted_len); # endif if (converted == NULL) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; - } + goto fail_with_errno; if (converted != result + length) { ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), @@ -2582,16 +2509,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Found the terminating NUL. */ break; if (count < 0) - { - /* Invalid or incomplete multibyte character. */ - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + /* Invalid or incomplete multibyte character. */ + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2618,16 +2537,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, /* Found the terminating NUL. */ break; if (count < 0) - { - /* Invalid or incomplete multibyte character. */ - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + /* Invalid or incomplete multibyte character. */ + goto fail_with_EILSEQ; arg_end += count; characters++; } @@ -2744,16 +2655,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; count = local_wcrtomb (cbuf, *arg_end, &state); if (count < 0) - { - /* Cannot convert. */ - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + /* Cannot convert. */ + goto fail_with_EILSEQ; if (precision < (unsigned int) count) break; arg_end++; @@ -2785,16 +2688,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, break; count = local_wcrtomb (cbuf, *arg_end, &state); if (count < 0) - { - /* Cannot convert. */ - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + /* Cannot convert. */ + goto fail_with_EILSEQ; arg_end++; characters += count; } @@ -2851,12 +2746,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, if (tmpdst == NULL) { free (tmpsrc); - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; + goto fail_with_errno; } free (tmpsrc); # endif @@ -2930,16 +2820,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, abort (); count = local_wcrtomb (cbuf, *arg, &state); if (count <= 0) - { - /* Cannot convert. */ - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - errno = EILSEQ; - return NULL; - } + /* Cannot convert. */ + goto fail_with_EILSEQ; ENSURE_ALLOCATION (xsum (length, count)); memcpy (result + length, cbuf, count); length += count; @@ -3075,14 +2957,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, NULL, NULL, &tmpdst_len); if (tmpdst == NULL) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; - } + goto fail_with_errno; # endif if (has_width) @@ -5455,13 +5330,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, errno = EINVAL; } - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - - return NULL; + goto fail_with_errno; } #if USE_SNPRINTF @@ -5595,14 +5464,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, NULL, NULL, &tmpdst_len); if (tmpdst == NULL) - { - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); - return NULL; - } + goto fail_with_errno; ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len), { free (tmpdst); goto out_of_memory; }); DCHAR_CPY (result + length, tmpdst, tmpdst_len); @@ -5827,25 +5689,40 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp, #if USE_SNPRINTF overflow: - if (result != resultbuf) - free (result); - if (buf_malloced != NULL) - free (buf_malloced); - CLEANUP (); errno = EOVERFLOW; - return NULL; + goto fail_with_errno; #endif out_of_memory: + errno = ENOMEM; + goto fail_with_errno; + +#if ENABLE_UNISTDIO || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T) + fail_with_EILSEQ: + errno = EILSEQ; + goto fail_with_errno; +#endif + + fail_with_errno: if (result != resultbuf) free (result); if (buf_malloced != NULL) free (buf_malloced); - out_of_memory_1: CLEANUP (); - errno = ENOMEM; return NULL; } + + out_of_memory_1: + errno = ENOMEM; + goto fail_1_with_errno; + + fail_1_with_EINVAL: + errno = EINVAL; + goto fail_1_with_errno; + + fail_1_with_errno: + CLEANUP (); + return NULL; } #undef MAX_ROOM_NEEDED -- 2.25.1