fixes #23973; fixes #23974; Memory corruption with lent and ORC (#23981)

fixes #23973;
fixes #23974
This commit is contained in:
ringabout
2024-08-20 17:57:47 +08:00
committed by GitHub
parent 34719cad9d
commit 26107e931c
2 changed files with 46 additions and 2 deletions

View File

@@ -441,11 +441,15 @@ proc destMightOwn(c: var Partitions; dest: var VarIndex; n: PNode) =
of nkIfStmt, nkIfExpr:
for i in 0..<n.len:
inc c.inConditional
destMightOwn(c, dest, n[i].lastSon)
dec c.inConditional
of nkCaseStmt:
for i in 1..<n.len:
inc c.inConditional
destMightOwn(c, dest, n[i].lastSon)
dec c.inConditional
of nkStmtList, nkStmtListExpr:
if n.len > 0:
@@ -498,8 +502,17 @@ proc destMightOwn(c: var Partitions; dest: var VarIndex; n: PNode) =
# we know the result is derived from the first argument:
var roots: seq[(PSym, int)] = @[]
allRoots(n[1], roots, RootEscapes)
for r in roots:
connect(c, dest.sym, r[0], n[1].info)
if roots.len == 0 and c.inConditional > 0:
# when in a conditional expression,
# to ensure that the first argument isn't outlived
# by the lvalue, we need find the root, otherwise
# it is probably a local temporary
# (e.g. a return value from a call),
# we should prevent cursorfication
dest.flags.incl preventCursor
else:
for r in roots:
connect(c, dest.sym, r[0], n[1].info)
else:
let magic = if n[0].kind == nkSym: n[0].sym.magic else: mNone

View File

@@ -32,6 +32,7 @@ true
copying
123
42
@["", "d", ""]
ok
destroying variable: 20
destroying variable: 10
@@ -789,3 +790,33 @@ block: # bug #23907
doAssert callback(Thingy(value: 123)) == 123
import std/strutils
block: # bug #23974
func g(e: seq[string]): lent seq[string] = result = e
proc k(f: string): seq[string] = f.split("/")
proc n() =
const r = "/d/"
let t =
if true:
k(r).g()
else:
k("/" & r).g()
echo t
n()
block: # bug #23973
func g(e: seq[string]): lent seq[string] = result = e
proc k(f: string): seq[string] = f.split("/")
proc n() =
const r = "/test/empty" # or "/test/empty/1"
let a = k(r).g()
let t =
if true:
k(r).g()
else:
k("/" & r).g() # or raiseAssert ""
doAssert t == a
n()