Austin Schuh | bb1338c | 2024-06-15 19:31:16 -0700 | [diff] [blame] | 1 | /* gmp_vasprintf -- formatted output to an allocated space. |
| 2 | |
| 3 | Copyright 2001 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of the GNU MP Library. |
| 6 | |
| 7 | The GNU MP Library is free software; you can redistribute it and/or modify |
| 8 | it under the terms of either: |
| 9 | |
| 10 | * the GNU Lesser General Public License as published by the Free |
| 11 | Software Foundation; either version 3 of the License, or (at your |
| 12 | option) any later version. |
| 13 | |
| 14 | or |
| 15 | |
| 16 | * the GNU General Public License as published by the Free Software |
| 17 | Foundation; either version 2 of the License, or (at your option) any |
| 18 | later version. |
| 19 | |
| 20 | or both in parallel, as here. |
| 21 | |
| 22 | The GNU MP Library is distributed in the hope that it will be useful, but |
| 23 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 24 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 25 | for more details. |
| 26 | |
| 27 | You should have received copies of the GNU General Public License and the |
| 28 | GNU Lesser General Public License along with the GNU MP Library. If not, |
| 29 | see https://www.gnu.org/licenses/. */ |
| 30 | |
| 31 | #include <stdarg.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include "gmp-impl.h" |
| 37 | |
| 38 | #if ! HAVE_VSNPRINTF |
| 39 | #define vsnprintf __gmp_replacement_vsnprintf |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | /* vasprintf isn't used since we prefer all GMP allocs to go through |
| 44 | __gmp_allocate_func, and in particular we don't want the -1 return from |
| 45 | vasprintf for out-of-memory, instead __gmp_allocate_func should handle |
| 46 | that. Using vsnprintf unfortunately means we might have to re-run it if |
| 47 | our current space is insufficient. |
| 48 | |
| 49 | The initial guess for the needed space is an arbitrary 256 bytes. If |
| 50 | that (and any extra GMP_ASPRINTF_T_NEED might give) isn't enough then an |
| 51 | ISO C99 standard vsnprintf will tell us what we really need. |
| 52 | |
| 53 | GLIBC 2.0.x vsnprintf returns either -1 or space-1 to indicate overflow, |
| 54 | without giving any indication how much is really needed. In this case |
| 55 | keep trying with double the space each time. |
| 56 | |
| 57 | A return of space-1 is success on a C99 vsnprintf, but we're not |
| 58 | bothering to identify which style vsnprintf we've got, so just take the |
| 59 | pessimistic option and assume it's glibc 2.0.x. |
| 60 | |
| 61 | Notice the use of ret+2 for the new space in the C99 case. This ensures |
| 62 | the next vsnprintf return value will be space-2, which is unambiguously |
| 63 | successful. But actually GMP_ASPRINTF_T_NEED() will realloc to even |
| 64 | bigger than that ret+2. |
| 65 | |
| 66 | vsnprintf might trash it's given ap, so copy it in case we need to use it |
| 67 | more than once. See comments with gmp_snprintf_format. */ |
| 68 | |
| 69 | static int |
| 70 | gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt, |
| 71 | va_list orig_ap) |
| 72 | { |
| 73 | int ret; |
| 74 | va_list ap; |
| 75 | size_t space = 256; |
| 76 | |
| 77 | for (;;) |
| 78 | { |
| 79 | GMP_ASPRINTF_T_NEED (d, space); |
| 80 | space = d->alloc - d->size; |
| 81 | va_copy (ap, orig_ap); |
| 82 | ret = vsnprintf (d->buf + d->size, space, fmt, ap); |
| 83 | if (ret == -1) |
| 84 | { |
| 85 | ASSERT (strlen (d->buf + d->size) == space-1); |
| 86 | ret = space-1; |
| 87 | } |
| 88 | |
| 89 | /* done if output fits in our space */ |
| 90 | if (ret < space-1) |
| 91 | break; |
| 92 | |
| 93 | if (ret == space-1) |
| 94 | space *= 2; /* possible glibc 2.0.x, so double */ |
| 95 | else |
| 96 | space = ret+2; /* C99, so now know space required */ |
| 97 | } |
| 98 | |
| 99 | d->size += ret; |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | const struct doprnt_funs_t __gmp_asprintf_funs = { |
| 104 | (doprnt_format_t) gmp_asprintf_format, |
| 105 | (doprnt_memory_t) __gmp_asprintf_memory, |
| 106 | (doprnt_reps_t) __gmp_asprintf_reps, |
| 107 | (doprnt_final_t) __gmp_asprintf_final |
| 108 | }; |
| 109 | |
| 110 | int |
| 111 | gmp_vasprintf (char **result, const char *fmt, va_list ap) |
| 112 | { |
| 113 | struct gmp_asprintf_t d; |
| 114 | GMP_ASPRINTF_T_INIT (d, result); |
| 115 | return __gmp_doprnt (&__gmp_asprintf_funs, &d, fmt, ap); |
| 116 | } |