Skip to content

Commit 467b19a

Browse files
committed
Fixed startup segfault in non-debug builds
Fixes issue #87
1 parent 17039ed commit 467b19a

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

phpdbg.c

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,32 @@ void phpdbg_signal_handler(int sig, siginfo_t *info, void *context) {
879879
}
880880
#endif
881881

882+
static inline zend_mm_heap *phpdbg_mm_get_heap() {
883+
zend_mm_heap *mm_heap;
884+
885+
TSRMLS_FETCH();
886+
887+
mm_heap = zend_mm_set_heap(NULL TSRMLS_CC);
888+
zend_mm_set_heap(mm_heap TSRMLS_CC);
889+
890+
return mm_heap;
891+
}
892+
893+
void *phpdbg_malloc_wrapper(size_t size)
894+
{
895+
return zend_mm_alloc(phpdbg_mm_get_heap(), size);
896+
}
897+
898+
void phpdbg_free_wrapper(void *p)
899+
{
900+
zend_mm_free(phpdbg_mm_get_heap(), p);
901+
}
902+
903+
void *phpdbg_realloc_wrapper(void *ptr, size_t size)
904+
{
905+
return zend_mm_realloc(phpdbg_mm_get_heap(), ptr, size);
906+
}
907+
882908
int main(int argc, char **argv) /* {{{ */
883909
{
884910
sapi_module_struct *phpdbg = &phpdbg_sapi_module;
@@ -1221,20 +1247,17 @@ int main(int argc, char **argv) /* {{{ */
12211247
EXCEPTION_POINTERS *xp;
12221248
__try {
12231249
#endif
1224-
zend_mm_heap *mm_heap = zend_mm_set_heap(NULL TSRMLS_CC);
1225-
#if ZEND_DEBUG
1226-
if (!mm_heap->use_zend_alloc) {
1227-
mm_heap->_malloc = malloc;
1228-
mm_heap->_realloc = realloc;
1229-
mm_heap->_free = free;
1230-
#endif
1231-
PHPDBG_G(original_free_function) = mm_heap->_free;
1232-
mm_heap->_free = phpdbg_watch_efree;
1250+
zend_mm_heap *mm_heap = phpdbg_mm_get_heap();
1251+
1252+
if (mm_heap->use_zend_alloc) {
1253+
mm_heap->_malloc = phpdbg_malloc_wrapper;
1254+
mm_heap->_realloc = phpdbg_realloc_wrapper;
1255+
mm_heap->_free = phpdbg_free_wrapper;
12331256
mm_heap->use_zend_alloc = 0;
1234-
#if ZEND_DEBUG
12351257
}
1236-
#endif
1237-
zend_mm_set_heap(mm_heap TSRMLS_CC);
1258+
1259+
PHPDBG_G(original_free_function) = mm_heap->_free;
1260+
mm_heap->_free = phpdbg_watch_efree;
12381261

12391262
zend_activate(TSRMLS_C);
12401263

0 commit comments

Comments
 (0)