summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2014-05-31 20:51:16 +0200
committerAndreas Rumpf <rumpf_a@web.de>2014-05-31 20:51:16 +0200
commit6ae46265cc327b94f8841a0acf7ce967af8eeb75 (patch)
tree91db9dc56620f3e1c09ace467a96f83ae1fdbe50
parent4bb07bc88c7f352606404310db753108aee3e113 (diff)
parentce773b70a7725cecec14ed4dcbdeeac4c7c0db1c (diff)
downloadNim-6ae46265cc327b94f8841a0acf7ce967af8eeb75.tar.gz
Merge pull request #1221 from rbehrends/readallbuf-fix
Fixed readAllBuffer() to avoid adding garbage bytes at end.
-rw-r--r--lib/system/sysio.nim12
1 files 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`