From: Miroslav Lichvar Date: Thu, 10 Nov 2011 15:04:45 +0000 (+0100) Subject: fix returning strings in whiptail and whiptcl (#752818) X-Git-Tag: r0-52-14~3 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f40f18ae2a37e42a795b85e35bce9018dec90c53;p=thirdparty%2Fnewt.git fix returning strings in whiptail and whiptcl (#752818) --- diff --git a/dialogboxes.c b/dialogboxes.c index 4850e55..c518f16 100644 --- a/dialogboxes.c +++ b/dialogboxes.c @@ -192,7 +192,7 @@ int gauge(const char * text, int height, int width, poptContext optCon, int fd, } int inputBox(const char * text, int height, int width, poptContext optCon, - int flags, const char ** result) { + int flags, char ** result) { newtComponent form, entry, okay, cancel, answer, tb; const char * val; int pFlag = (flags & FLAG_PASSWORD) ? NEWT_FLAG_PASSWORD : 0; @@ -212,12 +212,13 @@ int inputBox(const char * text, int height, int width, poptContext optCon, addButtons(height, width, form, &okay, &cancel, flags); answer = newtRunForm(form); + *result = NULL; if (answer == cancel) rc = DLG_CANCEL; else if (answer == NULL) rc = DLG_ESCAPE; - - *result = val; + else + *result = strdup(val); newtFormDestroy(form); @@ -254,7 +255,7 @@ static int mystrncpyw(char *dest, const char *src, int n, int *maxwidth) } int listBox(const char * text, int height, int width, poptContext optCon, - int flags, const char *default_item, const char ** result) { + int flags, const char *default_item, char ** result) { newtComponent form, okay, tb, answer, listBox; newtComponent cancel = NULL; const char * arg; @@ -372,13 +373,15 @@ int listBox(const char * text, int height, int width, poptContext optCon, addButtons(height, width, form, &okay, &cancel, flags); answer = newtRunForm(form); + *result = NULL; if (answer == cancel) rc = DLG_CANCEL; if (answer == NULL) rc = DLG_ESCAPE; - - i = (long) newtListboxGetCurrent(listBox); - *result = itemInfo[i].tag; + else { + i = (long) newtListboxGetCurrent(listBox); + *result = strdup(itemInfo[i].tag); + } newtFormDestroy(form); free(itemInfo); @@ -387,7 +390,7 @@ int listBox(const char * text, int height, int width, poptContext optCon, } int checkList(const char * text, int height, int width, poptContext optCon, - int useRadio, int flags, const char *** selections) { + int useRadio, int flags, char *** selections) { newtComponent form, okay, tb, subform, answer; newtComponent sb = NULL, cancel = NULL; const char * arg; @@ -481,38 +484,41 @@ int checkList(const char * text, int height, int width, poptContext optCon, addButtons(height, width, form, &okay, &cancel, flags); answer = newtRunForm(form); + *selections = NULL; if (answer == cancel) rc = DLG_CANCEL; if (answer == NULL) rc = DLG_ESCAPE; - - if (useRadio) { - answer = newtRadioGetCurrent(cbInfo[0].comp); - *selections = malloc(sizeof(char *) * 2); - if (*selections == NULL) - return DLG_ERROR; - (*selections)[0] = (*selections)[1] = NULL; - for (i = 0; i < numBoxes; i++) - if (cbInfo[i].comp == answer) { - (*selections)[0] = cbInfo[i].tag; - break; + else { + if (useRadio) { + answer = newtRadioGetCurrent(cbInfo[0].comp); + *selections = malloc(sizeof(char *) * 2); + if (*selections == NULL) + return DLG_ERROR; + (*selections)[0] = (*selections)[1] = NULL; + for (i = 0; i < numBoxes; i++) + if (cbInfo[i].comp == answer) { + (*selections)[0] = strdup(cbInfo[i].tag); + break; + } + } else { + numSelected = 0; + for (i = 0; i < numBoxes; i++) { + if (cbStates[i] != ' ') numSelected++; } - } else { - numSelected = 0; - for (i = 0; i < numBoxes; i++) { - if (cbStates[i] != ' ') numSelected++; - } - *selections = malloc(sizeof(char *) * (numSelected + 1)); - if (*selections == NULL) return DLG_ERROR; + *selections = malloc(sizeof(char *) * (numSelected + 1)); + if (*selections == NULL) + return DLG_ERROR; - numSelected = 0; - for (i = 0; i < numBoxes; i++) { - if (cbStates[i] != ' ') - (*selections)[numSelected++] = cbInfo[i].tag; - } + numSelected = 0; + for (i = 0; i < numBoxes; i++) { + if (cbStates[i] != ' ') + (*selections)[numSelected++] = strdup(cbInfo[i].tag); + } - (*selections)[numSelected] = NULL; + (*selections)[numSelected] = NULL; + } } newtFormDestroy(form); diff --git a/dialogboxes.h b/dialogboxes.h index 6d133e1..ca51090 100644 --- a/dialogboxes.h +++ b/dialogboxes.h @@ -29,11 +29,11 @@ int max(int a, int b); int messageBox(const char * text, int height, int width, int type, int flags); int checkList(const char * text, int height, int width, poptContext optCon, - int useRadio, int flags, const char *** selections); + int useRadio, int flags, char *** selections); int listBox(const char * text, int height, int width, poptContext optCon, - int flags, const char *default_item, const char ** result); + int flags, const char *default_item, char ** result); int inputBox(const char * text, int height, int width, poptContext optCon, - int flags, const char ** result); + int flags, char ** result); int gauge(const char * text, int height, int width, poptContext optCon, int fd, int flags); void useFullButtons(int state); diff --git a/whiptail.c b/whiptail.c index 7aa811a..a631bc4 100644 --- a/whiptail.c +++ b/whiptail.c @@ -344,8 +344,8 @@ int main(int argc, const char ** argv) { int outputfd = 2; int topLeft = 0; FILE *output = stderr; - const char * result; - const char ** selections, ** next; + char * result; + char ** selections, ** next; char * title = NULL; char *default_item = NULL; char * backtitle = NULL; @@ -556,25 +556,34 @@ int main(int argc, const char ** argv) { case MODE_INPUTBOX: rc = inputBox(text, height, width, optCon, flags, &result); - if (rc == DLG_OKAY) fprintf(output, "%s", result); + if (rc == DLG_OKAY) { + fprintf(output, "%s", result); + free(result); + } break; case MODE_PASSWORDBOX: rc = inputBox(text, height, width, optCon, flags | FLAG_PASSWORD, &result); - if (rc == DLG_OKAY) fprintf (output, "%s", result); + if (rc == DLG_OKAY) { + fprintf (output, "%s", result); + free(result); + } break; case MODE_MENU: rc = listBox(text, height, width, optCon, flags, default_item, &result); - if (rc == DLG_OKAY) fprintf(output, "%s", result); + if (rc == DLG_OKAY) { + fprintf(output, "%s", result); + free(result); + } break; case MODE_RADIOLIST: rc = checkList(text, height, width, optCon, 1, flags, &selections); - if (rc == DLG_OKAY) { - if (selections[0]) - fprintf(output, "%s", selections[0]); + if (rc == DLG_OKAY && selections[0]) { + fprintf(output, "%s", selections[0]); + free(selections[0]); free(selections); } break; @@ -591,6 +600,7 @@ int main(int argc, const char ** argv) { } else { fprintf(output, "%s\n", *next); } + free(*next); } free(selections); diff --git a/whiptcl.c b/whiptcl.c index 82c12ab..8688780 100644 --- a/whiptcl.c +++ b/whiptcl.c @@ -71,8 +71,8 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc, int rc = 0; int flags = 0; int defaultNo = 0; - const char * result; - const char ** selections, ** next; + char * result; + char ** selections, ** next; char * title = NULL; char *default_item = NULL; struct poptOption optionsTable[] = { @@ -205,7 +205,7 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc, case MODE_INPUTBOX: rc = inputBox(text, height, width, optCon, flags, &result); if (rc ==DLG_OKAY) { - interp->result = strdup(result); + interp->result = result; interp->freeProc = TCL_DYNAMIC; } break; @@ -213,7 +213,7 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc, case MODE_MENU: rc = listBox(text, height, width, optCon, flags, default_item, &result); if (rc==DLG_OKAY) { - interp->result = strdup(result); + interp->result = result; interp->freeProc = TCL_DYNAMIC; } break; @@ -221,8 +221,10 @@ static int wtCmd(ClientData clientData, Tcl_Interp * interp, int argc, case MODE_RADIOLIST: rc = checkList(text, height, width, optCon, 1, flags, &selections); if (rc==DLG_OKAY) { - interp->result = strdup(selections[0]); + interp->result = selections[0]; interp->freeProc = TCL_DYNAMIC; + + free(selections); } break;