From 3379d26629f30e6be8d303a36e220d1039eb4551 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:20:54 +0800 Subject: [PATCH] fixes #23223; prevents `insert` self-assignment (#23225) fixes #23223 --- lib/system.nim | 2 ++ tests/system/tsystem_misc.nim | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/lib/system.nim b/lib/system.nim index 0602d8fa42..8aa1cc34c2 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2520,6 +2520,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 diff --git a/tests/system/tsystem_misc.nim b/tests/system/tsystem_misc.nim index 7f59147254..c8e2b2a9df 100644 --- a/tests/system/tsystem_misc.nim +++ b/tests/system/tsystem_misc.nim @@ -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()