Skip to content
Snippets Groups Projects
Commit 8be0bfa1 authored by Arnaud Rebillout's avatar Arnaud Rebillout
Browse files

Fix regression in text_handler_multiselect in the text frontend

In commit 299ecb85, we started to use
readline to prompt the user, and as a result, the `answer` variable is
now dynamically allocated (before, it used to be a 4096 chars array on
the stack).

This broke two things.

First, the use of `sizeof(answer)` is not valid anymore, as it just
returns the size of a pointer, not the size of the memory area.

Second, even if we fix the above to get the size right, we have another
problem. There's no guarantee that `answer` is big enough to accomodate
the final answer. If we take the example of the d-i tasksel screen, the
answer from user is likely to be something like `1 7 12`, but then it's
converted to values, something like `Debian desktop environment, ...
MATE, standard system utilities`, which is much longer. So if we want to
reuse `answer` to hold the final answer, we'll need to reallocate it.

Therefore, with this commit, we do just that. We first run a loop to
compute the size needed for the answer, then we free/alloc answer for
this size, and we can finally use it.

Closes: #1025133
parent 63286e5c
No related branches found
No related tags found
1 merge request!18Fix regression in text_handler_multiselect in the text frontend
......@@ -620,6 +620,7 @@ static int text_handler_multiselect(struct frontend *obj, unsigned printed, stru
char **defaults;
char *defval, *cp;
char *answer = NULL;
size_t answer_len;
int i, j, dcount, choice;
int ret = DC_OK;
unsigned start = 0;
......@@ -713,14 +714,24 @@ static int text_handler_multiselect(struct frontend *obj, unsigned printed, stru
}
}
answer[0] = 0;
answer_len = 0;
for (i = 0; i < choices->count; i++)
{
if (choices->selected[i])
answer_len += strlen(choices->choices[i]) + 2;
}
free(answer);
answer = malloc(answer_len);
if (answer_len > 0)
answer[0] = 0;
for (i = 0; i < choices->count; i++)
{
if (choices->selected[i])
{
if (answer[0] != 0)
strvacat(answer, sizeof(answer), ", ", NULL);
strvacat(answer, sizeof(answer), choices->choices[i], NULL);
strvacat(answer, answer_len, ", ", NULL);
strvacat(answer, answer_len, choices->choices[i], NULL);
}
}
question_setvalue(q, answer);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment