summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorflywind <43030857+xflywind@users.noreply.github.com>2022-07-15 15:42:54 +0800
committerGitHub <noreply@github.com>2022-07-15 09:42:54 +0200
commit286fcef68ef6f0e3a20ab5b25307c9b4705ce54d (patch)
tree8be4c27651f1dca9c08f3ebe4d17a67f19043b6f
parent800cb006e74f8c52bde69cfc1eb9e55fcc439633 (diff)
downloadNim-286fcef68ef6f0e3a20ab5b25307c9b4705ce54d.tar.gz
[Orc] fixes "streams.readDataStr segafaults" when accepting a string literal (#20019) [backport]
fixes streams.readDataStr accept a string literal
-rw-r--r--lib/pure/streams.nim5
-rw-r--r--tests/stdlib/tstreams.nim10
2 files changed, 15 insertions, 0 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim
index f58273ee8..50b3085d2 100644
--- a/lib/pure/streams.nim
+++ b/lib/pure/streams.nim
@@ -1197,6 +1197,11 @@ else: # after 1.3 or JS not defined
 
   proc ssReadDataStr(s: Stream, buffer: var string, slice: Slice[int]): int =
     var s = StringStream(s)
+    when nimvm:
+      discard
+    else:
+      when declared(prepareMutation):
+        prepareMutation(buffer) # buffer might potentially be a CoW literal with ARC
     result = min(slice.b + 1 - slice.a, s.data.len - s.pos)
     if result > 0:
       jsOrVmBlock:
diff --git a/tests/stdlib/tstreams.nim b/tests/stdlib/tstreams.nim
index d9857926e..cc1343651 100644
--- a/tests/stdlib/tstreams.nim
+++ b/tests/stdlib/tstreams.nim
@@ -85,3 +85,13 @@ block:
 static: # Ensure streams it doesnt break with nimscript on arc/orc #19716
   let s = newStringStream("a")
   doAssert s.data == "a"
+
+template main =
+  var strm = newStringStream("abcde")
+  var buffer = "12345"
+  doAssert strm.readDataStr(buffer, 0..3) == 4
+  doAssert buffer == "abcd5"
+  strm.close()
+
+static: main()
+main()