add easprintf() and evasprintf(), error checking versions of asprintf() and vasprintf()

This commit is contained in:
Todd C. Miller
1999-07-22 16:22:47 +00:00
parent c8962786f4
commit fd410d4bcb
2 changed files with 55 additions and 1 deletions

54
alloc.c
View File

@@ -40,8 +40,10 @@
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
#include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */
#include <sys/param.h>
#include <sys/types.h>
#include "compat.h"
#include "sudo.h"
#ifndef STDC_HEADERS
#if !defined(__GNUC__) && !defined(HAVE_MALLOC_H)
@@ -107,3 +109,53 @@ estrdup(src)
}
return(dst);
}
/*
* easprintf() calls vasprintf() and exits with an error if vasprintf()
* returns -1 (out of memory).
*/
void
#ifdef __STDC__
easprintf(char **ret, const char *fmt, ...)
#else
easprintf(va_alist)
va_dcl
#endif
{
int len;
va_list ap;
#ifdef __STDC__
va_start(ap, fmt);
#else
char **ret;
const char *fmt;
va_start(ap);
ret = va_arg(ap, char **);
fmt = va_arg(ap, const char *);
#endif
len = vasprintf(ret, fmt, ap);
va_end(ap);
if (len == -1) {
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
}
/*
* evasprintf() calls vasprintf() and exits with an error if vasprintf()
* returns -1 (out of memory).
*/
void
evasprintf(ret, format, args)
char **ret;
const char *format;
va_list args;
{
if (vasprintf(ret, format, args) == -1) {
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
}

2
sudo.h
View File

@@ -154,6 +154,8 @@ void pass_warn __P((FILE *));
VOID *emalloc __P((size_t));
VOID *erealloc __P((VOID *, size_t));
char *estrdup __P((const char *));
void easprintf __P((char **, const char *, ...));
void evasprintf __P((char **, const char *, va_list));
YY_DECL;
/* Only provide extern declarations outside of sudo.c. */