diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2024-08-20 17:57:47 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-20 11:57:47 +0200 |
commit | 26107e931cb846647bae3a7335d2ac1993dc4386 (patch) | |
tree | e6ccfe49cb179a3ee737b06b84c2118fa2867914 | |
parent | 34719cad9d95b9a13fbbc5bfb9e5e06662a8c6ed (diff) | |
download | Nim-26107e931cb846647bae3a7335d2ac1993dc4386.tar.gz |
fixes #23973; fixes #23974; Memory corruption with lent and ORC (#23981)
fixes #23973; fixes #23974
-rw-r--r-- | compiler/varpartitions.nim | 17 | ||||
-rw-r--r-- | tests/arc/tarcmisc.nim | 31 |
2 files changed, 46 insertions, 2 deletions
diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index 84cf3265c..1711fea46 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -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 diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim index 58b4aa7c0..49f1b80ce 100644 --- a/tests/arc/tarcmisc.nim +++ b/tests/arc/tarcmisc.nim @@ -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() |