--- a/popt/popt.c +++ b/popt/popt.c @@ -1351,12 +1351,19 @@ int poptAddItem(poptContext con, poptItem newItem, int flags) const char * poptBadOption(poptContext con, unsigned int flags) { - struct optionStackEntry * os = NULL; + struct optionStackEntry * os; - if (con != NULL) - os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os; + if (!con) + return NULL; + + os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os; + if (!os) + return NULL; + + if (con->doExec && con->doExec->argv && !os->nextCharArg && os->next == os->argc) + return con->doExec->argv[0]; - return (os != NULL && os->argv != NULL ? os->argv[os->next - 1] : NULL); + return ((os->argv && (os->next > 0)) ? os->argv[os->next - 1] : NULL); } const char * poptStrerror(const int error)