Skip to content

Commit

Permalink
Fix free(NULL) when canceling thread with no cleanup handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy-JL committed Feb 23, 2025
1 parent 8cde8ae commit dc5b38a
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/thread/thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,15 @@ void thread_self_quit_if_canceled(Thread* self) {
thread_assert_self(self);
if(!self->canceled) return;

for(size_t i = self->cleanups_count - 1; i < self->cleanups_count; i--) {
ThreadCancelCleanup* cleanup = &self->cleanups[i];
cleanup->callback(cleanup->context);
if(self->cleanups_count > 0) {
for(size_t i = self->cleanups_count - 1; i < self->cleanups_count; i--) {
ThreadCancelCleanup* cleanup = &self->cleanups[i];
cleanup->callback(cleanup->context);
}
self->cleanups_count = 0;
free(self->cleanups);
self->cleanups = NULL;
}
self->cleanups_count = 0;
free(self->cleanups);
self->cleanups = NULL;

pthread_exit(PTHREAD_CANCELED);
}
Expand Down

0 comments on commit dc5b38a

Please sign in to comment.