diff --git a/popt/popt.c b/popt/popt.c index 0e05d62..1eb3c0a 100644 --- a/popt/popt.c +++ b/popt/popt.c @@ -163,7 +163,7 @@ static void invokeCallbacksOPTION(poptContext con, poptContext poptGetContext(const char * name, int argc, const char ** argv, const struct poptOption * options, unsigned int flags) { - poptContext con = malloc(sizeof(*con)); + poptContext con = xmalloc(sizeof(*con)); if (con == NULL) return NULL; /* XXX can't happen */ memset(con, 0, sizeof(*con)); @@ -178,7 +178,7 @@ poptContext poptGetContext(const char * name, int argc, const char ** argv, if (!(flags & POPT_CONTEXT_KEEP_FIRST)) con->os->next = 1; /* skip argv[0] */ - con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) ); + con->leftovers = xcalloc( (argc + 1), sizeof(*con->leftovers) ); /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */ con->options = options; /*@=dependenttrans =assignexpose@*/ @@ -188,7 +188,7 @@ poptContext poptGetContext(const char * name, int argc, const char ** argv, con->execs = NULL; con->numExecs = 0; con->finalArgvAlloced = argc * 2; - con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) ); + con->finalArgv = xcalloc( con->finalArgvAlloced, sizeof(*con->finalArgv) ); con->execAbsolute = 1; con->arg_strip = NULL; @@ -196,7 +196,7 @@ poptContext poptGetContext(const char * name, int argc, const char ** argv, con->flags |= POPT_CONTEXT_POSIXMEHARDER; if (name) { - char * t = malloc(strlen(name) + 1); + char * t = xmalloc(strlen(name) + 1); if (t) con->appName = strcpy(t, name); } @@ -288,13 +288,13 @@ static int handleExec(/*@special@*/ poptContext con, time 'round */ if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) { con->finalArgvAlloced += 10; - con->finalArgv = realloc(con->finalArgv, + con->finalArgv = xrealloc(con->finalArgv, sizeof(*con->finalArgv) * con->finalArgvAlloced); } i = con->finalArgvCount++; if (con->finalArgv != NULL) /* XXX can't happen */ - { char *s = malloc((longName ? strlen(longName) : 0) + 3); + { char *s = xmalloc((longName ? strlen(longName) : 0) + 3); if (s != NULL) { /* XXX can't happen */ if (longName) sprintf(s, "--%s", longName); @@ -378,7 +378,7 @@ static int execCommand(poptContext con) (!con->execAbsolute && strchr(item->argv[0], '/'))) return POPT_ERROR_NOARG; - argv = malloc(sizeof(*argv) * + argv = xmalloc(sizeof(*argv) * (6 + item->argc + con->numLeftovers + con->finalArgvCount)); if (argv == NULL) return POPT_ERROR_MALLOC; @@ -582,7 +582,7 @@ expandNextArg(/*@special@*/ poptContext con, const char * s) char c; if (!con) return NULL; - te = t = malloc(tn);; + te = t = xmalloc(tn); if (t == NULL) return NULL; /* XXX can't happen */ while ((c = *s++) != '\0') { switch (c) { @@ -604,7 +604,7 @@ expandNextArg(/*@special@*/ poptContext con, const char * s) alen = strlen(a); tn += alen; *te = '\0'; - t = realloc(t, tn); + t = xrealloc(t, tn); te = t + strlen(t); strncpy(te, a, alen); te += alen; continue; @@ -615,7 +615,7 @@ expandNextArg(/*@special@*/ poptContext con, const char * s) *te++ = c; } *te = '\0'; - t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */ + t = xrealloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */ return t; } @@ -999,12 +999,12 @@ int poptGetNextOpt(poptContext con) if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) { con->finalArgvAlloced += 10; - con->finalArgv = realloc(con->finalArgv, + con->finalArgv = xrealloc(con->finalArgv, sizeof(*con->finalArgv) * con->finalArgvAlloced); } if (con->finalArgv != NULL) - { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3); + { char *s = xmalloc((opt->longName ? strlen(opt->longName) : 0) + 3); if (s != NULL) { /* XXX can't happen */ if (opt->longName) sprintf(s, "%s%s", @@ -1156,7 +1156,7 @@ int poptAddItem(poptContext con, poptItem newItem, int flags) /*@notreached@*/ break; } - *items = realloc((*items), ((*nitems) + 1) * sizeof(**items)); + *items = xrealloc((*items), ((*nitems) + 1) * sizeof(**items)); if ((*items) == NULL) return 1; diff --git a/popt/popthelp.c b/popt/popthelp.c index 4555948..ab3b8cd 100644 --- a/popt/popthelp.c +++ b/popt/popthelp.c @@ -201,7 +201,7 @@ singleOptionDefaultValue(size_t lineLength, /*@*/ { const char * defstr = D_(translation_domain, "default"); - char * le = malloc(4*lineLength + 1); + char * le = xmalloc(4*lineLength + 1); char * l = le; if (le == NULL) return NULL; /* XXX can't happen */ @@ -287,8 +287,7 @@ static void singleOptionHelp(FILE * fp, columns_t columns, if (opt->longName) nb += strlen(opt->longName); if (argDescrip) nb += strlen(argDescrip); - left = malloc(nb); - if (left == NULL) return; /* XXX can't happen */ + left = xmalloc(nb); left[0] = '\0'; left[maxLeftCol] = '\0'; @@ -315,7 +314,7 @@ static void singleOptionHelp(FILE * fp, columns_t columns, if (opt->argInfo & POPT_ARGFLAG_SHOW_DEFAULT) { defs = singleOptionDefaultValue(lineLength, opt, translation_domain); if (defs) { - char * t = malloc((help ? strlen(help) : 0) + + char * t = xmalloc((help ? strlen(help) : 0) + strlen(defs) + sizeof(" ")); if (t) { char * te = t; diff --git a/popt/poptparse.c b/popt/poptparse.c index f577a02..e4ce616 100644 --- a/popt/poptparse.c +++ b/popt/poptparse.c @@ -26,7 +26,7 @@ int poptDupArgv(int argc, const char **argv, nb += strlen(argv[i]) + 1; } - dst = malloc(nb); + dst = xmalloc(nb); if (dst == NULL) /* XXX can't happen */ return POPT_ERROR_MALLOC; argv2 = (void *) dst; @@ -41,7 +41,7 @@ int poptDupArgv(int argc, const char **argv, if (argvPtr) { *argvPtr = argv2; } else { - free(argv2); + xfree(argv2); argv2 = NULL; } if (argcPtr) @@ -54,7 +54,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) const char * src; char quote = '\0'; int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA; - const char ** argv = malloc(sizeof(*argv) * argvAlloced); + const char ** argv = xmalloc(sizeof(*argv) * argvAlloced); int argc = 0; size_t buflen = strlen(s) + 1; char * buf, * bufOrig = NULL; @@ -86,7 +86,7 @@ int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr) buf++, argc++; if (argc == argvAlloced) { argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA; - argv = realloc(argv, sizeof(*argv) * argvAlloced); + argv = xrealloc(argv, sizeof(*argv) * argvAlloced); if (argv == NULL) goto exit; } argv[argc] = buf; diff --git a/popt/system.h b/popt/system.h index 68a5c2a..b32a0fc 100644 --- a/popt/system.h +++ b/popt/system.h @@ -1,3 +1,5 @@ +#ifndef __POPT_SYSTEM_H__ +#define __POPT_SYSTEM_H__ /** * \file popt/system.h */ @@ -71,11 +73,13 @@ char * xstrdup (const char *str) #define xcalloc(_nmemb, _size) (calloc((_nmemb), (_size)) ? : vmefail()) #define xrealloc(_ptr, _size) (realloc((_ptr), (_size)) ? : vmefail()) #define xstrdup(_str) (strcpy((malloc(strlen(_str)+1) ? : vmefail()), (_str))) +#define xfree(_ptr) do { if(_ptr) free(_ptr); else vmefail(); } while(0) #else #define xmalloc(_size) malloc(_size) #define xcalloc(_nmemb, _size) calloc((_nmemb), (_size)) #define xrealloc(_ptr, _size) realloc((_ptr), (_size)) #define xstrdup(_str) strdup(_str) +#define xfree(ptr) free(ptr) #endif /* defined(HAVE_MCHECK_H) && defined(__GNUC__) */ #if defined(HAVE___SECURE_GETENV) && !defined(__LCLINT__) @@ -83,3 +87,4 @@ char * xstrdup (const char *str) #endif #include "popt.h" +#endif /* __POPT_SYSTEM_H__ */