Coverage Report

Created: 2026-03-12 07:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gettext/gettext-tools/libgettextpo/xvasprintf.c
Line
Count
Source
1
/* vasprintf and asprintf with out-of-memory checking.
2
   Copyright (C) 1999, 2002-2004, 2006-2026 Free Software Foundation, Inc.
3
4
   This program is free software: you can redistribute it and/or modify
5
   it under the terms of the GNU General Public License as published by
6
   the Free Software Foundation, either version 3 of the License, or
7
   (at your option) any later version.
8
9
   This program 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
12
   GNU General Public License for more details.
13
14
   You should have received a copy of the GNU General Public License
15
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16
17
#include <config.h>
18
19
/* Specification.  */
20
#include "xvasprintf.h"
21
22
#include <errno.h>
23
#include <limits.h>
24
#include <stdio.h>
25
#include <stdlib.h>
26
#include <string.h>
27
28
#include "xalloc.h"
29
30
/* Checked size_t computations.  */
31
#include "xsize.h"
32
33
static char *
34
xstrcat (size_t argcount, va_list args)
35
0
{
36
  /* Determine the total size.  */
37
0
  size_t totalsize = 0;
38
0
  {
39
0
    va_list ap;
40
0
    va_copy (ap, args);
41
0
    for (size_t i = argcount; i > 0; i--)
42
0
      {
43
0
        const char *next = va_arg (ap, const char *);
44
0
        totalsize = xsum (totalsize, strlen (next));
45
0
      }
46
0
    va_end (ap);
47
0
  }
48
49
  /* Test for overflow in the summing pass above or in (totalsize + 1)
50
     below.  */
51
0
  if (totalsize == SIZE_MAX)
52
0
    xalloc_die ();
53
54
  /* Allocate and fill the result string.  */
55
0
  char *result = XNMALLOC (totalsize + 1, char);
56
0
  {
57
0
    char *p = result;
58
0
    for (size_t i = argcount; i > 0; i--)
59
0
      {
60
0
        const char *next = va_arg (args, const char *);
61
0
        size_t len = strlen (next);
62
0
        memcpy (p, next, len);
63
0
        p += len;
64
0
      }
65
0
    *p = '\0';
66
0
  }
67
68
0
  return result;
69
0
}
70
71
char *
72
xvasprintf (const char *format, va_list args)
73
9.16k
{
74
75
  /* Recognize the special case format = "%s...%s".  It is a frequently used
76
     idiom for string concatenation and needs to be fast.  We don't want to
77
     have a separate function xstrcat() for this purpose.  */
78
9.16k
  {
79
9.16k
    size_t argcount = 0;
80
81
9.16k
    for (const char *f = format;;)
82
9.16k
      {
83
9.16k
        if (*f == '\0')
84
          /* Recognized the special case of string concatenation.  */
85
0
          return xstrcat (argcount, args);
86
9.16k
        if (*f != '%')
87
9.16k
          break;
88
0
        f++;
89
0
        if (*f != 's')
90
0
          break;
91
0
        f++;
92
0
        argcount++;
93
0
      }
94
9.16k
  }
95
96
9.16k
  char *result;
97
9.16k
  if (vaszprintf (&result, format, args) < 0)
98
0
    {
99
0
      if (errno == ENOMEM)
100
0
        xalloc_die ();
101
0
      else
102
0
        {
103
          /* The programmer ought to have ensured that none of the other errors
104
             can occur.  */
105
0
          int err = errno;
106
0
          char errbuf[20];
107
0
          const char *errname;
108
#if HAVE_WORKING_STRERRORNAME_NP
109
          errname = strerrorname_np (err);
110
          if (errname == NULL)
111
#else
112
0
          if (err == EINVAL)
113
0
            errname = "EINVAL";
114
0
          else if (err == EILSEQ)
115
0
            errname = "EILSEQ";
116
0
          else if (err == EOVERFLOW)
117
0
            errname = "EOVERFLOW";
118
0
          else
119
0
#endif
120
0
            {
121
0
              sprintf (errbuf, "%d", err);
122
0
              errname = errbuf;
123
0
            }
124
0
          fprintf (stderr, "vasprintf failed! format=\"%s\", errno=%s\n",
125
0
                   format, errname);
126
0
          fflush (stderr);
127
0
          abort ();
128
0
        }
129
0
    }
130
131
9.16k
  return result;
132
9.16k
}