mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-10 06:54:16 +00:00
fix segfault when calling shallow() on an empty string (#9782) [backport]
shallow() casts its string argument to a seq and then tries to access its fields. Guess what happens when that string is nil, which seems to be the representation of an empty string (both the default value and an explicitly assigned ""). Segfault encountered when running "ntags -R ." on a large project. The relevant line:a1c62c38e5/ntags.nim (lines-125)(cherry picked from commitf8fa94cb20)
This commit is contained in:
committed by
narimiran
parent
9fcdc14527
commit
7dfb2a8cd3
@@ -3974,6 +3974,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
|
||||
|
||||
@@ -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"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user