aboutsummaryrefslogtreecommitdiff
path: root/libio/vasprintf.c
blob: 4f69cb61f95e95bb1e6be8e47c69ce0d82eac204 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* Copyright (C) 1995-2022 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.

   As a special exception, if you link the code in this file with
   files compiled with a GNU compiler to produce an executable,
   that does not cause the resulting executable to be covered by
   the GNU Lesser General Public License.  This exception does not
   however invalidate any other reasons why the executable file
   might be covered by the GNU Lesser General Public License.
   This exception applies to code released by its copyright holders
   in files containing the exception.  */

#include <array_length.h>
#include <errno.h>
#include <limits.h>
#include <math_ldbl_opt.h>
#include <printf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <printf_buffer.h>

struct __printf_buffer_asprintf
{
  /* base.write_base points either to a heap-allocated buffer, or to
     the direct array below.  */
  struct __printf_buffer base;

  /* Initial allocation.  200 should be large enough to copy almost
     all asprintf usages with just a single (final, correctly sized)
     heap allocation.  */
  char direct[PRINTF_BUFFER_SIZE_ASPRINTF];
};

void
__printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf)
{
  size_t current_pos = buf->base.write_ptr - buf->base.write_base;
  if (current_pos >= INT_MAX)
    {
      /* The result is not representable.  No need to continue.  */
      __set_errno (EOVERFLOW);
      __printf_buffer_mark_failed (&buf->base);
      return;
    }

  size_t current_size = buf->base.write_end - buf->base.write_base;
  /* Implement an exponentiation sizing policy.  Keep the size
     congruent 8 (mod 16), to account for the footer in glibc
     malloc.  */
  size_t new_size = ALIGN_UP (current_size + current_size / 2, 16) | 8;
  char *new_buffer;
  if (buf->base.write_base == buf->direct)
    {
      new_buffer = malloc (new_size);
      if (new_buffer == NULL)
	{
	  __printf_buffer_mark_failed (&buf->base);
	  return;
	}
      memcpy (new_buffer, buf->direct, current_pos);
    }
  else
    {
      new_buffer = realloc (buf->base.write_base, new_size);
      if (new_buffer == NULL)
	{
	  __printf_buffer_mark_failed (&buf->base);
	  return;
	}
    }

  /* Set up the new write area.  */
  buf->base.write_base = new_buffer;
  buf->base.write_ptr = new_buffer + current_pos;
  buf->base.write_end = new_buffer + new_size;
}


int
__vasprintf_internal (char **result_ptr, const char *format, va_list args,
		      unsigned int mode_flags)
{
  struct __printf_buffer_asprintf buf;
  __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct),
			__printf_buffer_mode_asprintf);

  __printf_buffer (&buf.base, format, args, mode_flags);
  int done = __printf_buffer_done (&buf.base);
  if (done < 0)
    {
      if (buf.base.write_base != buf.direct)
	free (buf.base.write_base);
      return done;
    }

  /* Transfer to the final buffer.  */
  char *result;
  size_t size = buf.base.write_ptr - buf.base.write_base;
  if (buf.base.write_base == buf.direct)
    {
      result = malloc (size + 1);
      if (result == NULL)
	return -1;
      memcpy (result, buf.direct, size);
    }
  else
    {
      result = realloc (buf.base.write_base, size + 1);
      if (result == NULL)
	{
	  free (buf.base.write_base);
	  return -1;
	}
    }

  /* Add NUL termination.  */
  result[size] = '\0';
  *result_ptr = result;

  return done;
}

int
__vasprintf (char **result_ptr, const char *format, va_list args)
{
  return __vasprintf_internal (result_ptr, format, args, 0);
}
ldbl_weak_alias (__vasprintf, vasprintf)