You can't spell "evil" without "vi".

Rob Landley rob at landley.net
Mon Oct 13 04:48:22 UTC 2008


If you use the cursor keys in vi, it adds three characters at a time.  (The 
ansi "move cursor" sequence for the direction you hit.)  If you hold it down 
so it repeats, on a slow system (such as qemu's arm emulation) or one that's 
heavily loaded, you can easily have several of them queue up before VI gets 
scheduled again.

A dozen times 3 characters is 36 characters.  If you look at editors/vi.c line 
216 you'll see that the buffer you're reading into (readbuffer) is 32 
characters.  I.E. your buffer is not divisible by 3, so it'll read an even 10 
cursor lefts, plus an escape left bracket pair which isn't part of a 
recognized sequence, so is interpreted as separate characters.  The escape 
drops you out of insert mode and into edit mode.

Next time it reads a buffer, it starts with the last character of a cursor 
left sequence: capital D.  Capital D is "delete to end of line", which it 
does.

So basically, busybox vi is corrupting your data when you cursor around in a 
file on a loaded system.  Wheee...

Rob

P.S.  And no, making the buffer bigger isn't necessarily going to fix it, you 
need a flag that remembers the last buffer was full so when you check for 
more data you treat it as part of the same unit for multi-character 
sequences.

P.P.S. An additional complication is that the readahead code (circa line 2270) 
to check for more characters specifically for an escape sequence fills the 
buffer up to sizeof()-8 (I.E. 24), which by pure luck is divisible by 3, 
although not all sequences are.  But in any case, I'm not quite sure what 
this is doing.  Seems overcomplicated, but then the whole "#define G.thingy 
thingy" bit copied into each file pretty much claimed "overcomplicated", 
planeted a flag in it, an started reciting a national anthem.  Is there 
something wrong with just using G.thingy when that's what you mean?  As for 
copying G.chars_to_parse into n in readit(), if we didn't declare it volatile 
that's exactly the sort of thing the optimizer should do _for_ us, isn't it?

Rob



More information about the busybox mailing list