php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47358 glob returns error, should be empty array()
Submitted: 2009-02-11 08:12 UTC Modified: 2013-08-19 12:26 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at guggemand dot dk Assigned: pajoye (profile)
Status: Closed Package: Safe Mode/open_basedir
PHP Version: 5.2.9RC1 OS: FreeBSD 7.1
Private report: No CVE-ID: None
 [2009-02-11 08:12 UTC] php at guggemand dot dk
Description:
------------
glob() cant be used directly in foreach when open_basedir is set.

i found #41655 which is about the change causing this, and a few other closed tickets with "not a bug" as answer.

But that change destroys foreach(glob("nonexistent*") as ... when using open_basedir, and the following comment in dir.c states that should be usable.

/* Some glob implementation simply return no data if no matches
   were found, others return the GLOB_NOMATCH error code.
   We don't want to treat GLOB_NOMATCH as an error condition
   so that PHP glob() behaves the same on both types of
   implementations and so that 'foreach (glob() as ...'
   can be used for simple glob() calls without further error
   checking.
*/

As far as i can tell the right thing to do would be to return an empty array both when using glob("/etc/hosts") and glob("/etc/nonexistent") when open_basedir is used. Or what am i missing?

Ive been using the following patch, and as far as i can tell its not possible to check if a file exists when open_basedir is used, and foreach(glob("nonext*")) works correctly.

http://gugge.dlx.dk/ting/php5-patch-dir.c

Reproduce code:
---------------
set open_basedir /documentroot

open /documentroot/test.php with the following content

<? var_dump(glob("nonexistent*")); ?>


Expected result:
----------------
array(0) {
}


Actual result:
--------------
bool(false) 

Patches

open_basedir_error_fix (last revision 2011-10-09 17:08 UTC by pajoye@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-02-11 14:20 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The function is documented to return FALSE on error. There is no problem 
here...
 [2009-02-12 10:52 UTC] php at guggemand dot dk
"an empty array if no file matched" is what i see in the manual.
but this returns error, and not an empty array()

glob("/path/allowd/in/open_basedir/nonexitentfile.*"); 

I can understand why a system glob call returning no files, and a call returning only nonallowed files has to be treated the same.

But im to dumb to see the logic in treating both as errors instead of "no files matched", especially because that breaks existing code.
And treating it as "no files matched" doesnt break anything.
Please enlighten me if im wrong, and ill put on my pointy hat and sit in the corner for the rest of the day.

But now i have link i can give the users telling me my servers doesn't work right. So i guess i can live with that.
 [2011-10-04 14:32 UTC] harald dot lapp at gmail dot com
It seems to me, that "glob" is returning false, even though the path i try to glob 
is valid compared to the "open_basdir" settings. Could you please have a further 
look at this issue?

(tried this with php 5.3.6 and php 5.3.8, btw.)
 [2011-10-09 16:04 UTC] tyrael@php.net
-Status: Bogus +Status: Open
 [2011-10-09 16:39 UTC] pajoye@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: pajoye
 [2011-10-09 16:39 UTC] pajoye@php.net
I agree, there is no error here but a wrong test. I will fix it soonish, checking 
the logic in there.
 [2011-10-09 16:50 UTC] pajoye@php.net
The following patch has been added/updated:

Patch Name: open_basedir_error_fix
Revision:   1318179021
URL:        https://bugs.php.net/patch-display.php?bug=47358&patch=open_basedir_error_fix&revision=1318179021
 [2011-10-09 16:51 UTC] pajoye@php.net
Please try using this patch (against 5.4 but should work against 5.3). I will 
remove the warning too to make it works like the other call (was just to ease the 
test).
 [2011-10-09 17:08 UTC] pajoye@php.net
The following patch has been added/updated:

Patch Name: open_basedir_error_fix
Revision:   1318180102
URL:        https://bugs.php.net/patch-display.php?bug=47358&patch=open_basedir_error_fix&revision=1318180102
 [2012-11-09 14:09 UTC] manuel-php at mausz dot at
Hi Pierre!

Please merge your patch asap to php.
It works as expected and will fix the following inconsistency:
$ php -dopen_basedir=/ -r 'var_dump(glob("/does/not/exist"));'
bool(false)
$ php -r 'var_dump(glob("/does/not/exist"));'
array(0) {
}

Something to consider is updating the docs to mention returning false if open_basedir restriction is in effect.
 [2013-01-08 14:21 UTC] pajoye@php.net
-Status: Assigned +Status: Closed
 [2013-01-08 14:21 UTC] pajoye@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2fb7cd30c59345838588ba89ad8b05cc4c34d6dd
Log: - fix bug #47358, glob returns error, should be empty array()
 [2013-01-08 14:21 UTC] pajoye@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=50ceeb9f750cecedc483ffc3d3e65a3324e21866
Log: - fix bug #47358, glob returns error, should be empty array()
 [2013-08-19 08:24 UTC] ab@php.net
-Status: Closed +Status: Re-Opened
 [2013-08-19 08:24 UTC] ab@php.net
There are some cases with the platform difference

windows

x64\Debug_TS\php -n -d open_basedir=C:\php-sdk\phpmaster\vc11\x64\php-src -r 
"var_dump(glob('C:\php-sdk\phpmaster\vc11\x64\php-src\*.none'));"
bool(false)

x64\Debug_TS\php -n -r "var_dump(glob('C:\php-sdk\phpmaster\vc11\x64\php-src\*.none'));"
array(0) {
}

linux

 sapi/cli/php -n -d open_basedir=/home/anatol/dws/src/php-master-ts -r 
'var_dump(glob("/home/anatol/dws/src/php-master-ts/*.none"));'
array(0) {
}

sapi/cli/php -n -r 'var_dump(glob("/home/anatol/dws/src/php-master-ts/*.none"));'
array(0) {
}


The cause of this platform difference is that here 
http://lxr.php.net/xref/PHP_TRUNK/ext/standard/dir.c#495 php_check_open_basedir_ex() 
subsequently calls virtual_file_ex() 
http://lxr.php.net/xref/PHP_TRUNK/TSRM/tsrm_virtual_cwd.c#1241 and if the pattern 
contains '*' or '?', php_check_open_basedir_ex() will return -1 on windows.

I've discovered while testing this PR https://github.com/php/php-src/pull/398 which had 
the same issue.
 [2013-08-19 08:28 UTC] ab@php.net
Here's a testcase i wrote to ensure it does right across platforms

--TEST--
Test glob() function: ensure no platform difference
--FILE--
<?php
$path = dirname(__FILE__);

ini_set('open_basedir', NULL);
var_dump(glob("$path/*.none"));

ini_set('open_basedir', $path);
var_dump(glob("$path/*.none"));

?>
==DONE==
--EXPECT--
array(0) {
}
array(0) {
}
==DONE==
 [2013-08-19 09:36 UTC] ab@php.net
I can see the patch commited at 2013-01-08 

http://git.php.net/?p=php-src.git;a=history;f=ext/standard/dir.c;hb=HEAD

However the 5.5 branch doesn't have it, it seems to be created before 2013-01-08

http://git.php.net/?p=php-src.git;a=history;f=ext/standard/dir.c;hb=refs/heads/PHP-5.5

This way it works in PHP <= 5.5 is returning false or array() on a nonexistent path depending on whether 
basedir was set or not. In master where the patch is present it returns an empty array disregarding 
openbasedir setting on linux, but still has difference on windows when the path contains '*' or '?'.

I've pushed the test splitted in 5.4/5.5 indicating the 'is state'

http://git.php.net/?p=php-
src.git;a=blob_plain;f=ext/standard/tests/file/glob_variation3.phpt;hb=refs/heads/PHP-5.4

and into master:

http://git.php.net/?p=php-
src.git;a=blob_plain;f=ext/standard/tests/file/glob_variation3.phpt;hb=refs/heads/master

On master it'll fail on windows because of the platform difference in virtual_file_ex() mentioned above.
 [2013-08-19 12:26 UTC] ab@php.net
-Status: Re-Opened +Status: Closed
 [2013-08-19 12:26 UTC] ab@php.net
fixed with f4df40108be641a4167f6f6c1c3989958dda438a
 [2013-11-17 09:31 UTC] laruence@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=2fb7cd30c59345838588ba89ad8b05cc4c34d6dd
Log: - fix bug #47358, glob returns error, should be empty array()
 [2013-11-17 09:31 UTC] laruence@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=50ceeb9f750cecedc483ffc3d3e65a3324e21866
Log: - fix bug #47358, glob returns error, should be empty array()
 [2014-08-15 00:43 UTC] stas@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=481c4715d4f4c77293d65216324215f671954660
Log: - fix bug #47358, glob returns error, should be empty array()
 [2014-08-15 04:58 UTC] dmitry@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=481c4715d4f4c77293d65216324215f671954660
Log: - fix bug #47358, glob returns error, should be empty array()
 [2014-10-07 23:13 UTC] stas@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=481c4715d4f4c77293d65216324215f671954660
Log: - fix bug #47358, glob returns error, should be empty array()
 [2014-10-07 23:24 UTC] stas@php.net
Automatic comment on behalf of pierre.php@gmail.com
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=481c4715d4f4c77293d65216324215f671954660
Log: - fix bug #47358, glob returns error, should be empty array()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC