Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/pure/streams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ else: # after 1.3 or JS not defined
raise newException(Defect, "could not write to string stream, " &
"did you use a non-string buffer pointer?", getCurrentException())
elif not defined(nimscript):
copyMem(beginStore(s.data, s.pos + bufLen, s.pos), buffer, bufLen)
copyMem(beginStore(s.data, max(s.data.len, s.pos + bufLen), s.pos), buffer, bufLen)
endStore(s.data)
inc(s.pos, bufLen)

Expand Down
8 changes: 8 additions & 0 deletions tests/js/tstreams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,11 @@ doAssert s2.readAll == "abc"
s2.write("def")
doAssert s2.data == "abcdef"
s2.close

# bug #26088
var s3 = newStringStream("0123456789ABCDEF")
s3.setPosition(0)
s3.write("XX")
doAssert s3.data == "XX23456789ABCDEF"
doAssert not s3.atEnd
s3.close
14 changes: 14 additions & 0 deletions tests/stdlib/tstreams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,20 @@ block:
ss.setPosition(0)
doAssert(ss.peekStr(5) == "hello")

# bug #26088 - Overwriting a string stream must not truncate it
block:
var short = newStringStream("0123456789ABCDEF")
short.setPosition(0)
short.write("XX")
doAssert short.data == "XX23456789ABCDEF"
doAssert not short.atEnd

var long = newStringStream("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
long.setPosition(0)
long.write("XX")
doAssert long.data == "XX23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
doAssert not long.atEnd

# bug #19716
static: # Ensure streams it doesnt break with nimscript on arc/orc #19716
let s = newStringStream("a")
Expand Down
Loading