Skip to content

Commit 2a5092e

Browse files
committed
Use macro to check if object is initialized
1 parent d893fd4 commit 2a5092e

File tree

1 file changed

+24
-61
lines changed

1 file changed

+24
-61
lines changed

ext/spl/spl_directory.c

Lines changed: 24 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ PHPAPI zend_class_entry *spl_ce_GlobIterator;
5959
PHPAPI zend_class_entry *spl_ce_SplFileObject;
6060
PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
6161

62+
// TODO Use standard Error
63+
#define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED() \
64+
if (!intern->u.file.stream) { \
65+
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized"); \
66+
RETURN_THROWS(); \
67+
}
68+
6269
static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
6370
{
6471
if (intern->u.file.current_line) {
@@ -2019,7 +2026,7 @@ static int spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object
20192026

20202027
static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
20212028
{
2022-
if(!intern->u.file.stream) {
2029+
if (!intern->u.file.stream) {
20232030
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
20242031
return;
20252032
}
@@ -2147,10 +2154,7 @@ PHP_METHOD(SplFileObject, eof)
21472154
RETURN_THROWS();
21482155
}
21492156

2150-
if(!intern->u.file.stream) {
2151-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2152-
RETURN_THROWS();
2153-
}
2157+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
21542158

21552159
RETURN_BOOL(php_stream_eof(intern->u.file.stream));
21562160
} /* }}} */
@@ -2183,10 +2187,7 @@ PHP_METHOD(SplFileObject, fgets)
21832187
RETURN_THROWS();
21842188
}
21852189

2186-
if(!intern->u.file.stream) {
2187-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2188-
RETURN_THROWS();
2189-
}
2190+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
21902191

21912192
if (spl_filesystem_file_read(intern, 0) == FAILURE) {
21922193
RETURN_FALSE;
@@ -2203,10 +2204,7 @@ PHP_METHOD(SplFileObject, current)
22032204
RETURN_THROWS();
22042205
}
22052206

2206-
if(!intern->u.file.stream) {
2207-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2208-
RETURN_THROWS();
2209-
}
2207+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
22102208

22112209
if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
22122210
spl_filesystem_file_read_line(ZEND_THIS, intern, 1);
@@ -2337,10 +2335,7 @@ PHP_METHOD(SplFileObject, fgetcsv)
23372335

23382336
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == SUCCESS) {
23392337

2340-
if(!intern->u.file.stream) {
2341-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2342-
RETURN_THROWS();
2343-
}
2338+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
23442339

23452340
switch(ZEND_NUM_ARGS())
23462341
{
@@ -2527,10 +2522,7 @@ PHP_METHOD(SplFileObject, flock)
25272522
RETURN_THROWS();
25282523
}
25292524

2530-
if(!intern->u.file.stream) {
2531-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2532-
RETURN_THROWS();
2533-
}
2525+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
25342526

25352527
act = operation & PHP_LOCK_UN;
25362528
// TODO doesn't this fail if operation is a bitmask with LOCK_NB?
@@ -2561,10 +2553,7 @@ PHP_METHOD(SplFileObject, fflush)
25612553
{
25622554
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
25632555

2564-
if(!intern->u.file.stream) {
2565-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2566-
RETURN_THROWS();
2567-
}
2556+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
25682557

25692558
RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
25702559
} /* }}} */
@@ -2575,10 +2564,7 @@ PHP_METHOD(SplFileObject, ftell)
25752564
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
25762565
zend_long ret;
25772566

2578-
if(!intern->u.file.stream) {
2579-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2580-
RETURN_THROWS();
2581-
}
2567+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
25822568

25832569
ret = php_stream_tell(intern->u.file.stream);
25842570

@@ -2599,10 +2585,7 @@ PHP_METHOD(SplFileObject, fseek)
25992585
RETURN_THROWS();
26002586
}
26012587

2602-
if(!intern->u.file.stream) {
2603-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2604-
RETURN_THROWS();
2605-
}
2588+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
26062589

26072590
spl_filesystem_file_free_line(intern);
26082591
RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, (int)whence));
@@ -2615,10 +2598,7 @@ PHP_METHOD(SplFileObject, fgetc)
26152598
char buf[2];
26162599
int result;
26172600

2618-
if(!intern->u.file.stream) {
2619-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2620-
RETURN_THROWS();
2621-
}
2601+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
26222602

26232603
spl_filesystem_file_free_line(intern);
26242604

@@ -2642,10 +2622,7 @@ PHP_METHOD(SplFileObject, fpassthru)
26422622
{
26432623
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
26442624

2645-
if(!intern->u.file.stream) {
2646-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2647-
RETURN_THROWS();
2648-
}
2625+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
26492626

26502627
RETURN_LONG(php_stream_passthru(intern->u.file.stream));
26512628
} /* }}} */
@@ -2662,10 +2639,7 @@ PHP_METHOD(SplFileObject, fscanf)
26622639
RETURN_THROWS();
26632640
}
26642641

2665-
if (!intern->u.file.stream) {
2666-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2667-
RETURN_THROWS();
2668-
}
2642+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
26692643

26702644
/* Get next line */
26712645
if (spl_filesystem_file_read(intern, 0) == FAILURE) {
@@ -2693,10 +2667,7 @@ PHP_METHOD(SplFileObject, fwrite)
26932667
RETURN_THROWS();
26942668
}
26952669

2696-
if(!intern->u.file.stream) {
2697-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2698-
RETURN_THROWS();
2699-
}
2670+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
27002671

27012672
if (ZEND_NUM_ARGS() > 1) {
27022673
if (length >= 0) {
@@ -2727,10 +2698,7 @@ PHP_METHOD(SplFileObject, fread)
27272698
RETURN_THROWS();
27282699
}
27292700

2730-
if(!intern->u.file.stream) {
2731-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2732-
RETURN_THROWS();
2733-
}
2701+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
27342702

27352703
if (length <= 0) {
27362704
php_error_docref(NULL, E_WARNING, "Length parameter must be greater than 0");
@@ -2779,10 +2747,7 @@ PHP_METHOD(SplFileObject, ftruncate)
27792747
RETURN_THROWS();
27802748
}
27812749

2782-
if(!intern->u.file.stream) {
2783-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2784-
RETURN_THROWS();
2785-
}
2750+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
27862751

27872752
if (!php_stream_truncate_supported(intern->u.file.stream)) {
27882753
zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't truncate file %s", intern->file_name);
@@ -2801,10 +2766,8 @@ PHP_METHOD(SplFileObject, seek)
28012766
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
28022767
RETURN_THROWS();
28032768
}
2804-
if(!intern->u.file.stream) {
2805-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2806-
RETURN_THROWS();
2807-
}
2769+
2770+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
28082771

28092772
if (line_pos < 0) {
28102773
zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't seek file %s to negative line " ZEND_LONG_FMT, intern->file_name, line_pos);

0 commit comments

Comments
 (0)