--- a/popt/popt.c +++ b/popt/popt.c @@ -60,6 +60,7 @@ static void prtcon(const char *msg, poptContext con) void poptSetExecPath(poptContext con, const char * path, int allowAbsolute) { + if (con == NULL) return; con->execPath = _free(con->execPath); con->execPath = xstrdup(path); con->execAbsolute = allowAbsolute; @@ -70,7 +71,7 @@ static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt) /*@globals internalState@*/ /*@modifies internalState@*/ { - if (opt != NULL) + if (con == NULL || opt == NULL) return; for (; opt->longName || opt->shortName || opt->arg; opt++) { poptArg arg = { .ptr = opt->arg }; if (arg.ptr) @@ -94,7 +95,7 @@ static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt) /*@globals internalState@*/ /*@modifies internalState@*/ { - if (opt != NULL) + if (con == NULL || opt == NULL) return; for (; opt->longName || opt->shortName || opt->arg; opt++) { poptArg arg = { .ptr = opt->arg }; if (arg.ptr) @@ -124,7 +125,7 @@ static void invokeCallbacksOPTION(poptContext con, const struct poptOption * cbopt = NULL; poptArg cbarg = { .ptr = NULL }; - if (opt != NULL) + if (con == NULL || opt == NULL) return; for (; opt->longName || opt->shortName || opt->arg; opt++) { poptArg arg = { .ptr = opt->arg }; switch (poptArgType(opt)) { @@ -260,7 +261,7 @@ static int handleExec(/*@special@*/ poptContext con, poptItem item; int i; - if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */ + if (con == NULL || con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */ return 0; for (i = con->numExecs - 1; i >= 0; i--) { @@ -319,7 +320,7 @@ static int handleAlias(/*@special@*/ poptContext con, con->os->currAlias, con->os->currAlias->option.longName @*/ /*@modifies con @*/ { - poptItem item = con->os->currAlias; + poptItem item = (con && con->os) ? con->os->currAlias : NULL; int rc; int i; @@ -333,7 +334,7 @@ static int handleAlias(/*@special@*/ poptContext con, return 0; } - if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */ + if (con == NULL || con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */ return 0; for (i = con->numAliases - 1; i >= 0; i--) { @@ -442,7 +443,7 @@ static int execCommand(poptContext con) /*@globals internalState @*/ /*@modifies internalState @*/ { - poptItem item = con->doExec; + poptItem item = con ? con->doExec : NULL; poptArgv argv = NULL; int argc = 0; int rc; @@ -608,9 +609,11 @@ static const char * findNextArg(/*@special@*/ poptContext con, con->os->next, con->os->argb, con->os->argc, con->os->argv @*/ /*@modifies con @*/ { - struct optionStackEntry * os = con->os; + struct optionStackEntry * os = con ? con->os : NULL; const char * arg; + if (os == NULL || con->optionStack == NULL) return NULL; + do { int i; arg = NULL; @@ -650,6 +653,8 @@ expandNextArg(/*@special@*/ poptContext con, const char * s) size_t tn = strlen(s) + 1; char c; + if (con == NULL) return NULL; + te = t = malloc(tn); if (t == NULL) return NULL; /* XXX can't happen */ *t = '\0'; @@ -1283,6 +1288,9 @@ int poptAddAlias(poptContext con, struct poptAlias alias, { struct poptItem_s item_buf; poptItem item = &item_buf; + + if (con == NULL) return 1; + memset(item, 0, sizeof(*item)); item->option.longName = alias.longName; item->option.shortName = alias.shortName; @@ -1301,6 +1309,8 @@ int poptAddItem(poptContext con, poptItem newItem, int flags) poptItem * items, item; int * nitems; + if (con == NULL) return 1; + switch (flags) { case 1: items = &con->execs; @@ -1372,6 +1382,8 @@ const char * poptStrerror(const int error) return POPT_("memory allocation failed"); case POPT_ERROR_ERRNO: return strerror(errno); + case POPT_ERROR_NOCONTEXT: + return POPT_("no context"); default: return POPT_("unknown error"); } @@ -1382,6 +1394,9 @@ int poptStuffArgs(poptContext con, const char ** argv) int argc; int rc; + if (con == NULL) + return POPT_ERROR_NOCONTEXT; + if ((con->os - con->optionStack) == POPT_OPTION_DEPTH) return POPT_ERROR_OPTSTOODEEP; @@ -1402,7 +1417,7 @@ int poptStuffArgs(poptContext con, const char ** argv) const char * poptGetInvocationName(poptContext con) { - return (con->os->argv ? con->os->argv[0] : ""); + return ((con && con->os->argv) ? con->os->argv[0] : ""); } int poptStrippedArgv(poptContext con, int argc, char ** argv) @@ -1412,14 +1427,14 @@ int poptStrippedArgv(poptContext con, int argc, char ** argv) int i; /*@-sizeoftype@*/ - if (con->arg_strip) + if (con && con->arg_strip) for (i = 1; i < argc; i++) { if (PBM_ISSET(i, con->arg_strip)) numargs--; } for (i = 1; i < argc; i++) { - if (con->arg_strip && PBM_ISSET(i, con->arg_strip)) + if (con && con->arg_strip && PBM_ISSET(i, con->arg_strip)) continue; argv[j] = (j < numargs) ? argv[i] : NULL; j++; --- a/popt/popt.h +++ b/popt/popt.h @@ -95,6 +95,7 @@ #define POPT_ERROR_BADOPERATION -19 /*!< mutually exclusive logical operations requested */ #define POPT_ERROR_NULLARG -20 /*!< opt->arg should not be NULL */ #define POPT_ERROR_MALLOC -21 /*!< memory allocation failed */ +#define POPT_ERROR_NOCONTEXT -22 /*!< no context */ /*@}*/ /** \ingroup popt --- a/popt/poptconfig.c +++ b/popt/poptconfig.c @@ -40,7 +40,7 @@ static void configLine(poptContext con, char * line) poptItem item = &item_buf; int i, j; - if (con->appName == NULL) + if (con == NULL || con->appName == NULL) return; nameLength = strlen(con->appName); @@ -119,6 +119,8 @@ int poptReadConfigFile(poptContext con, const char * fn) int fd, rc; off_t fileLength; + if (con == NULL) return POPT_ERROR_NOCONTEXT; + fd = open(fn, O_RDONLY); if (fd < 0) return (errno == ENOENT ? 0 : POPT_ERROR_ERRNO); @@ -190,7 +192,7 @@ int poptReadDefaultConfig(poptContext con, /*@unused@*/ UNUSED(int useEnv)) struct stat s; int rc; - if (con->appName == NULL) return 0; + if (con == NULL || con->appName == NULL) return 0; if (strcmp(_popt_sysconfdir, _popt_etc)) { rc = poptReadConfigFile(con, _popt_sysconfdir); --- a/popt/popthelp.c +++ b/popt/popthelp.c @@ -588,6 +588,8 @@ static void singleTableHelp(poptContext con, FILE * fp, const char *sub_transdom; int xx; + if (con == NULL) return; + if (table == poptAliasOptions) { itemHelp(fp, con->aliases, con->numAliases, columns, NULL); itemHelp(fp, con->execs, con->numExecs, columns, NULL); @@ -633,7 +635,7 @@ static size_t showHelpIntro(poptContext con, FILE * fp) xx = POPT_fprintf(fp, POPT_("Usage:")); if (!(con->flags & POPT_CONTEXT_KEEP_FIRST)) { /*@-type@*/ /* LCL: wazzup? */ - fn = con->optionStack->argv[0]; + fn = con->optionStack ? con->optionStack->argv[0] : NULL; /*@=type@*/ if (fn == NULL) return len; if (strchr(fn, '/')) fn = strrchr(fn, '/') + 1; @@ -650,6 +652,8 @@ void poptPrintHelp(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags)) columns_t columns = calloc((size_t)1, sizeof(*columns)); int xx; + if (con == NULL) return; + (void) showHelpIntro(con, fp); if (con->otherHelp) xx = POPT_fprintf(fp, " %s\n", con->otherHelp); @@ -787,7 +791,7 @@ static size_t singleTableUsage(poptContext con, FILE * fp, columns_t columns, /*@globals fileSystem @*/ /*@modifies fp, columns->cur, done, fileSystem @*/ { - if (opt != NULL) + if (con != NULL && opt != NULL) for (; (opt->longName || opt->shortName || opt->arg) ; opt++) { if (poptArgType(opt) == POPT_ARG_INTL_DOMAIN) { translation_domain = (const char *)opt->arg; @@ -872,6 +876,8 @@ void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags)) struct poptDone_s done_buf; poptDone done = &done_buf; + if (con == NULL) return; + memset(done, 0, sizeof(*done)); done->nopts = 0; done->maxopts = 64; @@ -905,6 +911,8 @@ void poptPrintUsage(poptContext con, FILE * fp, /*@unused@*/ UNUSED(int flags)) void poptSetOtherOptionHelp(poptContext con, const char * text) { + if (con == NULL) return; + con->otherHelp = _free(con->otherHelp); con->otherHelp = xstrdup(text); }