25
25
#include "zend_builtin_functions.h"
26
26
#include "zend_interfaces.h"
27
27
28
- static zend_class_entry * default_exception_ptr ;
28
+ static zend_class_entry * default_exception_ce ;
29
29
static zend_object_handlers default_exception_handlers ;
30
30
ZEND_API void zend_throw_exception (zend_class_entry * exception_ce , char * message , long code TSRMLS_DC );
31
31
@@ -47,9 +47,9 @@ static zend_object_value zend_default_exception_new(zend_class_entry *class_type
47
47
trace -> refcount = 0 ;
48
48
zend_fetch_debug_backtrace (trace , 0 TSRMLS_CC );
49
49
50
- zend_update_property_string (default_exception_ptr , & obj , "file" , sizeof ("file" )- 1 , zend_get_executed_filename (TSRMLS_C ) TSRMLS_CC );
51
- zend_update_property_long (default_exception_ptr , & obj , "line" , sizeof ("line" )- 1 , zend_get_executed_lineno (TSRMLS_C ) TSRMLS_CC );
52
- zend_update_property (default_exception_ptr , & obj , "trace" , sizeof ("trace" )- 1 , trace TSRMLS_CC );
50
+ zend_update_property_string (default_exception_ce , & obj , "file" , sizeof ("file" )- 1 , zend_get_executed_filename (TSRMLS_C ) TSRMLS_CC );
51
+ zend_update_property_long (default_exception_ce , & obj , "line" , sizeof ("line" )- 1 , zend_get_executed_lineno (TSRMLS_C ) TSRMLS_CC );
52
+ zend_update_property (default_exception_ce , & obj , "trace" , sizeof ("trace" )- 1 , trace TSRMLS_CC );
53
53
54
54
return obj .value .obj ;
55
55
}
@@ -74,11 +74,11 @@ ZEND_METHOD(exception, __construct)
74
74
object = getThis ();
75
75
76
76
if (message ) {
77
- zend_update_property_string (default_exception_ptr , object , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
77
+ zend_update_property_string (default_exception_ce , object , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
78
78
}
79
79
80
80
if (code ) {
81
- zend_update_property_long (default_exception_ptr , object , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
81
+ zend_update_property_long (default_exception_ce , object , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
82
82
}
83
83
}
84
84
@@ -91,7 +91,7 @@ static void _default_exception_get_entry(zval *object, char *name, int name_len,
91
91
{
92
92
zval * value ;
93
93
94
- value = zend_read_property (default_exception_ptr , object , name , name_len , 0 TSRMLS_CC );
94
+ value = zend_read_property (default_exception_ce , object , name , name_len , 0 TSRMLS_CC );
95
95
96
96
* return_value = * value ;
97
97
zval_copy_ctor (return_value );
@@ -290,7 +290,7 @@ ZEND_METHOD(exception, getTraceAsString)
290
290
char * res = estrdup ("" ), * * str = & res , * s_tmp ;
291
291
int res_len = 0 , * len = & res_len , num = 0 ;
292
292
293
- trace = zend_read_property (default_exception_ptr , getThis (), "trace" , sizeof ("trace" )- 1 , 1 TSRMLS_CC );
293
+ trace = zend_read_property (default_exception_ce , getThis (), "trace" , sizeof ("trace" )- 1 , 1 TSRMLS_CC );
294
294
zend_hash_apply_with_arguments (Z_ARRVAL_P (trace ), (apply_func_args_t )_build_trace_string , 3 , str , len , & num );
295
295
296
296
s_tmp = emalloc (1 + MAX_LENGTH_OF_LONG + 7 + 1 );
@@ -316,15 +316,19 @@ int zend_spprintf(char **message, int max_len, char *format, ...)
316
316
317
317
ZEND_METHOD (exception , __toString )
318
318
{
319
- zval * message , * file , * line , * trace ;
319
+ zval message , file , line , * trace ;
320
320
char * str ;
321
321
int len ;
322
322
zend_fcall_info fci ;
323
323
zval fname ;
324
-
325
- message = zend_read_property (default_exception_ptr , getThis (), "message" , sizeof ("message" )- 1 , 1 TSRMLS_CC );
326
- file = zend_read_property (default_exception_ptr , getThis (), "file" , sizeof ("file" )- 1 , 1 TSRMLS_CC );
327
- line = zend_read_property (default_exception_ptr , getThis (), "line" , sizeof ("line" )- 1 , 1 TSRMLS_CC );
324
+
325
+ _default_exception_get_entry (getThis (), "message" , sizeof ("message" )- 1 , & message TSRMLS_CC );
326
+ _default_exception_get_entry (getThis (), "file" , sizeof ("file" )- 1 , & file TSRMLS_CC );
327
+ _default_exception_get_entry (getThis (), "line" , sizeof ("line" )- 1 , & line TSRMLS_CC );
328
+
329
+ convert_to_string (& message );
330
+ convert_to_string (& file );
331
+ convert_to_long (& line );
328
332
329
333
ZVAL_STRINGL (& fname , "gettraceasstring" , sizeof ("gettraceasstring" )- 1 , 0 );
330
334
@@ -340,21 +344,31 @@ ZEND_METHOD(exception, __toString)
340
344
341
345
zend_call_function (& fci , NULL TSRMLS_CC );
342
346
343
- if (Z_STRLEN_P (message ) > 0 ) {
347
+ if (Z_TYPE_P (trace ) != IS_STRING ) {
348
+ trace = NULL ;
349
+ }
350
+
351
+ if (Z_STRLEN (message ) > 0 ) {
344
352
len = zend_spprintf (& str , 0 , "exception '%s' with message '%s' in %s:%ld\nStack trace:\n%s" ,
345
- Z_OBJCE_P (getThis ())-> name , Z_STRVAL_P (message ), Z_STRVAL_P (file ), Z_LVAL_P (line ),
346
- Z_STRLEN_P (trace ) ? Z_STRVAL_P (trace ) : "#0 {main}\n" );
353
+ Z_OBJCE_P (getThis ())-> name , Z_STRVAL (message ), Z_STRVAL (file ), Z_LVAL (line ),
354
+ ( trace && Z_STRLEN_P (trace ) ) ? Z_STRVAL_P (trace ) : "#0 {main}\n" );
347
355
} else {
348
356
len = zend_spprintf (& str , 0 , "exception '%s' in %s:%ld\nStack trace:\n%s" ,
349
- Z_OBJCE_P (getThis ())-> name , Z_STRVAL_P (file ), Z_LVAL_P (line ),
350
- Z_STRLEN_P (trace ) ? Z_STRVAL_P (trace ) : "#0 {main}\n" );
357
+ Z_OBJCE_P (getThis ())-> name , Z_STRVAL (file ), Z_LVAL (line ),
358
+ ( trace && Z_STRLEN_P (trace ) ) ? Z_STRVAL_P (trace ) : "#0 {main}\n" );
351
359
}
352
360
353
361
/* We store the result in the private property string so we can access
354
362
* the result in uncaught exception handlers without memleaks. */
355
- zend_update_property_string (default_exception_ptr , getThis (), "string" , sizeof ("string" )- 1 , str TSRMLS_CC );
363
+ zend_update_property_string (default_exception_ce , getThis (), "string" , sizeof ("string" )- 1 , str TSRMLS_CC );
364
+
365
+ if (trace ) {
366
+ zval_ptr_dtor (& trace );
367
+ }
356
368
357
- zval_ptr_dtor (& trace );
369
+ zval_dtor (& message );
370
+ zval_dtor (& file );
371
+ zval_dtor (& line );
358
372
359
373
RETURN_STRINGL (str , len , 0 );
360
374
}
@@ -386,22 +400,22 @@ static void zend_register_default_exception(TSRMLS_D)
386
400
zend_class_entry ce ;
387
401
388
402
INIT_CLASS_ENTRY (ce , "exception" , default_exception_functions );
389
- default_exception_ptr = zend_register_internal_class (& ce TSRMLS_CC );
390
- default_exception_ptr -> create_object = zend_default_exception_new ;
403
+ default_exception_ce = zend_register_internal_class (& ce TSRMLS_CC );
404
+ default_exception_ce -> create_object = zend_default_exception_new ;
391
405
memcpy (& default_exception_handlers , zend_get_std_object_handlers (), sizeof (zend_object_handlers ));
392
406
default_exception_handlers .clone_obj = NULL ;
393
407
394
- zend_declare_property_string (default_exception_ptr , "message" , sizeof ("message" )- 1 , "" , ZEND_ACC_PROTECTED TSRMLS_CC );
395
- zend_declare_property_string (default_exception_ptr , "string" , sizeof ("string" )- 1 , "" , ZEND_ACC_PRIVATE TSRMLS_CC );
396
- zend_declare_property_long (default_exception_ptr , "code" , sizeof ("code" )- 1 , 0 , ZEND_ACC_PROTECTED TSRMLS_CC );
397
- zend_declare_property_null (default_exception_ptr , "file" , sizeof ("file" )- 1 , ZEND_ACC_PROTECTED TSRMLS_CC );
398
- zend_declare_property_null (default_exception_ptr , "line" , sizeof ("line" )- 1 , ZEND_ACC_PROTECTED TSRMLS_CC );
399
- zend_declare_property_null (default_exception_ptr , "trace" , sizeof ("trace" )- 1 , ZEND_ACC_PRIVATE TSRMLS_CC );
408
+ zend_declare_property_string (default_exception_ce , "message" , sizeof ("message" )- 1 , "" , ZEND_ACC_PROTECTED TSRMLS_CC );
409
+ zend_declare_property_string (default_exception_ce , "string" , sizeof ("string" )- 1 , "" , ZEND_ACC_PRIVATE TSRMLS_CC );
410
+ zend_declare_property_long (default_exception_ce , "code" , sizeof ("code" )- 1 , 0 , ZEND_ACC_PROTECTED TSRMLS_CC );
411
+ zend_declare_property_null (default_exception_ce , "file" , sizeof ("file" )- 1 , ZEND_ACC_PROTECTED TSRMLS_CC );
412
+ zend_declare_property_null (default_exception_ce , "line" , sizeof ("line" )- 1 , ZEND_ACC_PROTECTED TSRMLS_CC );
413
+ zend_declare_property_null (default_exception_ce , "trace" , sizeof ("trace" )- 1 , ZEND_ACC_PRIVATE TSRMLS_CC );
400
414
}
401
415
402
416
ZEND_API zend_class_entry * zend_exception_get_default (void )
403
417
{
404
- return default_exception_ptr ;
418
+ return default_exception_ce ;
405
419
}
406
420
407
421
ZEND_API void zend_throw_exception_ex (zend_class_entry * exception_ce , long code TSRMLS_DC , char * format , ...)
@@ -416,21 +430,21 @@ ZEND_API void zend_throw_exception_ex(zend_class_entry *exception_ce, long code
416
430
417
431
MAKE_STD_ZVAL (ex );
418
432
if (exception_ce ) {
419
- if (!instanceof_function (exception_ce , default_exception_ptr TSRMLS_CC )) {
433
+ if (!instanceof_function (exception_ce , default_exception_ce TSRMLS_CC )) {
420
434
zend_error (E_NOTICE , "Exceptions must be derived from exception" );
421
- exception_ce = default_exception_ptr ;
435
+ exception_ce = default_exception_ce ;
422
436
}
423
437
} else {
424
- exception_ce = default_exception_ptr ;
438
+ exception_ce = default_exception_ce ;
425
439
}
426
440
object_init_ex (ex , exception_ce );
427
441
428
442
429
443
if (message ) {
430
- zend_update_property_string (default_exception_ptr , ex , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
444
+ zend_update_property_string (default_exception_ce , ex , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
431
445
}
432
446
if (code ) {
433
- zend_update_property_long (default_exception_ptr , ex , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
447
+ zend_update_property_long (default_exception_ce , ex , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
434
448
}
435
449
436
450
efree (message );
@@ -448,21 +462,21 @@ ZEND_API void zend_throw_exception(zend_class_entry *exception_ce, char *message
448
462
449
463
MAKE_STD_ZVAL (ex );
450
464
if (exception_ce ) {
451
- if (!instanceof_function (exception_ce , default_exception_ptr TSRMLS_CC )) {
465
+ if (!instanceof_function (exception_ce , default_exception_ce TSRMLS_CC )) {
452
466
zend_error (E_NOTICE , "Exceptions must be derived from exception" );
453
- exception_ce = default_exception_ptr ;
467
+ exception_ce = default_exception_ce ;
454
468
}
455
469
} else {
456
- exception_ce = default_exception_ptr ;
470
+ exception_ce = default_exception_ce ;
457
471
}
458
472
object_init_ex (ex , exception_ce );
459
473
460
474
461
475
if (message ) {
462
- zend_update_property_string (default_exception_ptr , ex , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
476
+ zend_update_property_string (default_exception_ce , ex , "message" , sizeof ("message" )- 1 , message TSRMLS_CC );
463
477
}
464
478
if (code ) {
465
- zend_update_property_long (default_exception_ptr , ex , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
479
+ zend_update_property_long (default_exception_ce , ex , "code" , sizeof ("code" )- 1 , code TSRMLS_CC );
466
480
}
467
481
468
482
zend_throw_exception_internal (ex TSRMLS_CC );
@@ -480,7 +494,7 @@ static void zend_error_va(int type, const char *file, uint lineno, const char *f
480
494
ZEND_API void zend_exception_error (zval * exception TSRMLS_DC )
481
495
{
482
496
zend_class_entry * ce_exception = Z_OBJCE_P (exception );
483
- if (instanceof_function (ce_exception , default_exception_ptr TSRMLS_CC )) {
497
+ if (instanceof_function (ce_exception , default_exception_ce TSRMLS_CC )) {
484
498
zval * str , * file , * line ;
485
499
zval * old_exception = EG (exception );
486
500
@@ -491,26 +505,26 @@ ZEND_API void zend_exception_error(zval *exception TSRMLS_DC)
491
505
if (Z_TYPE_P (str ) != IS_STRING ) {
492
506
zend_error (E_WARNING , "%s::__toString() must return a string" , ce_exception -> name );
493
507
} else {
494
- zend_update_property_string (default_exception_ptr , exception , "string" , sizeof ("string" )- 1 , EG (exception ) ? ce_exception -> name : Z_STRVAL_P (str ) TSRMLS_CC );
508
+ zend_update_property_string (default_exception_ce , exception , "string" , sizeof ("string" )- 1 , EG (exception ) ? ce_exception -> name : Z_STRVAL_P (str ) TSRMLS_CC );
495
509
}
496
510
}
497
511
zval_ptr_dtor (& str );
498
512
499
513
if (EG (exception )) {
500
514
/* do the best we can to inform about the inner exception */
501
- if (instanceof_function (ce_exception , default_exception_ptr TSRMLS_CC )) {
502
- file = zend_read_property (default_exception_ptr , EG (exception ), "file" , sizeof ("file" )- 1 , 1 TSRMLS_CC );
503
- line = zend_read_property (default_exception_ptr , EG (exception ), "line" , sizeof ("line" )- 1 , 1 TSRMLS_CC );
515
+ if (instanceof_function (ce_exception , default_exception_ce TSRMLS_CC )) {
516
+ file = zend_read_property (default_exception_ce , EG (exception ), "file" , sizeof ("file" )- 1 , 1 TSRMLS_CC );
517
+ line = zend_read_property (default_exception_ce , EG (exception ), "line" , sizeof ("line" )- 1 , 1 TSRMLS_CC );
504
518
} else {
505
519
file = NULL ;
506
520
line = NULL ;
507
521
}
508
522
zend_error_va (E_WARNING , file ? Z_STRVAL_P (file ) : NULL , line ? Z_LVAL_P (line ) : 0 , "Uncaught %s in exception handling during call to %s::__tostring()" , Z_OBJCE_P (EG (exception ))-> name , ce_exception -> name );
509
523
}
510
524
511
- str = zend_read_property (default_exception_ptr , exception , "string" , sizeof ("string" )- 1 , 1 TSRMLS_CC );
512
- file = zend_read_property (default_exception_ptr , exception , "file" , sizeof ("file" )- 1 , 1 TSRMLS_CC );
513
- line = zend_read_property (default_exception_ptr , exception , "line" , sizeof ("line" )- 1 , 1 TSRMLS_CC );
525
+ str = zend_read_property (default_exception_ce , exception , "string" , sizeof ("string" )- 1 , 1 TSRMLS_CC );
526
+ file = zend_read_property (default_exception_ce , exception , "file" , sizeof ("file" )- 1 , 1 TSRMLS_CC );
527
+ line = zend_read_property (default_exception_ce , exception , "line" , sizeof ("line" )- 1 , 1 TSRMLS_CC );
514
528
515
529
EG (exception ) = old_exception ;
516
530
0 commit comments