chzyer / readline

Readline is a pure go(golang) implementation for GNU-Readline kind library
MIT License
2.08k stars 275 forks source link

TOCTOU race in prompt redraw #220

Open slingamn opened 1 year ago

slingamn commented 1 year ago

Using the test case from #217 (calling (*Instance).Write() concurrently with (*Instance).Readline()), with v1.5.1 on go 1.20, there is a race condition (although not a data race) where the prompt is not always redrawn correctly. Example output:

> a
received a
> b
> received b
> c
received c
> d
> received d
> e
received e
> f
received f
> g
received g
> 
received 
> 
received 
> 
received 
> 
received 
> 
> received 
> 
received 
> 
received 
> 
> received 

The lines where > is followed by text are failures to redraw the prompt.

This appears to be caused by a TOCTOU race here:

https://github.com/chzyer/readline/blob/7f93d88cd5ffa0e805d58d2f9fc3191be15ec668/operation.go#L48-L50

Removing this fast path fixes the issue, but I'm wondering if it might have negative performance implications?