Skip to content

Commit

Permalink
fixes #23223; prevents insert self-assignment (#23225)
Browse files Browse the repository at this point in the history
fixes #23223

(cherry picked from commit 3379d26)
  • Loading branch information
ringabout authored and narimiran committed Apr 20, 2024
1 parent eda7db1 commit 3b5eaea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,8 @@ when hasAlloc or defined(nimscript):
## var a = "abc"
## a.insert("zz", 0) # a <- "zzabc"
## ```
if item.len == 0: # prevents self-assignment
return
var xl = x.len
setLen(x, xl+item.len)
var j = xl-1
Expand Down
7 changes: 7 additions & 0 deletions tests/system/tsystem_misc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,10 @@ block:
doAssert not compiles(echo p.rawProc.repr)
doAssert not compiles(echo p.rawEnv.repr)
doAssert not compiles(echo p.finished)

proc bug23223 = # bug #23223
var stuff = "hello"
stuff.insert ""
doAssert stuff == "hello"

bug23223()

0 comments on commit 3b5eaea

Please sign in to comment.