Search code examples
phpgdbcoredump

Segmentation fault while running zend_hash_find()


I have written a simple php module and try to run the zend_hash_find() function. But I'm getting a strange segfault message.
Running on Centos 6.2 64bit (Under VM), php 5.4.0 (happened on 5.3.3 as well)

Here is the function:

void my_func(const char *fname,int fname_len TSRMLS_DC){
void *func;
char *funcname = estrndup( fname , fname_len ) ;

php_printf("Before find\n");

if (zend_hash_find(EG(function_table), funcname,  fname_len + 1, (void **) &func) == FAILURE)
    php_printf("not found");

php_printf("after find \n");
efree(funcname);
}

This is how the function is called

my_func("fopen",5 TSRMLS_CC);

I see the "Before find" message, then coredump. Here is gdb message

Loaded symbols for /lib64/ld-linux-x86-64.so.2
Reading symbols from /usr/lib64/php/modules/mymod.so...done.
Loaded symbols for /usr/lib64/php/modules/mymod.so
Core was generated by `php test.php'.
Program terminated with signal 11, Segmentation fault.
#0  0x000000000083457d in _zend_is_inconsistent (ht=0x0, 
file=0xcd2b68 "/home/user/php/php-5.4.0/Zend/zend_hash.c", line=919)
at /home/user/php/php-5.4.0/Zend/zend_hash.c:54
54      if (ht->inconsistent==HT_OK) {
...skipped...

Anybody has any idea why this could be happening?
Thank you!


Solution

  • The usage of zend_hash_find() is correct, I'm using it the same way in production without any problems. The gdb message says that the hash table (function_table) is NULL, so I suppose that the problem is with the initialization of your module. If you paste the full source, we might help :-)

    BTW estrndup is not necessary in your case