From ce773b70a7725cecec14ed4dcbdeeac4c7c0db1c Mon Sep 17 00:00:00 2001 From: Reimer Behrends Date: Sat, 24 May 2014 07:04:34 +0200 Subject: Fixed readAllBuffer() to avoid adding garbage bytes at end. The function readAllBuffer() always returned a string that was a multiple of the BufSize in length, regardless of how many bytes were actually read, padding the result with garbage bytes on the last chunk. This fix properly trims the last chunk to its actual size. --- lib/system/sysio.nim | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 02c17b92b..32d4c3e91 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -115,10 +115,14 @@ proc readAllBuffer(file: TFile): string = # bytes we need to read before the buffer is empty. result = "" var buffer = newString(BufSize) - var bytesRead = BufSize - while bytesRead == BufSize: - bytesRead = readBuffer(file, addr(buffer[0]), BufSize) - result.add(buffer) + while true: + var bytesRead = readBuffer(file, addr(buffer[0]), BufSize) + if bytesRead == BufSize: + result.add(buffer) + else: + buffer.setLen(bytesRead) + result.add(buffer) + break proc rawFileSize(file: TFile): int = # this does not raise an error opposed to `getFileSize` -- cgit 1.4.1-2-gfad0