Skip to content

Commit f0bf3d4

Browse files
committed
Replace fatal error in SplFileInfo with exception
Using RuntimeException here, because the same error condition uses that in many other places in this file.
1 parent 634dd38 commit f0bf3d4

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

ext/spl/spl_directory.c

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,16 @@ PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_
199199
return intern->_path;
200200
} /* }}} */
201201

202-
static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
202+
static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
203203
{
204204
char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
205205

206206
switch (intern->type) {
207207
case SPL_FS_INFO:
208208
case SPL_FS_FILE:
209209
if (!intern->file_name) {
210-
php_error_docref(NULL, E_ERROR, "Object not initialized");
210+
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
211+
return FAILURE;
211212
}
212213
break;
213214
case SPL_FS_DIR:
@@ -228,6 +229,7 @@ static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *in
228229
}
229230
break;
230231
}
232+
return SUCCESS;
231233
} /* }}} */
232234

233235
static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
@@ -479,16 +481,13 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
479481
zval arg1, arg2;
480482
zend_error_handling error_handling;
481483

482-
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
483-
484484
switch (source->type) {
485485
case SPL_FS_INFO:
486486
case SPL_FS_FILE:
487487
break;
488488
case SPL_FS_DIR:
489489
if (!source->u.dir.entry.d_name[0]) {
490490
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
491-
zend_restore_error_handling(&error_handling);
492491
return NULL;
493492
}
494493
}
@@ -498,13 +497,16 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
498497
ce = ce ? ce : source->info_class;
499498

500499
if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
501-
break;
500+
return NULL;
502501
}
503502

504503
intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
505504
RETVAL_OBJ(&intern->std);
506505

507-
spl_filesystem_object_get_file_name(source);
506+
if (spl_filesystem_object_get_file_name(source) != SUCCESS) {
507+
return NULL;
508+
}
509+
508510
if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
509511
ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
510512
zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
@@ -521,7 +523,7 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
521523
ce = ce ? ce : source->file_class;
522524

523525
if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
524-
break;
526+
return NULL;
525527
}
526528

527529
char *open_mode = "r";
@@ -531,15 +533,15 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
531533
if (zend_parse_parameters(num_args, "|sbr",
532534
&open_mode, &open_mode_len, &use_include_path, &resource) == FAILURE
533535
) {
534-
zend_restore_error_handling(&error_handling);
535536
return NULL;
536537
}
537538

538539
intern = spl_filesystem_from_obj(spl_filesystem_object_new_ex(ce));
539-
540540
RETVAL_OBJ(&intern->std);
541541

542-
spl_filesystem_object_get_file_name(source);
542+
if (spl_filesystem_object_get_file_name(source) != SUCCESS) {
543+
return NULL;
544+
}
543545

544546
if (ce->constructor->common.scope != spl_ce_SplFileObject) {
545547
ZVAL_STRINGL(&arg1, source->file_name, source->file_name_len);
@@ -557,21 +559,21 @@ static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, sp
557559
intern->u.file.open_mode_len = open_mode_len;
558560
intern->u.file.zcontext = resource;
559561

562+
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
560563
if (spl_filesystem_file_open(intern, use_include_path, 0) == FAILURE) {
561564
zend_restore_error_handling(&error_handling);
562565
zval_ptr_dtor(return_value);
563566
ZVAL_NULL(return_value);
564567
return NULL;
565568
}
569+
zend_restore_error_handling(&error_handling);
566570
}
567571
break;
568572
}
569573
case SPL_FS_DIR:
570-
zend_restore_error_handling(&error_handling);
571574
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
572575
return NULL;
573576
}
574-
zend_restore_error_handling(&error_handling);
575577
return NULL;
576578
} /* }}} */
577579

@@ -1060,7 +1062,9 @@ PHP_METHOD(FilesystemIterator, key)
10601062
if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
10611063
RETURN_STRING(intern->u.dir.entry.d_name);
10621064
} else {
1063-
spl_filesystem_object_get_file_name(intern);
1065+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1066+
RETURN_THROWS();
1067+
}
10641068
RETURN_STRINGL(intern->file_name, intern->file_name_len);
10651069
}
10661070
}
@@ -1076,10 +1080,14 @@ PHP_METHOD(FilesystemIterator, current)
10761080
}
10771081

10781082
if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1079-
spl_filesystem_object_get_file_name(intern);
1083+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1084+
RETURN_THROWS();
1085+
}
10801086
RETURN_STRINGL(intern->file_name, intern->file_name_len);
10811087
} else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1082-
spl_filesystem_object_get_file_name(intern);
1088+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1089+
RETURN_THROWS();
1090+
}
10831091
spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
10841092
} else {
10851093
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
@@ -1131,9 +1139,10 @@ PHP_METHOD(SplFileInfo, func_name) \
11311139
if (zend_parse_parameters_none() == FAILURE) { \
11321140
RETURN_THROWS(); \
11331141
} \
1134-
\
1142+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) { \
1143+
RETURN_THROWS(); \
1144+
} \
11351145
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
1136-
spl_filesystem_object_get_file_name(intern); \
11371146
php_stat(intern->file_name, intern->file_name_len, func_num, return_value); \
11381147
zend_restore_error_handling(&error_handling); \
11391148
}
@@ -1214,7 +1223,9 @@ PHP_METHOD(SplFileInfo, getLinkTarget)
12141223
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
12151224

12161225
if (intern->file_name == NULL) {
1217-
spl_filesystem_object_get_file_name(intern);
1226+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1227+
RETURN_THROWS();
1228+
}
12181229
}
12191230
#if defined(PHP_WIN32) || defined(HAVE_SYMLINK)
12201231
if (intern->file_name == NULL) {
@@ -1263,7 +1274,9 @@ PHP_METHOD(SplFileInfo, getRealPath)
12631274
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
12641275

12651276
if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
1266-
spl_filesystem_object_get_file_name(intern);
1277+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1278+
RETURN_THROWS();
1279+
}
12671280
}
12681281

12691282
if (intern->orig_path) {
@@ -1445,7 +1458,9 @@ PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
14451458
if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
14461459
RETURN_FALSE;
14471460
} else {
1448-
spl_filesystem_object_get_file_name(intern);
1461+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1462+
RETURN_THROWS();
1463+
}
14491464
if (!allow_links && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
14501465
php_stat(intern->file_name, intern->file_name_len, FS_IS_LINK, return_value);
14511466
if (zend_is_true(return_value)) {
@@ -1469,7 +1484,9 @@ PHP_METHOD(RecursiveDirectoryIterator, getChildren)
14691484
RETURN_THROWS();
14701485
}
14711486

1472-
spl_filesystem_object_get_file_name(intern);
1487+
if (spl_filesystem_object_get_file_name(intern) != SUCCESS) {
1488+
RETURN_THROWS();
1489+
}
14731490

14741491
ZVAL_LONG(&zflags, intern->flags);
14751492
ZVAL_STRINGL(&zpath, intern->file_name, intern->file_name_len);
@@ -1698,13 +1715,17 @@ static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
16981715

16991716
if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
17001717
if (Z_ISUNDEF(iterator->current)) {
1701-
spl_filesystem_object_get_file_name(object);
1718+
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1719+
return NULL;
1720+
}
17021721
ZVAL_STRINGL(&iterator->current, object->file_name, object->file_name_len);
17031722
}
17041723
return &iterator->current;
17051724
} else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
17061725
if (Z_ISUNDEF(iterator->current)) {
1707-
spl_filesystem_object_get_file_name(object);
1726+
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1727+
return NULL;
1728+
}
17081729
spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
17091730
}
17101731
return &iterator->current;
@@ -1722,7 +1743,9 @@ static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval
17221743
if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
17231744
ZVAL_STRING(key, object->u.dir.entry.d_name);
17241745
} else {
1725-
spl_filesystem_object_get_file_name(object);
1746+
if (spl_filesystem_object_get_file_name(object) != SUCCESS) {
1747+
return;
1748+
}
17261749
ZVAL_STRINGL(key, object->file_name, object->file_name_len);
17271750
}
17281751
}

0 commit comments

Comments
 (0)