From 4856beae70ba058612a07ab787c9f283f885b453 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 20 Aug 2024 17:57:47 +0800 Subject: [PATCH] fixes #23973; fixes #23974; Memory corruption with lent and ORC (#23981) fixes #23973; fixes #23974 (cherry picked from commit 26107e931cb846647bae3a7335d2ac1993dc4386) --- compiler/varpartitions.nim | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/compiler/varpartitions.nim b/compiler/varpartitions.nim index 024b58e624..784d2d9e17 100644 --- a/compiler/varpartitions.nim +++ b/compiler/varpartitions.nim @@ -431,11 +431,15 @@ proc destMightOwn(c: var Partitions; dest: var VarIndex; n: PNode) = of nkIfStmt, nkIfExpr: for i in 0.. 0: @@ -488,8 +492,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