From 8f04deadc1f1620dbd0aaea07f04a6c7f82f58c0 Mon Sep 17 00:00:00 2001 From: Achilleas Anagnostopoulos Date: Wed, 28 Mar 2018 07:25:52 +0100 Subject: [PATCH] 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. --- src/gopheros/kernel/kfmt/ringbuf.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/gopheros/kernel/kfmt/ringbuf.go b/src/gopheros/kernel/kfmt/ringbuf.go index 2bd5baa..c377dbc 100644 --- a/src/gopheros/kernel/kfmt/ringbuf.go +++ b/src/gopheros/kernel/kfmt/ringbuf.go @@ -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 }