Skip to content

Commit

Permalink
fixes nim-lang#18070;fixes memory leak with nested Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Aug 23, 2023
1 parent 3de75ff commit 7b15a84
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1292,6 +1292,7 @@ proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
# we handled the exception, remember this:
linefmt(p, cpsStmts, "*nimErr_ = NIM_FALSE;$n", [])
expr(p, t[i][0], d)
linefmt(p, cpsStmts, "#popCurrentException();$n", [])
else:
var orExpr = newRopeAppender()
for j in 0..<t[i].len - 1:
Expand All @@ -1311,13 +1312,14 @@ proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
# we handled the exception, remember this:
linefmt(p, cpsStmts, "*nimErr_ = NIM_FALSE;$n", [])
expr(p, t[i][^1], d)
linefmt(p, cpsStmts, "#popCurrentException();$n", [])

linefmt(p, cpsStmts, "#popCurrentException();$n", [])
linefmt(p, cpsStmts, "LA$1_:;$n", [nextExcept])
endBlock(p)

inc(i)
discard pop(p.nestedTryStmts)

endBlock(p)

if i < t.len and t[i].kind == nkFinally:
Expand All @@ -1338,8 +1340,10 @@ proc genTryGoto(p: BProc; t: PNode; d: var TLoc) =
# 3. finally is run for exception handling code without any 'except'
# handler present or only handlers that did not match.
linefmt(p, cpsStmts, "*nimErr_ = oldNimErrFin$1_;$n", [lab])

endBlock(p)
raiseExit(p)
linefmt(p, cpsStmts, "*nimErr_ = NIM_FALSE;$n", [])
if hasExcept: inc p.withinTryWithExcept

proc genTrySetjmp(p: BProc, t: PNode, d: var TLoc) =
Expand Down
18 changes: 18 additions & 0 deletions tests/exception/t18070.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
discard """
matrix: "--mm:refc; --mm:orc"
"""

# bug #18070
proc main() =
try:
try:
raise newException(CatchableError, "something")
except:
raise newException(CatchableError, "something else")
except:
doAssert getCurrentExceptionMsg() == "something else"

let msg = getCurrentExceptionMsg()
doAssert msg == "", "expected empty string but got: " & $msg

main()

0 comments on commit 7b15a84

Please sign in to comment.