@@ -48,7 +48,7 @@ static void (*zend_message_dispatcher_p)(long message, void *data);
4848static int (* zend_get_ini_entry_p )(char * name , uint name_length , zval * contents );
4949
5050#if ZEND_NEW_ERROR_HANDLING
51- static void (* zend_error_cb )(int type , const char * format , ... );
51+ static void (* zend_error_cb )(int type , const char * error_filename , const uint error_lineno , const char * format , va_list args );
5252#else
5353ZEND_API void (* zend_error_cb )(int type , const char * format , ...);
5454#endif
@@ -503,54 +503,91 @@ ZEND_API void zend_error(int type, const char *format, ...)
503503 zval * * params ;
504504 zval retval ;
505505 zval error_type , error_message ;
506+ char * error_filename ;
507+ uint error_lineno ;
506508 ELS_FETCH ();
507509 CLS_FETCH ();
508510
509- INIT_PZVAL (& error_message );
510- error_message .value .str .val = (char * ) emalloc (ZEND_ERROR_BUFFER_SIZE );
511+ /* Obtain relevant filename and lineno */
512+ switch (type ) {
513+ case E_CORE_ERROR :
514+ case E_CORE_WARNING :
515+ error_filename = NULL ;
516+ error_lineno = 0 ;
517+ break ;
518+ case E_PARSE :
519+ case E_COMPILE_ERROR :
520+ case E_COMPILE_WARNING :
521+ case E_ERROR :
522+ case E_NOTICE :
523+ case E_WARNING :
524+ case E_USER_ERROR :
525+ case E_USER_WARNING :
526+ case E_USER_NOTICE :
527+ if (zend_is_compiling ()) {
528+ error_filename = zend_get_compiled_filename (CLS_C );
529+ error_lineno = zend_get_compiled_lineno (CLS_C );
530+ } else if (zend_is_executing ()) {
531+ error_filename = zend_get_executed_filename (ELS_C );
532+ error_lineno = zend_get_executed_lineno (ELS_C );
533+ } else {
534+ error_filename = NULL ;
535+ error_lineno = 0 ;
536+ }
537+ break ;
538+ default :
539+ error_filename = NULL ;
540+ error_lineno = 0 ;
541+ break ;
542+ }
543+ if (!error_filename ) {
544+ error_filename = "Unknown" ;
545+ }
511546
512- va_start (args , format );
513- /* error_message.value.str.len = vsnprintf(error_message->value.str.val, error_message->value.str.len-1, format, args); */
514- error_message .value .str .len = vsprintf (error_message .value .str .val , format , args );
515- error_message .type = IS_STRING ;
516- va_end (args );
517547
548+ va_start (args , format );
518549 /* if we don't have a user defined error handler */
519550 if (!EG (user_error_handler )) {
520- zend_error_cb (type , error_message .value .str .val );
521- efree (error_message .value .str .val );
522- return ;
523- }
524-
525- /* or the error may not be safe to handle in user-space */
526- switch (type ) {
551+ zend_error_cb (type , error_filename , error_lineno , format , args );
552+ } else switch (type ) {
527553 case E_ERROR :
528554 case E_PARSE :
529555 case E_CORE_ERROR :
530556 case E_CORE_WARNING :
531557 case E_COMPILE_ERROR :
532558 case E_COMPILE_WARNING :
533- zend_error_cb (type , error_message .value .str .val );
534- efree (error_message .value .str .val );
535- return ;
536- }
559+ /* The error may not be safe to handle in user-space */
560+ zend_error_cb (type , error_filename , error_lineno , format , args );
561+ break ;
562+ default :
563+ /* Handle the error in user space */
564+ INIT_PZVAL (& error_message );
565+ INIT_PZVAL (& error_type );
566+ error_message .value .str .val = (char * ) emalloc (ZEND_ERROR_BUFFER_SIZE );
537567
538- /* Handle the error in user space */
568+ /* error_message.value.str.len = vsnprintf(error_message->value.str.val, error_message->value.str.len-1, format, args); */
569+ error_message .value .str .len = vsprintf (error_message .value .str .val , format , args );
570+ error_message .type = IS_STRING ;
539571
540- error_type .value .lval = type ;
541- error_type .type = IS_LONG ;
572+ error_type .value .lval = type ;
573+ error_type .type = IS_LONG ;
542574
543- params = (zval * * ) emalloc (sizeof (zval * )* 2 );
544- params [0 ] = & error_type ;
545- params [1 ] = & error_message ;
575+ params = (zval * * ) emalloc (sizeof (zval * )* 2 );
576+ params [0 ] = & error_type ;
577+ params [1 ] = & error_message ;
546578
547- if (call_user_function (CG (function_table ), NULL , EG (user_error_handler ), & retval , 2 , params )== SUCCESS ) {
548- } else {
549- /* The user error handler failed, use built-in error handler */
550- zend_error_cb (type , error_message .value .str .val );
579+ if (call_user_function (CG (function_table ), NULL , EG (user_error_handler ), & retval , 2 , params )== SUCCESS ) {
580+ zval_dtor (& retval );
581+ } else {
582+ /* The user error handler failed, use built-in error handler */
583+ zend_error_cb (type , error_filename , error_lineno , format , args );
584+ }
585+ efree (params );
586+ efree (error_message .value .str .val );
587+ break ;
551588 }
552- efree ( params );
553- efree ( error_message . value . str . val );
589+
590+ va_end ( args );
554591}
555592
556593#endif
0 commit comments