Skip to content

Commit

Permalink
Use normal error in SPL for uninitialized objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Sep 15, 2020
1 parent 9affbef commit b620733
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 49 deletions.
11 changes: 4 additions & 7 deletions ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ PHPAPI zend_class_entry *spl_ce_GlobIterator;
PHPAPI zend_class_entry *spl_ce_SplFileObject;
PHPAPI zend_class_entry *spl_ce_SplTempFileObject;

// TODO Use standard Error
#define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(spl_filesystem_object_pointer) \
if (!(spl_filesystem_object_pointer)->u.file.stream) { \
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized"); \
zend_throw_error(NULL, "Object not initialized"); \
RETURN_THROWS(); \
}

Expand Down Expand Up @@ -209,7 +208,7 @@ static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *int
case SPL_FS_INFO:
case SPL_FS_FILE:
if (!intern->file_name) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
zend_throw_error(NULL, "Object not initialized");
return FAILURE;
}
break;
Expand Down Expand Up @@ -1393,9 +1392,7 @@ PHP_METHOD(SplFileInfo, __debugInfo)
/* {{{ */
PHP_METHOD(SplFileInfo, _bad_state_ex)
{
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The parent constructor was not called: the object is in an "
"invalid state ");
zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
}
/* }}} */

Expand Down Expand Up @@ -2021,7 +2018,7 @@ static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object
static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
{
if (!intern->u.file.stream) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
zend_throw_error(NULL, "Object not initialized");
return;
}
if (-1 == php_stream_rewind(intern->u.file.stream)) {
Expand Down
24 changes: 8 additions & 16 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
do { \
spl_dual_it_object *it = Z_SPLDUAL_IT_P(objzval); \
if (it->dit_type == DIT_Unknown) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
RETURN_THROWS(); \
} \
(var) = it; \
Expand All @@ -135,8 +134,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
#define SPL_FETCH_SUB_ELEMENT(var, object, element) \
do { \
if(!(object)->iterators) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
return; \
} \
(var) = (object)->iterators[(object)->level].element; \
Expand All @@ -145,8 +143,7 @@ static inline spl_recursive_it_object *spl_recursive_it_from_obj(zend_object *ob
#define SPL_FETCH_SUB_ELEMENT_ADDR(var, object, element) \
do { \
if(!(object)->iterators) { \
zend_throw_exception_ex(spl_ce_LogicException, 0, \
"The object is in an invalid state as the parent constructor was not called"); \
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called"); \
RETURN_THROWS(); \
} \
(var) = &(object)->iterators[(object)->level].element; \
Expand Down Expand Up @@ -698,8 +695,7 @@ PHP_METHOD(RecursiveIteratorIterator, getSubIterator)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down Expand Up @@ -1059,8 +1055,7 @@ PHP_METHOD(RecursiveTreeIterator, getPrefix)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down Expand Up @@ -1092,8 +1087,7 @@ PHP_METHOD(RecursiveTreeIterator, getEntry)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand All @@ -1110,8 +1104,7 @@ PHP_METHOD(RecursiveTreeIterator, getPostfix)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand All @@ -1131,8 +1124,7 @@ PHP_METHOD(RecursiveTreeIterator, current)
}

if(!object->iterators) {
zend_throw_exception_ex(spl_ce_LogicException, 0,
"The object is in an invalid state as the parent constructor was not called");
zend_throw_error(NULL, "The object is in an invalid state as the parent constructor was not called");
RETURN_THROWS();
}

Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/bug54281.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ foreach($it as $k=>$v) { }

?>
--EXPECTF--
Fatal error: Uncaught LogicException: The object is in an invalid state as the parent constructor was not called in %s:%d
Fatal error: Uncaught Error: The object is in an invalid state as the parent constructor was not called in %s:%d
Stack trace:
#0 %s%ebug54281.php(8): RecursiveIteratorIterator->rewind()
#1 {main}
Expand Down
34 changes: 17 additions & 17 deletions ext/spl/tests/bug54384.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function test($f) {
try {
$f();
echo "ran normally (unexpected)\n\n";
} catch (LogicException $e) {
echo "exception (expected)\n";
} catch (\Error $e) {
echo "Error (expected)\n";
}
}

Expand Down Expand Up @@ -154,18 +154,18 @@ echo $a,"\n";
} );
?>
--EXPECT--
IteratorIterator... exception (expected)
FilterIterator... exception (expected)
RecursiveFilterIterator... exception (expected)
ParentIterator... exception (expected)
LimitIterator... exception (expected)
CachingIterator... exception (expected)
RecursiveCachingIterator... exception (expected)
NoRewindIterator... exception (expected)
RegexIterator... exception (expected)
RecursiveRegexIterator... exception (expected)
GlobIterator... exception (expected)
SplFileObject... exception (expected)
SplTempFileObject... exception (expected)
AppendIterator... exception (expected)
InfiniteIterator... exception (expected)
IteratorIterator... Error (expected)
FilterIterator... Error (expected)
RecursiveFilterIterator... Error (expected)
ParentIterator... Error (expected)
LimitIterator... Error (expected)
CachingIterator... Error (expected)
RecursiveCachingIterator... Error (expected)
NoRewindIterator... Error (expected)
RegexIterator... Error (expected)
RecursiveRegexIterator... Error (expected)
GlobIterator... Error (expected)
SplFileObject... Error (expected)
SplTempFileObject... Error (expected)
AppendIterator... Error (expected)
InfiniteIterator... Error (expected)
12 changes: 6 additions & 6 deletions ext/spl/tests/bug55701.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function testBaseClass($f) {
} catch (RuntimeException $e) {
// Throwing a RuntimeException is the correct behaviour for some methods
echo "ran normally (expected)\n";
} catch (LogicException $e) {
} catch (\Error $e) {
// Throwing a LogicException is not correct
echo "threw LogicException (unexpected)\n";
}
Expand All @@ -27,8 +27,8 @@ function testChildClass($f) {
try {
$f();
echo "didn't throw (unexpected)\n";
} catch (LogicException $e) {
echo "threw LogicException (expected)\n";
} catch (\Error $e) {
echo "threw Error (expected)\n";
} catch (Exception $e) {
echo "threw other exception (unexpected)\n";
}
Expand Down Expand Up @@ -330,6 +330,6 @@ non-empty GlobIterator... ran normally (expected)
======================= test there are no regressions =======================
SplFileObject existent file... ran normally (expected)
SplFileObject non-existent file... ran normally (expected)
extends GlobIterator... threw LogicException (expected)
extends SplFileObject... threw LogicException (expected)
extends SplTempFileObject... threw LogicException (expected)
extends GlobIterator... threw Error (expected)
extends SplFileObject... threw Error (expected)
extends SplTempFileObject... threw Error (expected)
2 changes: 1 addition & 1 deletion ext/spl/tests/bug79710.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Run::main();

?>
--EXPECTF--
Fatal error: Uncaught RuntimeException: Object not initialized in %s:%d
Fatal error: Uncaught Error: Object not initialized in %s:%d
Stack trace:
#0 %s(%d): SplFileObject->flock(2)
#1 [internal function]: Target->__destruct()
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/tests/iterator_031.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ try
{
$ap->append($it);
}
catch(LogicException $e)
catch(\Error $e)
{
echo $e->getMessage() . "\n";
}
Expand Down

0 comments on commit b620733

Please sign in to comment.