Reordered prompt done callback to avoid accessing out of bound history#3318
Conversation
More precisely, what happens is:
i.History[ptype] = append(i.History[ptype], "")
h := i.History[i.PromptType]
h[len(h)-1] = respsince it doesn't realize that
Anyway, your fix looks correct. |
| break | ||
| } | ||
| } | ||
| i.PromptCallback(resp, false) |
There was a problem hiding this comment.
nit: add empty line above this line?
There was a problem hiding this comment.
Please remove spaces from this empty line.
434eec8 to
26f192c
Compare
|
Merged, thanks. |
According to https://pkg.go.dev/github.com/zyedidia/micro/v2@v2.0.13/internal/info#InfoBuf.Prompt
A lua script can provide a done callback when the prompt has finished (enter is pressed).
This works fine until the done callback itself wants to have another prompt, for example multiple questions to configure an action.
The behavior in main right now will crash with
So if I have something like this
It won't work and crash inside the 2nd prompt.
This is due to the 2nd call in the prompt trying to access history while the first prompt has not finished modifying the history.
See
https://github.com/zyedidia/micro/blob/e9bd1b35f4ee62352a361fdec94684accd6f4874/internal/info/infobuffer.go#L97-L109
and https://github.com/zyedidia/micro/blob/e9bd1b35f4ee62352a361fdec94684accd6f4874/internal/info/infobuffer.go#L150-L154
where line 152 is calling the done callback and we are still modifying the history for the current prompt on line 153+
The proposed solution just needs to reorder the done callback to the end of the block where we have finished modifying the history so that the next prompt call can safely modify it.