Blame printf/vasprintf.c

Packit 5c3484
/* gmp_vasprintf -- formatted output to an allocated space.
Packit 5c3484
Packit 5c3484
Copyright 2001 Free Software Foundation, Inc.
Packit 5c3484
Packit 5c3484
This file is part of the GNU MP Library.
Packit 5c3484
Packit 5c3484
The GNU MP Library is free software; you can redistribute it and/or modify
Packit 5c3484
it under the terms of either:
Packit 5c3484
Packit 5c3484
  * the GNU Lesser General Public License as published by the Free
Packit 5c3484
    Software Foundation; either version 3 of the License, or (at your
Packit 5c3484
    option) any later version.
Packit 5c3484
Packit 5c3484
or
Packit 5c3484
Packit 5c3484
  * the GNU General Public License as published by the Free Software
Packit 5c3484
    Foundation; either version 2 of the License, or (at your option) any
Packit 5c3484
    later version.
Packit 5c3484
Packit 5c3484
or both in parallel, as here.
Packit 5c3484
Packit 5c3484
The GNU MP Library is distributed in the hope that it will be useful, but
Packit 5c3484
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
Packit 5c3484
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
Packit 5c3484
for more details.
Packit 5c3484
Packit 5c3484
You should have received copies of the GNU General Public License and the
Packit 5c3484
GNU Lesser General Public License along with the GNU MP Library.  If not,
Packit 5c3484
see https://www.gnu.org/licenses/.  */
Packit 5c3484
Packit 5c3484
#include <stdarg.h>
Packit 5c3484
#include <stdio.h>
Packit 5c3484
#include <stdlib.h>
Packit 5c3484
#include <string.h>
Packit 5c3484
Packit 5c3484
#include "gmp.h"
Packit 5c3484
#include "gmp-impl.h"
Packit 5c3484
Packit 5c3484
#if ! HAVE_VSNPRINTF
Packit 5c3484
#define vsnprintf  __gmp_replacement_vsnprintf
Packit 5c3484
#endif
Packit 5c3484
Packit 5c3484
Packit 5c3484
/* vasprintf isn't used since we prefer all GMP allocs to go through
Packit 5c3484
   __gmp_allocate_func, and in particular we don't want the -1 return from
Packit 5c3484
   vasprintf for out-of-memory, instead __gmp_allocate_func should handle
Packit 5c3484
   that.  Using vsnprintf unfortunately means we might have to re-run it if
Packit 5c3484
   our current space is insufficient.
Packit 5c3484
Packit 5c3484
   The initial guess for the needed space is an arbitrary 256 bytes.  If
Packit 5c3484
   that (and any extra GMP_ASPRINTF_T_NEED might give) isn't enough then an
Packit 5c3484
   ISO C99 standard vsnprintf will tell us what we really need.
Packit 5c3484
Packit 5c3484
   GLIBC 2.0.x vsnprintf returns either -1 or space-1 to indicate overflow,
Packit 5c3484
   without giving any indication how much is really needed.  In this case
Packit 5c3484
   keep trying with double the space each time.
Packit 5c3484
Packit 5c3484
   A return of space-1 is success on a C99 vsnprintf, but we're not
Packit 5c3484
   bothering to identify which style vsnprintf we've got, so just take the
Packit 5c3484
   pessimistic option and assume it's glibc 2.0.x.
Packit 5c3484
Packit 5c3484
   Notice the use of ret+2 for the new space in the C99 case.  This ensures
Packit 5c3484
   the next vsnprintf return value will be space-2, which is unambiguously
Packit 5c3484
   successful.  But actually GMP_ASPRINTF_T_NEED() will realloc to even
Packit 5c3484
   bigger than that ret+2.
Packit 5c3484
Packit 5c3484
   vsnprintf might trash it's given ap, so copy it in case we need to use it
Packit 5c3484
   more than once.  See comments with gmp_snprintf_format.  */
Packit 5c3484
Packit 5c3484
static int
Packit 5c3484
gmp_asprintf_format (struct gmp_asprintf_t *d, const char *fmt,
Packit 5c3484
                     va_list orig_ap)
Packit 5c3484
{
Packit 5c3484
  int      ret;
Packit 5c3484
  va_list  ap;
Packit 5c3484
  size_t   space = 256;
Packit 5c3484
Packit 5c3484
  for (;;)
Packit 5c3484
    {
Packit 5c3484
      GMP_ASPRINTF_T_NEED (d, space);
Packit 5c3484
      space = d->alloc - d->size;
Packit 5c3484
      va_copy (ap, orig_ap);
Packit 5c3484
      ret = vsnprintf (d->buf + d->size, space, fmt, ap);
Packit 5c3484
      if (ret == -1)
Packit 5c3484
        {
Packit 5c3484
          ASSERT (strlen (d->buf + d->size) == space-1);
Packit 5c3484
          ret = space-1;
Packit 5c3484
        }
Packit 5c3484
Packit 5c3484
      /* done if output fits in our space */
Packit 5c3484
      if (ret < space-1)
Packit 5c3484
        break;
Packit 5c3484
Packit 5c3484
      if (ret == space-1)
Packit 5c3484
        space *= 2;     /* possible glibc 2.0.x, so double */
Packit 5c3484
      else
Packit 5c3484
        space = ret+2;  /* C99, so now know space required */
Packit 5c3484
    }
Packit 5c3484
Packit 5c3484
  d->size += ret;
Packit 5c3484
  return ret;
Packit 5c3484
}
Packit 5c3484
Packit 5c3484
const struct doprnt_funs_t  __gmp_asprintf_funs = {
Packit 5c3484
  (doprnt_format_t) gmp_asprintf_format,
Packit 5c3484
  (doprnt_memory_t) __gmp_asprintf_memory,
Packit 5c3484
  (doprnt_reps_t)   __gmp_asprintf_reps,
Packit 5c3484
  (doprnt_final_t)  __gmp_asprintf_final
Packit 5c3484
};
Packit 5c3484
Packit 5c3484
int
Packit 5c3484
gmp_vasprintf (char **result, const char *fmt, va_list ap)
Packit 5c3484
{
Packit 5c3484
  struct gmp_asprintf_t  d;
Packit 5c3484
  GMP_ASPRINTF_T_INIT (d, result);
Packit 5c3484
  return __gmp_doprnt (&__gmp_asprintf_funs, &d, fmt, ap);
Packit 5c3484
}