00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030 #include <stdarg.h>
00031 #include <stdlib.h>
00032 #include <string.h>
00033 #include <errno.h>
00034
00035 #include "safe.h"
00036 #include "debug.h"
00037 #include <syslog.h>
00038
00039 void * safe_malloc (size_t size) {
00040 void * retval = NULL;
00041 retval = malloc(size);
00042 if (!retval) {
00043 debug(LOG_CRIT, "Failed to malloc %d bytes of memory: %s. Bailing out", size, strerror(errno));
00044 exit(1);
00045 }
00046 return (retval);
00047 }
00048
00049 char * safe_strdup(const char *s) {
00050 char * retval = NULL;
00051 if (!s) {
00052 debug(LOG_CRIT, "safe_strdup called with NULL which would have crashed strdup. Bailing out");
00053 exit(1);
00054 }
00055 retval = strdup(s);
00056 if (!retval) {
00057 debug(LOG_CRIT, "Failed to duplicate a string: %s. Bailing out", strerror(errno));
00058 exit(1);
00059 }
00060 return (retval);
00061 }
00062
00063 int safe_asprintf(char **strp, const char *fmt, ...) {
00064 va_list ap;
00065 int retval;
00066
00067 va_start(ap, fmt);
00068 retval = safe_vasprintf(strp, fmt, ap);
00069 va_end(ap);
00070
00071 return (retval);
00072 }
00073
00074 int safe_vasprintf(char **strp, const char *fmt, va_list ap) {
00075 int retval;
00076
00077 retval = vasprintf(strp, fmt, ap);
00078
00079 if (retval == -1) {
00080 debug(LOG_CRIT, "Failed to vasprintf: %s. Bailing out", strerror(errno));
00081 exit (1);
00082 }
00083 return (retval);
00084 }