Skip to content

Commit

Permalink
Fix null dereference in check_finalizer_nested if redirect malloc on …
Browse files Browse the repository at this point in the history
…Linux

(a cherry-pick of commit 0408c6e from 'master')

Issue #582 (bdwgc).

As noted in GC_start_routine, an allocation may happen in
GC_get_stack_base, causing GC_notify_or_invoke_finalizers to be called
before the thread gets registered.

* pthread_support.c [!GC_NO_FINALIZATION && INCLUDE_LINUX_THREAD_DESCR
&& REDIRECT_MALLOC] (GC_check_finalizer_nested): If me variable is NULL
then return NULL; add comment.
  • Loading branch information
ivmai committed Dec 14, 2023
1 parent 620d4a1 commit a022304
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pthread_support.c
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,15 @@ GC_INNER GC_thread GC_lookup_thread(pthread_t id)
GC_INNER unsigned char *GC_check_finalizer_nested(void)
{
GC_thread me = GC_lookup_thread(pthread_self());
unsigned nesting_level = me->finalizer_nested;
unsigned nesting_level;

# if defined(INCLUDE_LINUX_THREAD_DESCR) && defined(REDIRECT_MALLOC)
/* As noted in GC_start_routine, an allocation may happen in */
/* GC_get_stack_base, causing GC_notify_or_invoke_finalizers */
/* to be called before the thread gets registered. */
if (EXPECT(NULL == me, FALSE)) return NULL;
# endif
nesting_level = me->finalizer_nested;
if (nesting_level) {
/* We are inside another GC_invoke_finalizers(). */
/* Skip some implicitly-called GC_invoke_finalizers() */
Expand Down

0 comments on commit a022304

Please sign in to comment.