Coverage Report

Created: 2026-01-10 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gnupg/common/xasprintf.c
Line
Count
Source
1
/* xasprintf.c
2
 *  Copyright (C) 2003, 2005 Free Software Foundation, Inc.
3
 *
4
 * This file is part of GnuPG.
5
 *
6
 * This file is free software; you can redistribute it and/or modify
7
 * it under the terms of either
8
 *
9
 *   - the GNU Lesser General Public License as published by the Free
10
 *     Software Foundation; either version 3 of the License, or (at
11
 *     your option) any later version.
12
 *
13
 * or
14
 *
15
 *   - the GNU General Public License as published by the Free
16
 *     Software Foundation; either version 2 of the License, or (at
17
 *     your option) any later version.
18
 *
19
 * or both in parallel, as here.
20
 *
21
 * This file is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, see <https://www.gnu.org/licenses/>.
28
 */
29
30
#include <config.h>
31
#include <stdlib.h>
32
#include <errno.h>
33
34
#include "util.h"
35
36
/* Same as asprintf but return an allocated buffer suitable to be
37
   freed using xfree.  This function simply dies on memory failure,
38
   thus no extra check is required.
39
40
   FIXME: We should remove these functions in favor of gpgrt_bsprintf
41
   and a xgpgrt_bsprintf or rename them to xbsprintf and
42
   xtrybsprintf.  */
43
char *
44
xasprintf (const char *fmt, ...)
45
22.6k
{
46
22.6k
  va_list ap;
47
22.6k
  char *buf;
48
49
22.6k
  va_start (ap, fmt);
50
22.6k
  if (gpgrt_vasprintf (&buf, fmt, ap) < 0)
51
22.6k
    log_fatal ("estream_asprintf failed: %s\n", strerror (errno));
52
22.6k
  va_end (ap);
53
22.6k
  return buf;
54
22.6k
}
55
56
/* Same as above but return NULL on memory failure.  */
57
char *
58
xtryasprintf (const char *fmt, ...)
59
9.02k
{
60
9.02k
  int rc;
61
9.02k
  va_list ap;
62
9.02k
  char *buf;
63
64
9.02k
  va_start (ap, fmt);
65
9.02k
  rc = gpgrt_vasprintf (&buf, fmt, ap);
66
9.02k
  va_end (ap);
67
9.02k
  if (rc < 0)
68
0
    return NULL;
69
9.02k
  return buf;
70
9.02k
}