summaryrefslogtreecommitdiffstats
path: root/gl/vasnprintf.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/vasnprintf.c')
-rw-r--r--gl/vasnprintf.c241
1 files changed, 55 insertions, 186 deletions
diff --git a/gl/vasnprintf.c b/gl/vasnprintf.c
index d9b669d..285c674 100644
--- a/gl/vasnprintf.c
+++ b/gl/vasnprintf.c
@@ -1,5 +1,5 @@
1/* vsprintf with automatic memory allocation. 1/* vsprintf with automatic memory allocation.
2 Copyright (C) 1999, 2002-2021 Free Software Foundation, Inc. 2 Copyright (C) 1999, 2002-2022 Free Software Foundation, Inc.
3 3
4 This file is free software: you can redistribute it and/or modify 4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as 5 it under the terms of the GNU Lesser General Public License as
@@ -915,8 +915,7 @@ divide (mpn_t a, mpn_t b, mpn_t *q)
915 q_ptr[q_len++] = 1; 915 q_ptr[q_len++] = 1;
916 } 916 }
917 keep_q: 917 keep_q:
918 if (tmp_roomptr != NULL) 918 free (tmp_roomptr);
919 free (tmp_roomptr);
920 q->limbs = q_ptr; 919 q->limbs = q_ptr;
921 q->nlimbs = q_len; 920 q->nlimbs = q_len;
922 return roomptr; 921 return roomptr;
@@ -1873,11 +1872,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1873 free (a.arg); 1872 free (a.arg);
1874 1873
1875 if (PRINTF_FETCHARGS (args, &a) < 0) 1874 if (PRINTF_FETCHARGS (args, &a) < 0)
1876 { 1875 goto fail_1_with_EINVAL;
1877 CLEANUP ();
1878 errno = EINVAL;
1879 return NULL;
1880 }
1881 1876
1882 { 1877 {
1883 size_t buf_neededlength; 1878 size_t buf_neededlength;
@@ -1913,19 +1908,12 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1913 buf_malloced = buf; 1908 buf_malloced = buf;
1914 } 1909 }
1915 1910
1916 if (resultbuf != NULL) 1911 result = resultbuf;
1917 { 1912 allocated = (resultbuf != NULL ? *lengthp : 0);
1918 result = resultbuf;
1919 allocated = *lengthp;
1920 }
1921 else
1922 {
1923 result = NULL;
1924 allocated = 0;
1925 }
1926 length = 0; 1913 length = 0;
1927 /* Invariants: 1914 /* Invariants:
1928 result is either == resultbuf or == NULL or malloc-allocated. 1915 result is either == resultbuf or malloc-allocated.
1916 If result == NULL, resultbuf is == NULL as well.
1929 If length > 0, then result != NULL. */ 1917 If length > 0, then result != NULL. */
1930 1918
1931 /* Ensures that allocated >= needed. Aborts through a jump to 1919 /* Ensures that allocated >= needed. Aborts through a jump to
@@ -1942,7 +1930,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
1942 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ 1930 memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
1943 if (size_overflow_p (memory_size)) \ 1931 if (size_overflow_p (memory_size)) \
1944 oom_statement \ 1932 oom_statement \
1945 if (result == resultbuf || result == NULL) \ 1933 if (result == resultbuf) \
1946 memory = (DCHAR_T *) malloc (memory_size); \ 1934 memory = (DCHAR_T *) malloc (memory_size); \
1947 else \ 1935 else \
1948 memory = (DCHAR_T *) realloc (result, memory_size); \ 1936 memory = (DCHAR_T *) realloc (result, memory_size); \
@@ -2112,15 +2100,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2112 if (count == 0) 2100 if (count == 0)
2113 break; 2101 break;
2114 if (count < 0) 2102 if (count < 0)
2115 { 2103 goto fail_with_EILSEQ;
2116 if (!(result == resultbuf || result == NULL))
2117 free (result);
2118 if (buf_malloced != NULL)
2119 free (buf_malloced);
2120 CLEANUP ();
2121 errno = EILSEQ;
2122 return NULL;
2123 }
2124 arg_end += count; 2104 arg_end += count;
2125 characters++; 2105 characters++;
2126 } 2106 }
@@ -2137,15 +2117,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2137 if (count == 0) 2117 if (count == 0)
2138 break; 2118 break;
2139 if (count < 0) 2119 if (count < 0)
2140 { 2120 goto fail_with_EILSEQ;
2141 if (!(result == resultbuf || result == NULL))
2142 free (result);
2143 if (buf_malloced != NULL)
2144 free (buf_malloced);
2145 CLEANUP ();
2146 errno = EILSEQ;
2147 return NULL;
2148 }
2149 arg_end += count; 2121 arg_end += count;
2150 characters++; 2122 characters++;
2151 } 2123 }
@@ -2191,14 +2163,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2191 converted, &converted_len); 2163 converted, &converted_len);
2192# endif 2164# endif
2193 if (converted == NULL) 2165 if (converted == NULL)
2194 { 2166 goto fail_with_errno;
2195 if (!(result == resultbuf || result == NULL))
2196 free (result);
2197 if (buf_malloced != NULL)
2198 free (buf_malloced);
2199 CLEANUP ();
2200 return NULL;
2201 }
2202 if (converted != result + length) 2167 if (converted != result + length)
2203 { 2168 {
2204 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), 2169 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
@@ -2237,15 +2202,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2237 if (count == 0) 2202 if (count == 0)
2238 break; 2203 break;
2239 if (count < 0) 2204 if (count < 0)
2240 { 2205 goto fail_with_EILSEQ;
2241 if (!(result == resultbuf || result == NULL))
2242 free (result);
2243 if (buf_malloced != NULL)
2244 free (buf_malloced);
2245 CLEANUP ();
2246 errno = EILSEQ;
2247 return NULL;
2248 }
2249 arg_end += count; 2206 arg_end += count;
2250 characters++; 2207 characters++;
2251 } 2208 }
@@ -2262,15 +2219,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2262 if (count == 0) 2219 if (count == 0)
2263 break; 2220 break;
2264 if (count < 0) 2221 if (count < 0)
2265 { 2222 goto fail_with_EILSEQ;
2266 if (!(result == resultbuf || result == NULL))
2267 free (result);
2268 if (buf_malloced != NULL)
2269 free (buf_malloced);
2270 CLEANUP ();
2271 errno = EILSEQ;
2272 return NULL;
2273 }
2274 arg_end += count; 2223 arg_end += count;
2275 characters++; 2224 characters++;
2276 } 2225 }
@@ -2316,14 +2265,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2316 converted, &converted_len); 2265 converted, &converted_len);
2317# endif 2266# endif
2318 if (converted == NULL) 2267 if (converted == NULL)
2319 { 2268 goto fail_with_errno;
2320 if (!(result == resultbuf || result == NULL))
2321 free (result);
2322 if (buf_malloced != NULL)
2323 free (buf_malloced);
2324 CLEANUP ();
2325 return NULL;
2326 }
2327 if (converted != result + length) 2269 if (converted != result + length)
2328 { 2270 {
2329 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), 2271 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
@@ -2362,15 +2304,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2362 if (count == 0) 2304 if (count == 0)
2363 break; 2305 break;
2364 if (count < 0) 2306 if (count < 0)
2365 { 2307 goto fail_with_EILSEQ;
2366 if (!(result == resultbuf || result == NULL))
2367 free (result);
2368 if (buf_malloced != NULL)
2369 free (buf_malloced);
2370 CLEANUP ();
2371 errno = EILSEQ;
2372 return NULL;
2373 }
2374 arg_end += count; 2308 arg_end += count;
2375 characters++; 2309 characters++;
2376 } 2310 }
@@ -2387,15 +2321,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2387 if (count == 0) 2321 if (count == 0)
2388 break; 2322 break;
2389 if (count < 0) 2323 if (count < 0)
2390 { 2324 goto fail_with_EILSEQ;
2391 if (!(result == resultbuf || result == NULL))
2392 free (result);
2393 if (buf_malloced != NULL)
2394 free (buf_malloced);
2395 CLEANUP ();
2396 errno = EILSEQ;
2397 return NULL;
2398 }
2399 arg_end += count; 2325 arg_end += count;
2400 characters++; 2326 characters++;
2401 } 2327 }
@@ -2441,14 +2367,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2441 converted, &converted_len); 2367 converted, &converted_len);
2442# endif 2368# endif
2443 if (converted == NULL) 2369 if (converted == NULL)
2444 { 2370 goto fail_with_errno;
2445 if (!(result == resultbuf || result == NULL))
2446 free (result);
2447 if (buf_malloced != NULL)
2448 free (buf_malloced);
2449 CLEANUP ();
2450 return NULL;
2451 }
2452 if (converted != result + length) 2371 if (converted != result + length)
2453 { 2372 {
2454 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len), 2373 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
@@ -2590,16 +2509,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2590 /* Found the terminating NUL. */ 2509 /* Found the terminating NUL. */
2591 break; 2510 break;
2592 if (count < 0) 2511 if (count < 0)
2593 { 2512 /* Invalid or incomplete multibyte character. */
2594 /* Invalid or incomplete multibyte character. */ 2513 goto fail_with_EILSEQ;
2595 if (!(result == resultbuf || result == NULL))
2596 free (result);
2597 if (buf_malloced != NULL)
2598 free (buf_malloced);
2599 CLEANUP ();
2600 errno = EILSEQ;
2601 return NULL;
2602 }
2603 arg_end += count; 2514 arg_end += count;
2604 characters++; 2515 characters++;
2605 } 2516 }
@@ -2626,16 +2537,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2626 /* Found the terminating NUL. */ 2537 /* Found the terminating NUL. */
2627 break; 2538 break;
2628 if (count < 0) 2539 if (count < 0)
2629 { 2540 /* Invalid or incomplete multibyte character. */
2630 /* Invalid or incomplete multibyte character. */ 2541 goto fail_with_EILSEQ;
2631 if (!(result == resultbuf || result == NULL))
2632 free (result);
2633 if (buf_malloced != NULL)
2634 free (buf_malloced);
2635 CLEANUP ();
2636 errno = EILSEQ;
2637 return NULL;
2638 }
2639 arg_end += count; 2542 arg_end += count;
2640 characters++; 2543 characters++;
2641 } 2544 }
@@ -2752,16 +2655,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2752 break; 2655 break;
2753 count = local_wcrtomb (cbuf, *arg_end, &state); 2656 count = local_wcrtomb (cbuf, *arg_end, &state);
2754 if (count < 0) 2657 if (count < 0)
2755 { 2658 /* Cannot convert. */
2756 /* Cannot convert. */ 2659 goto fail_with_EILSEQ;
2757 if (!(result == resultbuf || result == NULL))
2758 free (result);
2759 if (buf_malloced != NULL)
2760 free (buf_malloced);
2761 CLEANUP ();
2762 errno = EILSEQ;
2763 return NULL;
2764 }
2765 if (precision < (unsigned int) count) 2660 if (precision < (unsigned int) count)
2766 break; 2661 break;
2767 arg_end++; 2662 arg_end++;
@@ -2793,16 +2688,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2793 break; 2688 break;
2794 count = local_wcrtomb (cbuf, *arg_end, &state); 2689 count = local_wcrtomb (cbuf, *arg_end, &state);
2795 if (count < 0) 2690 if (count < 0)
2796 { 2691 /* Cannot convert. */
2797 /* Cannot convert. */ 2692 goto fail_with_EILSEQ;
2798 if (!(result == resultbuf || result == NULL))
2799 free (result);
2800 if (buf_malloced != NULL)
2801 free (buf_malloced);
2802 CLEANUP ();
2803 errno = EILSEQ;
2804 return NULL;
2805 }
2806 arg_end++; 2693 arg_end++;
2807 characters += count; 2694 characters += count;
2808 } 2695 }
@@ -2859,12 +2746,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2859 if (tmpdst == NULL) 2746 if (tmpdst == NULL)
2860 { 2747 {
2861 free (tmpsrc); 2748 free (tmpsrc);
2862 if (!(result == resultbuf || result == NULL)) 2749 goto fail_with_errno;
2863 free (result);
2864 if (buf_malloced != NULL)
2865 free (buf_malloced);
2866 CLEANUP ();
2867 return NULL;
2868 } 2750 }
2869 free (tmpsrc); 2751 free (tmpsrc);
2870# endif 2752# endif
@@ -2938,16 +2820,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
2938 abort (); 2820 abort ();
2939 count = local_wcrtomb (cbuf, *arg, &state); 2821 count = local_wcrtomb (cbuf, *arg, &state);
2940 if (count <= 0) 2822 if (count <= 0)
2941 { 2823 /* Cannot convert. */
2942 /* Cannot convert. */ 2824 goto fail_with_EILSEQ;
2943 if (!(result == resultbuf || result == NULL))
2944 free (result);
2945 if (buf_malloced != NULL)
2946 free (buf_malloced);
2947 CLEANUP ();
2948 errno = EILSEQ;
2949 return NULL;
2950 }
2951 ENSURE_ALLOCATION (xsum (length, count)); 2825 ENSURE_ALLOCATION (xsum (length, count));
2952 memcpy (result + length, cbuf, count); 2826 memcpy (result + length, cbuf, count);
2953 length += count; 2827 length += count;
@@ -3083,14 +2957,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
3083 NULL, 2957 NULL,
3084 NULL, &tmpdst_len); 2958 NULL, &tmpdst_len);
3085 if (tmpdst == NULL) 2959 if (tmpdst == NULL)
3086 { 2960 goto fail_with_errno;
3087 if (!(result == resultbuf || result == NULL))
3088 free (result);
3089 if (buf_malloced != NULL)
3090 free (buf_malloced);
3091 CLEANUP ();
3092 return NULL;
3093 }
3094# endif 2961# endif
3095 2962
3096 if (has_width) 2963 if (has_width)
@@ -5463,13 +5330,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5463 errno = EINVAL; 5330 errno = EINVAL;
5464 } 5331 }
5465 5332
5466 if (!(result == resultbuf || result == NULL)) 5333 goto fail_with_errno;
5467 free (result);
5468 if (buf_malloced != NULL)
5469 free (buf_malloced);
5470 CLEANUP ();
5471
5472 return NULL;
5473 } 5334 }
5474 5335
5475#if USE_SNPRINTF 5336#if USE_SNPRINTF
@@ -5603,14 +5464,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5603 NULL, 5464 NULL,
5604 NULL, &tmpdst_len); 5465 NULL, &tmpdst_len);
5605 if (tmpdst == NULL) 5466 if (tmpdst == NULL)
5606 { 5467 goto fail_with_errno;
5607 if (!(result == resultbuf || result == NULL))
5608 free (result);
5609 if (buf_malloced != NULL)
5610 free (buf_malloced);
5611 CLEANUP ();
5612 return NULL;
5613 }
5614 ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len), 5468 ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len),
5615 { free (tmpdst); goto out_of_memory; }); 5469 { free (tmpdst); goto out_of_memory; });
5616 DCHAR_CPY (result + length, tmpdst, tmpdst_len); 5470 DCHAR_CPY (result + length, tmpdst, tmpdst_len);
@@ -5835,25 +5689,40 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
5835 5689
5836#if USE_SNPRINTF 5690#if USE_SNPRINTF
5837 overflow: 5691 overflow:
5838 if (!(result == resultbuf || result == NULL))
5839 free (result);
5840 if (buf_malloced != NULL)
5841 free (buf_malloced);
5842 CLEANUP ();
5843 errno = EOVERFLOW; 5692 errno = EOVERFLOW;
5844 return NULL; 5693 goto fail_with_errno;
5845#endif 5694#endif
5846 5695
5847 out_of_memory: 5696 out_of_memory:
5848 if (!(result == resultbuf || result == NULL)) 5697 errno = ENOMEM;
5698 goto fail_with_errno;
5699
5700#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)
5701 fail_with_EILSEQ:
5702 errno = EILSEQ;
5703 goto fail_with_errno;
5704#endif
5705
5706 fail_with_errno:
5707 if (result != resultbuf)
5849 free (result); 5708 free (result);
5850 if (buf_malloced != NULL) 5709 if (buf_malloced != NULL)
5851 free (buf_malloced); 5710 free (buf_malloced);
5852 out_of_memory_1:
5853 CLEANUP (); 5711 CLEANUP ();
5854 errno = ENOMEM;
5855 return NULL; 5712 return NULL;
5856 } 5713 }
5714
5715 out_of_memory_1:
5716 errno = ENOMEM;
5717 goto fail_1_with_errno;
5718
5719 fail_1_with_EINVAL:
5720 errno = EINVAL;
5721 goto fail_1_with_errno;
5722
5723 fail_1_with_errno:
5724 CLEANUP ();
5725 return NULL;
5857} 5726}
5858 5727
5859#undef MAX_ROOM_NEEDED 5728#undef MAX_ROOM_NEEDED