@@ -1859,6 +1859,11 @@ static zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *
1859
1859
}
1860
1860
/* }}} */
1861
1861
1862
+ static ZEND_COLD void spl_filesystem_file_cannot_read (spl_filesystem_object * intern )
1863
+ {
1864
+ zend_throw_exception_ex (spl_ce_RuntimeException , 0 , "Cannot read from file %s" , ZSTR_VAL (intern -> file_name ));
1865
+ }
1866
+
1862
1867
static zend_result spl_filesystem_file_read_ex (spl_filesystem_object * intern , bool silent , zend_long line_add , bool csv )
1863
1868
{
1864
1869
char * buf ;
@@ -1868,7 +1873,7 @@ static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bo
1868
1873
1869
1874
if (php_stream_eof (intern -> u .file .stream )) {
1870
1875
if (!silent ) {
1871
- zend_throw_exception_ex ( spl_ce_RuntimeException , 0 , "Cannot read from file %s" , ZSTR_VAL ( intern -> file_name ) );
1876
+ spl_filesystem_file_cannot_read ( intern );
1872
1877
}
1873
1878
return FAILURE ;
1874
1879
}
@@ -1923,10 +1928,10 @@ static bool is_line_empty(spl_filesystem_object *intern)
1923
1928
|| (current_line_len == 2 && current_line [0 ] == '\r' && current_line [1 ] == '\n' ))));
1924
1929
}
1925
1930
1926
- static zend_result spl_filesystem_file_read_csv (spl_filesystem_object * intern , char delimiter , char enclosure , int escape , zval * return_value ) /* {{{ */
1931
+ static zend_result spl_filesystem_file_read_csv (spl_filesystem_object * intern , char delimiter , char enclosure , int escape , zval * return_value , bool silent ) /* {{{ */
1927
1932
{
1928
1933
do {
1929
- zend_result ret = spl_filesystem_file_read (intern , /* silent */ true , /* csv */ true);
1934
+ zend_result ret = spl_filesystem_file_read (intern , silent , /* csv */ true);
1930
1935
if (ret != SUCCESS ) {
1931
1936
return ret ;
1932
1937
}
@@ -1952,19 +1957,21 @@ static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, c
1952
1957
}
1953
1958
/* }}} */
1954
1959
1955
- /* Call to this function reads a line in a "silent" fashion and does not throw an exception */
1956
- static zend_result spl_filesystem_file_read_line_ex (zval * this_ptr , spl_filesystem_object * intern ) /* {{{ */
1960
+ static zend_result spl_filesystem_file_read_line_ex (zval * this_ptr , spl_filesystem_object * intern , bool silent ) /* {{{ */
1957
1961
{
1958
1962
zval retval ;
1959
1963
1960
1964
/* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
1961
1965
if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_CSV )) {
1962
- return spl_filesystem_file_read_csv (intern , intern -> u .file .delimiter , intern -> u .file .enclosure , intern -> u .file .escape , NULL );
1966
+ return spl_filesystem_file_read_csv (intern , intern -> u .file .delimiter , intern -> u .file .enclosure , intern -> u .file .escape , NULL , silent );
1963
1967
}
1964
1968
if (intern -> u .file .func_getCurr -> common .scope != spl_ce_SplFileObject ) {
1965
1969
spl_filesystem_file_free_line (intern );
1966
1970
1967
1971
if (php_stream_eof (intern -> u .file .stream )) {
1972
+ if (!silent ) {
1973
+ spl_filesystem_file_cannot_read (intern );
1974
+ }
1968
1975
return FAILURE ;
1969
1976
}
1970
1977
zend_call_method_with_0_params (Z_OBJ_P (this_ptr ), Z_OBJCE_P (this_ptr ), & intern -> u .file .func_getCurr , "getCurrentLine" , & retval );
@@ -1988,18 +1995,17 @@ static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesys
1988
1995
zval_ptr_dtor (& retval );
1989
1996
return SUCCESS ;
1990
1997
} else {
1991
- return spl_filesystem_file_read (intern , /* silent */ true , /* csv */ false);
1998
+ return spl_filesystem_file_read (intern , silent , /* csv */ false);
1992
1999
}
1993
2000
} /* }}} */
1994
2001
1995
- /* Call to this function reads a line in a "silent" fashion and does not throw an exception */
1996
- static zend_result spl_filesystem_file_read_line (zval * this_ptr , spl_filesystem_object * intern ) /* {{{ */
2002
+ static zend_result spl_filesystem_file_read_line (zval * this_ptr , spl_filesystem_object * intern , bool silent ) /* {{{ */
1997
2003
{
1998
- zend_result ret = spl_filesystem_file_read_line_ex (this_ptr , intern );
2004
+ zend_result ret = spl_filesystem_file_read_line_ex (this_ptr , intern , silent );
1999
2005
2000
2006
while (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_SKIP_EMPTY ) && ret == SUCCESS && is_line_empty (intern )) {
2001
2007
spl_filesystem_file_free_line (intern );
2002
- ret = spl_filesystem_file_read_line_ex (this_ptr , intern );
2008
+ ret = spl_filesystem_file_read_line_ex (this_ptr , intern , silent );
2003
2009
}
2004
2010
2005
2011
return ret ;
@@ -2021,7 +2027,7 @@ static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *i
2021
2027
intern -> u .file .current_line_num = 0 ;
2022
2028
2023
2029
if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_AHEAD )) {
2024
- spl_filesystem_file_read_line (this_ptr , intern );
2030
+ spl_filesystem_file_read_line (this_ptr , intern , true );
2025
2031
}
2026
2032
} /* }}} */
2027
2033
@@ -2175,7 +2181,7 @@ PHP_METHOD(SplFileObject, current)
2175
2181
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED (intern );
2176
2182
2177
2183
if (!intern -> u .file .current_line && Z_ISUNDEF (intern -> u .file .current_zval )) {
2178
- spl_filesystem_file_read_line (ZEND_THIS , intern );
2184
+ spl_filesystem_file_read_line (ZEND_THIS , intern , true );
2179
2185
}
2180
2186
if (intern -> u .file .current_line && (!SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_CSV ) || Z_ISUNDEF (intern -> u .file .current_zval ))) {
2181
2187
RETURN_STRINGL (intern -> u .file .current_line , intern -> u .file .current_line_len );
@@ -2214,7 +2220,7 @@ PHP_METHOD(SplFileObject, next)
2214
2220
2215
2221
spl_filesystem_file_free_line (intern );
2216
2222
if (SPL_HAS_FLAG (intern -> flags , SPL_FILE_OBJECT_READ_AHEAD )) {
2217
- spl_filesystem_file_read_line (ZEND_THIS , intern );
2223
+ spl_filesystem_file_read_line (ZEND_THIS , intern , true );
2218
2224
}
2219
2225
intern -> u .file .current_line_num ++ ;
2220
2226
} /* }}} */
@@ -2332,7 +2338,7 @@ PHP_METHOD(SplFileObject, fgetcsv)
2332
2338
}
2333
2339
}
2334
2340
2335
- if (spl_filesystem_file_read_csv (intern , delimiter , enclosure , escape , return_value ) == FAILURE ) {
2341
+ if (spl_filesystem_file_read_csv (intern , delimiter , enclosure , escape , return_value , true ) == FAILURE ) {
2336
2342
RETURN_FALSE ;
2337
2343
}
2338
2344
}
@@ -2713,7 +2719,7 @@ PHP_METHOD(SplFileObject, seek)
2713
2719
spl_filesystem_file_rewind (ZEND_THIS , intern );
2714
2720
2715
2721
for (i = 0 ; i < line_pos ; i ++ ) {
2716
- if (spl_filesystem_file_read_line (ZEND_THIS , intern ) == FAILURE ) {
2722
+ if (spl_filesystem_file_read_line (ZEND_THIS , intern , true ) == FAILURE ) {
2717
2723
return ;
2718
2724
}
2719
2725
}
@@ -2733,8 +2739,12 @@ PHP_METHOD(SplFileObject, __toString)
2733
2739
2734
2740
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED (intern );
2735
2741
2736
- if (!intern -> u .file .current_line && Z_ISUNDEF (intern -> u .file .current_zval )) {
2737
- spl_filesystem_file_read_line (ZEND_THIS , intern );
2742
+ if (!intern -> u .file .current_line ) {
2743
+ ZEND_ASSERT (Z_ISUNDEF (intern -> u .file .current_zval ));
2744
+ zend_result result = spl_filesystem_file_read_line (ZEND_THIS , intern , false);
2745
+ if (UNEXPECTED (result != SUCCESS )) {
2746
+ RETURN_THROWS ();
2747
+ }
2738
2748
}
2739
2749
2740
2750
RETURN_STRINGL (intern -> u .file .current_line , intern -> u .file .current_line_len );
0 commit comments