]> granicus.if.org Git - sudo/commitdiff
add easprintf() and evasprintf(), error checking versions of asprintf() and vasprintf()
authorTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 22 Jul 1999 16:22:47 +0000 (16:22 +0000)
committerTodd C. Miller <Todd.Miller@courtesan.com>
Thu, 22 Jul 1999 16:22:47 +0000 (16:22 +0000)
alloc.c
sudo.h

diff --git a/alloc.c b/alloc.c
index 9e98f55e61bfc29b46a555276a951771a1cd1d1f..e3dc1af62217fe5369617e8ee801112414a5428a 100644 (file)
--- a/alloc.c
+++ b/alloc.c
 #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);
+    }
+}
diff --git a/sudo.h b/sudo.h
index 0d43313d9f514363a3313f9168c067766a9fa012..68c3e44ef2131cbc16ab18374db4726bdf266d80 100644 (file)
--- a/sudo.h
+++ b/sudo.h
@@ -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. */