2004-01-30 12:37:09 +00:00
|
|
|
/* xasprintf.c
|
2005-02-25 16:14:55 +00:00
|
|
|
* Copyright (C) 2003, 2005 Free Software Foundation, Inc.
|
2021-03-04 16:52:03 +01:00
|
|
|
* Copyright (C) 2020 g10 Code GmbH
|
2004-01-30 12:37:09 +00:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* This file is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of either
|
2004-01-30 12:37:09 +00:00
|
|
|
*
|
Change license for some files in common to LGPLv3+/GPLv2+.
Having the LGPL on the common GnuPG code helps to share code
between GnuPG and related projects (like GPGME and Libassuan). This
is good for interoperability and to reduces bugs.
* common/asshelp.c, common/asshelp.h, common/asshelp2.c, common/b64dec.c
* common/b64enc.c, common/convert.c, common/dns-cert.c
* common/dns-cert.h common/exechelp-posix.c, common/exechelp-w32.c
* common/exechelp-w32ce.c, common/exechelp.h, common/get-passphrase.c
* common/get-passphrase.h, common/gettime.c, common/gpgrlhelp.c
* common/helpfile.c, common/homedir.c, common/http.c, common/http.h
* common/i18n.c, common/init.c, common/init.h, common/iobuf.c
* common/iobuf.h, common/localename.c, common/membuf.c, common/membuf.h
* common/miscellaneous.c, common/openpgp-oid.c, common/openpgpdefs.h
* common/percent.c, common/pka.c, common/pka.h, common/session-env.c
* common/session-env.h, common/sexp-parse.h, common/sexputil.c
* common/signal.c, common/srv.c, common/srv.h, common/ssh-utils.c
* common/ssh-utils.h, common/sysutils.c, common/sysutils.h
* common/tlv.c, common/tlv.h, common/ttyio.c, common/ttyio.h
* common/userids.c, common/userids.h, common/xasprintf.c: Change
license to LGPLv3+/GPLv2+/
2012-04-20 15:43:06 +02:00
|
|
|
* - the GNU Lesser General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or
|
|
|
|
*
|
|
|
|
* - the GNU General Public License as published by the Free
|
|
|
|
* Software Foundation; either version 2 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* or both in parallel, as here.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful,
|
2004-01-30 12:37:09 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2016-11-05 12:02:19 +01:00
|
|
|
* along with this program; if not, see <https://www.gnu.org/licenses/>.
|
2004-01-30 12:37:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
/* Same as asprintf but return an allocated buffer suitable to be
|
|
|
|
freed using xfree. This function simply dies on memory failure,
|
2014-08-26 17:47:22 +02:00
|
|
|
thus no extra check is required.
|
|
|
|
|
|
|
|
FIXME: We should remove these functions in favor of gpgrt_bsprintf
|
|
|
|
and a xgpgrt_bsprintf or rename them to xbsprintf and
|
|
|
|
xtrybsprintf. */
|
2004-01-30 12:37:09 +00:00
|
|
|
char *
|
|
|
|
xasprintf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
2007-05-15 16:10:48 +00:00
|
|
|
char *buf;
|
2004-01-30 12:37:09 +00:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2014-08-26 17:47:22 +02:00
|
|
|
if (gpgrt_vasprintf (&buf, fmt, ap) < 0)
|
2007-05-15 16:10:48 +00:00
|
|
|
log_fatal ("estream_asprintf failed: %s\n", strerror (errno));
|
2004-01-30 12:37:09 +00:00
|
|
|
va_end (ap);
|
2007-05-15 16:10:48 +00:00
|
|
|
return buf;
|
2004-01-30 12:37:09 +00:00
|
|
|
}
|
2005-02-25 16:14:55 +00:00
|
|
|
|
* configure.ac (gl_INIT): Add gnulib stuff.
(fseeko, ftello, ttyname, isascii): Replaced the AC_REPLACE_FUNCS
by a simple check.
(putc_unlocked): Removed check. Not used.
(strsep, mkdtemp, asprintf): Replaced checks by gnulib checks.
(xsize): Added will probably come handy soon.
(CFLAGS): Use -Wformat-security instead of
-Wformat-nonliteral. Add --Wno-format-y2k.
* gl/, gl/m4/: New.
* gpg-agent.c: Include setenv.h.
* Makefile.am (AM_CPPFLAGS): Added.
* util.h: Add some includes for gnulib.
(ttyname, isascii): Define them inline.
* fseeko.c, ftello.c: Removed.
* strsep.c, mkdtemp.c: Removed.
* ttyname.c, isascii.c: Removed.
* mkdtemp.c: Removed.
* exec.c: Include mkdtemp.h
* keybox-file.c (ftello) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/ftello.c.
* keybox-update.c (fseeko) [!HAVE_FSEEKO]: New replacement
function. Copied from ../common/iobuf.c.
* scdaemon.c: Include mkdtemp.h.
* misc.c: Include setenv.h.
* symcryptrun.c: Include mkdtemp.h.
2005-06-01 15:46:01 +00:00
|
|
|
/* Same as above but return NULL on memory failure. */
|
2005-02-25 16:14:55 +00:00
|
|
|
char *
|
|
|
|
xtryasprintf (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
va_list ap;
|
2007-05-15 16:10:48 +00:00
|
|
|
char *buf;
|
2005-02-25 16:14:55 +00:00
|
|
|
|
|
|
|
va_start (ap, fmt);
|
2014-08-26 17:47:22 +02:00
|
|
|
rc = gpgrt_vasprintf (&buf, fmt, ap);
|
2005-02-25 16:14:55 +00:00
|
|
|
va_end (ap);
|
|
|
|
if (rc < 0)
|
|
|
|
return NULL;
|
2007-05-15 16:10:48 +00:00
|
|
|
return buf;
|
2005-02-25 16:14:55 +00:00
|
|
|
}
|
2021-03-04 16:52:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* This is safe version of realloc useful for reallocing a calloced
|
|
|
|
* array. There are two ways to call it: The first example
|
|
|
|
* reallocates the array A to N elements each of SIZE but does not
|
|
|
|
* clear the newly allocated elements:
|
|
|
|
*
|
|
|
|
* p = xtryreallocarray (a, n, n, nsize);
|
|
|
|
*
|
|
|
|
* Note that when NOLD is larger than N no cleaning is needed anyway.
|
|
|
|
* The second example reallocates an array of size NOLD to N elements
|
|
|
|
* each of SIZE but clear the newly allocated elements:
|
|
|
|
*
|
|
|
|
* p = xtryreallocarray (a, nold, n, nsize);
|
|
|
|
*
|
|
|
|
* Note that xtryreallocarray (NULL, 0, n, nsize) is equivalent to
|
|
|
|
* xtrycalloc (n, nsize).
|
|
|
|
*
|
|
|
|
* The same function under the name gpgrt_reallocarray exists in
|
|
|
|
* libgpg-error but only since version 1.38 and thus we use a copy
|
|
|
|
* here.
|
|
|
|
*/
|
|
|
|
void *
|
|
|
|
xtryreallocarray (void *a, size_t oldnmemb, size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
size_t oldbytes, bytes;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
bytes = nmemb * size; /* size_t is unsigned so the behavior on overflow
|
|
|
|
* is defined. */
|
|
|
|
if (size && bytes / size != nmemb)
|
|
|
|
{
|
|
|
|
gpg_err_set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = xtryrealloc (a, bytes);
|
|
|
|
if (p && oldnmemb < nmemb)
|
|
|
|
{
|
|
|
|
/* OLDNMEMBS is lower than NMEMB thus the user asked for a
|
|
|
|
calloc. Clear all newly allocated members. */
|
|
|
|
oldbytes = oldnmemb * size;
|
|
|
|
if (size && oldbytes / size != oldnmemb)
|
|
|
|
{
|
|
|
|
xfree (p);
|
|
|
|
gpg_err_set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset (p + oldbytes, 0, bytes - oldbytes);
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|