Skip to content

Commit

Permalink
system/excpt: check if the exception is not nil before pop (nim-lang#…
Browse files Browse the repository at this point in the history
…18247)

In CPS we would consume an exception in the except branch by stashing it
into a local then remove the exception from Nim environment so as not to
leak it to other code that would be running before the continuation
continues

However since popCurrentException() assumes that the exception always
exist, removing the exception from an except branch will cause a
SIGSEGV to happen. This commit fixes that.
  • Loading branch information
alaviss authored and PMunch committed Mar 28, 2022
1 parent 1548ea3 commit 11ac32c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/system/excpt.nim
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ proc pushCurrentException(e: sink(ref Exception)) {.compilerRtl, inl.} =
#showErrorMessage2 "A"

proc popCurrentException {.compilerRtl, inl.} =
currException = currException.up
#showErrorMessage2 "B"
if currException != nil:
currException = currException.up
#showErrorMessage2 "B"

proc popCurrentExceptionEx(id: uint) {.compilerRtl.} =
discard "only for bootstrapping compatbility"
Expand Down
7 changes: 7 additions & 0 deletions tests/exception/tsetexceptions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ let ex = newException(CatchableError, "test")
setCurrentException(ex)
doAssert getCurrentException().msg == ex.msg
doAssert getCurrentExceptionMsg() == ex.msg
setCurrentException(nil)

try:
raise newException(CatchableError, "test2")
except:
setCurrentException(nil)
doAssert getCurrentException() == nil

0 comments on commit 11ac32c

Please sign in to comment.