/src/gnupg/common/xasprintf.c
Line | Count | Source (jump to first uncovered line) |
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 | 0 | { |
46 | 0 | va_list ap; |
47 | 0 | char *buf; |
48 | |
|
49 | 0 | va_start (ap, fmt); |
50 | 0 | if (gpgrt_vasprintf (&buf, fmt, ap) < 0) |
51 | 0 | log_fatal ("estream_asprintf failed: %s\n", strerror (errno)); |
52 | 0 | va_end (ap); |
53 | 0 | return buf; |
54 | 0 | } |
55 | | |
56 | | /* Same as above but return NULL on memory failure. */ |
57 | | char * |
58 | | xtryasprintf (const char *fmt, ...) |
59 | 0 | { |
60 | 0 | int rc; |
61 | 0 | va_list ap; |
62 | 0 | char *buf; |
63 | |
|
64 | 0 | va_start (ap, fmt); |
65 | 0 | rc = gpgrt_vasprintf (&buf, fmt, ap); |
66 | 0 | va_end (ap); |
67 | 0 | if (rc < 0) |
68 | 0 | return NULL; |
69 | 0 | return buf; |
70 | 0 | } |