1
0
mirror of https://github.com/taigrr/gopher-os synced 2025-01-18 04:43:13 -08:00

kfmt: fix bug where calls to copy() resulted in garbage being copied

The bug manifested itself as a series of null bytes appearing to the
tty device when the ring buffer gets linked to it and its contents are
flushed to the tty.

Using gdb, I was able to track the problem into the ringbuf's Read
method and specifically to the call to copy(). I have replaced the call
with a for loop that the compiler would anyway optimize into a rep stosb
or equivalent asm instruction.
This commit is contained in:
Achilleas Anagnostopoulos 2018-03-28 07:25:52 +01:00
parent 2826507371
commit 8f04deadc1

View File

@ -40,10 +40,9 @@ func (rb *ringBuffer) Read(p []byte) (n int, err error) {
n = pLen
}
copy(p, rb.buffer[rb.rIndex:rb.rIndex+n])
rb.rIndex += n
return n, nil
for i := 0; i < n; i, rb.rIndex = i+1, rb.rIndex+1 {
p[i] = rb.buffer[rb.rIndex]
}
case rb.rIndex > rb.wIndex:
// Read up to min(len(buf) - rIndex, len(p)) bytes
n = len(rb.buffer) - rb.rIndex
@ -51,15 +50,17 @@ func (rb *ringBuffer) Read(p []byte) (n int, err error) {
n = pLen
}
copy(p, rb.buffer[rb.rIndex:rb.rIndex+n])
rb.rIndex += n
for i := 0; i < n; i, rb.rIndex = i+1, rb.rIndex+1 {
p[i] = rb.buffer[rb.rIndex]
}
if rb.rIndex == len(rb.buffer) {
rb.rIndex = 0
}
return n, nil
default: // rIndex == wIndex
return 0, io.EOF
n, err = 0, io.EOF
}
return n, err
}