diff --git a/lib/system.nim b/lib/system.nim index a7b22a2839..e8b427c554 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -4028,6 +4028,8 @@ proc shallow*(s: var string) {.noSideEffect, inline.} = ## purposes. when not defined(JS) and not defined(nimscript) and not defined(gcDestructors): var s = cast[PGenericSeq](s) + if s == nil: + s = cast[PGenericSeq](newString(0)) # string literals cannot become 'shallow': if (s.reserved and strlitFlag) == 0: s.reserved = s.reserved or seqShallowFlag diff --git a/tests/system/tnilconcats.nim b/tests/system/tnilconcats.nim index 5e4a1b3176..c1126405c5 100644 --- a/tests/system/tnilconcats.nim +++ b/tests/system/tnilconcats.nim @@ -23,3 +23,10 @@ when true: doAssert s == "fooabc" echo x + + # casting an empty string as sequence with shallow() should not segfault + var s2: string + shallow(s2) + s2 &= "foo" + doAssert s2 == "foo" +