(root)/
glibc-2.38/
libio/
vasprintf.c
       1  /* Copyright (C) 1995-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.
      17  
      18     As a special exception, if you link the code in this file with
      19     files compiled with a GNU compiler to produce an executable,
      20     that does not cause the resulting executable to be covered by
      21     the GNU Lesser General Public License.  This exception does not
      22     however invalidate any other reasons why the executable file
      23     might be covered by the GNU Lesser General Public License.
      24     This exception applies to code released by its copyright holders
      25     in files containing the exception.  */
      26  
      27  #include <array_length.h>
      28  #include <errno.h>
      29  #include <limits.h>
      30  #include <math_ldbl_opt.h>
      31  #include <printf.h>
      32  #include <stdio.h>
      33  #include <stdlib.h>
      34  #include <string.h>
      35  #include <printf_buffer.h>
      36  
      37  struct __printf_buffer_asprintf
      38  {
      39    /* base.write_base points either to a heap-allocated buffer, or to
      40       the direct array below.  */
      41    struct __printf_buffer base;
      42  
      43    /* Initial allocation.  200 should be large enough to copy almost
      44       all asprintf usages with just a single (final, correctly sized)
      45       heap allocation.  */
      46    char direct[PRINTF_BUFFER_SIZE_ASPRINTF];
      47  };
      48  
      49  void
      50  __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf)
      51  {
      52    size_t current_pos = buf->base.write_ptr - buf->base.write_base;
      53    if (current_pos >= INT_MAX)
      54      {
      55        /* The result is not representable.  No need to continue.  */
      56        __set_errno (EOVERFLOW);
      57        __printf_buffer_mark_failed (&buf->base);
      58        return;
      59      }
      60  
      61    size_t current_size = buf->base.write_end - buf->base.write_base;
      62    /* Implement an exponentiation sizing policy.  Keep the size
      63       congruent 8 (mod 16), to account for the footer in glibc
      64       malloc.  */
      65    size_t new_size = ALIGN_UP (current_size + current_size / 2, 16) | 8;
      66    char *new_buffer;
      67    if (buf->base.write_base == buf->direct)
      68      {
      69        new_buffer = malloc (new_size);
      70        if (new_buffer == NULL)
      71  	{
      72  	  __printf_buffer_mark_failed (&buf->base);
      73  	  return;
      74  	}
      75        memcpy (new_buffer, buf->direct, current_pos);
      76      }
      77    else
      78      {
      79        new_buffer = realloc (buf->base.write_base, new_size);
      80        if (new_buffer == NULL)
      81  	{
      82  	  __printf_buffer_mark_failed (&buf->base);
      83  	  return;
      84  	}
      85      }
      86  
      87    /* Set up the new write area.  */
      88    buf->base.write_base = new_buffer;
      89    buf->base.write_ptr = new_buffer + current_pos;
      90    buf->base.write_end = new_buffer + new_size;
      91  }
      92  
      93  
      94  int
      95  __vasprintf_internal (char **result_ptr, const char *format, va_list args,
      96  		      unsigned int mode_flags)
      97  {
      98    struct __printf_buffer_asprintf buf;
      99    __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct),
     100  			__printf_buffer_mode_asprintf);
     101  
     102    __printf_buffer (&buf.base, format, args, mode_flags);
     103    int done = __printf_buffer_done (&buf.base);
     104    if (done < 0)
     105      {
     106        if (buf.base.write_base != buf.direct)
     107  	free (buf.base.write_base);
     108        return done;
     109      }
     110  
     111    /* Transfer to the final buffer.  */
     112    char *result;
     113    size_t size = buf.base.write_ptr - buf.base.write_base;
     114    if (buf.base.write_base == buf.direct)
     115      {
     116        result = malloc (size + 1);
     117        if (result == NULL)
     118  	return -1;
     119        memcpy (result, buf.direct, size);
     120      }
     121    else
     122      {
     123        result = realloc (buf.base.write_base, size + 1);
     124        if (result == NULL)
     125  	{
     126  	  free (buf.base.write_base);
     127  	  return -1;
     128  	}
     129      }
     130  
     131    /* Add NUL termination.  */
     132    result[size] = '\0';
     133    *result_ptr = result;
     134  
     135    return done;
     136  }
     137  
     138  int
     139  __vasprintf (char **result_ptr, const char *format, va_list args)
     140  {
     141    return __vasprintf_internal (result_ptr, format, args, 0);
     142  }
     143  ldbl_weak_alias (__vasprintf, vasprintf)