-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Local variables can be prematurely moved to closure, causing use-after-move #23748
Comments
!nim c type
O = ref object
s: string
cb: seq[proc()]
proc push1(o: O, i: int) =
let o = o
echo o.s, " ", i
o.cb.add(proc() = echo o.s, " ", i)
proc push2(o: O, i: int) =
let o = o
echo o.s, " ", i
proc p() = echo o.s, " ", i
o.cb.add(p)
let o = O(s: "hello", cb: @[])
o.push1(42) # This segfaults
o.push2(42) # This also segfaults |
By the way, workaround is simple here: just add discard o in the end of the proc and everything will work fine. |
alex65536
added a commit
to alex65536/Nim
that referenced
this issue
Jun 28, 2024
…s for proc arguments
Graveflo
pushed a commit
to Graveflo/Nim
that referenced
this issue
Jul 1, 2024
…s for proc arguments (nim-lang#23769) fixes nim-lang#23748
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Description
Reproducer:
This code segfaults under
--mm:orc
and--mm:arc
, but works under--mm:refc
.Nim Version
Reproduces on
devel
,2.0.6
and1.6.16
with--mm:orc
, and doesn't reproduce with--mm:refc
.Current Output
Expected Output
Possible Solution
No response
Additional Information
--expandArc:push2
gives the following:It can be seen that using
:env.o1.cb
happens on the same line with moving:env
intoblitTmp
.The generated C code for the fragment
looks as follows:
Here, is it obvious that
:env
is moved first and then reused, thus leading to use-after-move.The text was updated successfully, but these errors were encountered: