Skip to content

Commit 9512d70

Browse files
committed
Fixed possible crash in DEBUG() macro when no GLOBAL_LD was provided.
This can happen in resizeThreadMax() called from alloc_thread()
1 parent f94d927 commit 9512d70

File tree

2 files changed

+33
-25
lines changed

2 files changed

+33
-25
lines changed

src/pl-builtin.h

+32-22
Original file line numberDiff line numberDiff line change
@@ -481,28 +481,38 @@ is also printed if stdio is not available.
481481
#include "pl-debug.h"
482482

483483
#if O_DEBUG
484-
#define DEBUG(n, g) do \
485-
{ if (DEBUGGING(n)) \
486-
{ ENTER_DEBUG(n); g; EXIT_DEBUG(n); } \
487-
} while(0)
488-
#define ENTER_DEBUG(n) pl_internaldebugstatus_t \
489-
__orig_ld_debug = GLOBAL_LD->internal_debug, \
490-
__new_ld_debug = GLOBAL_LD->internal_debug = \
491-
(pl_internaldebugstatus_t) \
492-
{ .channel = DEBUGGING(n) ? prolog_debug_topic_name(n) : NULL, \
493-
.depth = __orig_ld_debug.depth + 1, \
494-
}
495-
#define EXIT_DEBUG(n) GLOBAL_LD->internal_debug = \
496-
( GLOBAL_LD->internal_debug.depth != __new_ld_debug.depth \
497-
? Sdprintf("DEBUG stack depth mismatch! %d != %d\n", GLOBAL_LD->internal_debug.depth, __new_ld_debug.depth) \
498-
: 1 \
499-
) ? __orig_ld_debug : __orig_ld_debug
484+
#define DEBUG(n, g) \
485+
do \
486+
{ if (DEBUGGING(n)) \
487+
{ PL_local_data_t *__dbg_ld = GLOBAL_LD; \
488+
if ( __dbg_ld ) \
489+
{ ENTER_DEBUG(__dbg_ld, n); g; EXIT_DEBUG(__dbg_ld, n); \
490+
} else \
491+
{ g; \
492+
} \
493+
} \
494+
} while(0)
495+
#define ENTER_DEBUG(ld, n) \
496+
pl_internaldebugstatus_t \
497+
__orig_ld_debug = ld->internal_debug, \
498+
__new_ld_debug = ld->internal_debug = \
499+
(pl_internaldebugstatus_t) \
500+
{ .channel = DEBUGGING(n) ? prolog_debug_topic_name(n) : NULL, \
501+
.depth = __orig_ld_debug.depth + 1, \
502+
}
503+
#define EXIT_DEBUG(ld, n) \
504+
ld->internal_debug = \
505+
( GLOBAL_LD->internal_debug.depth != __new_ld_debug.depth \
506+
? Sdprintf("DEBUG stack depth mismatch! %d != %d\n", \
507+
GLOBAL_LD->internal_debug.depth, __new_ld_debug.depth) \
508+
: 1 \
509+
) ? __orig_ld_debug : __orig_ld_debug
500510
#define DEBUGGING(n) ((n) <= DBG_LEVEL9 ? GD->debug_level >= (n) : \
501511
(GD->debug_topics && true_bit(GD->debug_topics, n)))
502-
#define WITH_DEBUG_FOR(n) for \
503-
( ENTER_DEBUG(n); \
504-
__orig_ld_debug.depth >= 0; \
505-
EXIT_DEBUG(n), __orig_ld_debug.depth = -1 )
512+
#define WITH_DEBUG_FOR(n) for \
513+
( ENTER_DEBUG(GLOBAL_LD, n); \
514+
__orig_ld_debug.depth >= 0; \
515+
EXIT_DEBUG(GLOBAL_LD, n), __orig_ld_debug.depth = -1 )
506516
#define IF_DEBUGGING(n) if (DEBUGGING(n)) WITH_DEBUG_FOR(n)
507517

508518
/* We want to use the version of Sdprintf with the debug channel, if possible */
@@ -512,8 +522,8 @@ int Sdprintf_ex(const char *channel, const char *file, int line, const char *fm,
512522

513523
#else
514524
#define DEBUG(a, b) ((void)0)
515-
#define ENTER_DEBUG(n) ;
516-
#define EXIT_DEBUG(n) ;
525+
#define ENTER_DEBUG(ld, n) ;
526+
#define EXIT_DEBUG(ld, n) ;
517527
#define DEBUGGING(n) false
518528
#define WITH_DEBUG_FOR(n) /* empty */
519529
#define IF_DEBUGGING(n) if (0)

src/pl-thread.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -1602,7 +1602,7 @@ to ensure we can access GD->thread.threads[i] at any time without
16021602
locking.
16031603
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
16041604

1605-
static int
1605+
static void
16061606
resizeThreadMax(void)
16071607
{ int newmax = GD->thread.thread_max*2;
16081608
PL_thread_info_t **newinfo, **oldinfo;
@@ -1615,8 +1615,6 @@ resizeThreadMax(void)
16151615
GD->thread.threads = newinfo;
16161616
GD->thread.thread_max = newmax;
16171617
linger(&GD->thread.lingering, PL_free, oldinfo);
1618-
1619-
return true;
16201618
}
16211619

16221620

0 commit comments

Comments
 (0)