Calling returned closures cause segfault #6

Open
opened 2026-07-12 10:39:27 +00:00 by manabug · 0 comments
Owner

When calling a closure that has been returned from a function, a segmentation fault happens.

This is because the closure inherits then function's environment, which gets automatically destroyed as soon as the function body has been evaluated. So when the closure is called, a use-after-free bug happens when accessing it's environment.

Reproducible example:

fun returns_closure(x) {
    fun closure() { // closure inherits environment from returns_closure
        return x
    }
    return closure
} // the returns_closure environment gets destroyed.

c := returns_closure(2)
c()
When calling a closure that has been returned from a function, a segmentation fault happens. This is because the closure inherits then function's environment, which gets automatically destroyed as soon as the function body has been evaluated. So when the closure is called, a use-after-free bug happens when accessing it's environment. Reproducible example: ``` fun returns_closure(x) { fun closure() { // closure inherits environment from returns_closure return x } return closure } // the returns_closure environment gets destroyed. c := returns_closure(2) c() ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
manabug/chroma#6
No description provided.