php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80960 opendir() warning wrong info when failed on Windows
Submitted: 2021-04-16 07:08 UTC Modified: 2021-04-16 12:57 UTC
From: dixyes at gmail dot com Assigned: cmb (profile)
Status: Closed Package: *Directory/Filesystem functions
PHP Version: Irrelevant OS: Windows
Private report: No CVE-ID: None
 [2021-04-16 07:08 UTC] dixyes at gmail dot com
Description:
------------
opendir("some bad path*"); will make strange warning.
maybe other functions will have similar behavior.
because virtual_file_ex() donot do SetLastError() when failed in some routines, this cause some error message become corrupt.

Test script:
---------------
<?php

$filename = __DIR__ . "/afile";

const MAXPATHLEN = 2048; // PHP_WIN32_IOUTIL_MAXPATHLEN on my system
$cwd = getcwd();
define("LEN", MAXPATHLEN - strlen($cwd));

function tellmefail(){
    // these should always tell me ERROR_INVALID_NAME "The filename, directory name, or volume label syntax is incorrect."
    // or same as error when path ending with space: ERROR_ACCESS_DENIED "Access is denied."
    // because the path is illegal,
    // at least, warnings should be always the same
    opendir("notexist*"); // * varient
    opendir("notexist?"); // ? varient
    opendir(str_pad("longname", LEN, "_"));
}

@rmdir($filename);
@unlink($filename);
@unlink($filename);
// should failed here with ERROR_FILE_NOT_FOUND

tellmefail();

@touch($filename);
@fopen($filename, "x");
// should failed here with ERROR_ALREADY_EXISTS

tellmefail();

@opendir($filename);
// should failed here with ERROR_DIRECTORY

tellmefail();

@unlink($filename);
@mkdir($filename);
@fopen($filename, "x");
// should failed here with ERROR_ACCESS_DENIED
// (on windows you need FILE_FLAG_BACKUP_SEMANTICS to open a dir via CreateFileW, otherwise failed with ERROR_ACCESS_DENIED)

tellmefail();

// clean up
@rmdir($filename);
@unlink($filename);


Expected result:
----------------
all the warnings should be the same,
if possible, use EINVAL/ERROR_INVALID_NAME/"The filename, directory name, or volume label syntax is incorrect." as error text(rather than EPERM like fopen pathes ending with space, that's really confusing).

Actual result:
--------------
all the warings become early error

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-04-16 12:57 UTC] cmb@php.net
-Summary: opendir() warning wrong info when failed on Windows ZTS build +Summary: opendir() warning wrong info when failed on Windows -Status: Open +Status: Verified -Assigned To: +Assigned To: cmb
 [2021-04-16 12:57 UTC] cmb@php.net
Oh, wow, that is pretty broken indeed.  Thanks for reporting!

One full example:

    <?php
    opendir("notexist*");
    ?>

    Warning: opendir(notexist*,notexist*): The operation completed successfully. (code: 0) in %s on line %d

    Warning: opendir(notexist*): failed to open dir: File exists in %s on line %dir

So two wrong error messages (neither did the operation complete
successfully, nor does the file exist), plus the confusing
duplicate filename in the first message.

And to clarify: this is not particularly related to ZTS, and for
too long filenames partially affects non Windows systems as well.

> rather than EPERM like fopen pathes ending with space, that's
> really confusing

I agree (besides that it's ENOENT), and only in some occassions,
for instance:

    <?php
    rename("foo ", "bar ");
    ?>

    Warning: rename(foo ,bar ): The filename, directory name, or volume label syntax is incorrect. (code: 123) in %s on line %d
 [2021-04-16 13:31 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Fix #80960: opendir() warning wrong info when failed on Windows
On GitHub:  https://github.com/php/php-src/pull/6872
Patch:      https://github.com/php/php-src/pull/6872.patch
 [2021-04-19 14:19 UTC] git@php.net
Automatic comment on behalf of cmb69
Revision: https://github.com/php/php-src/commit/ea3c992bfff30f4ca6b1b1feaea106683dd1397f
Log: Fix #80960: opendir() warning wrong info when failed on Windows
 [2021-04-19 14:19 UTC] git@php.net
-Status: Verified +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC