Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use FFI::load on CRLF header file with apache2handler #14215

Closed
DoorLocker1141 opened this issue May 13, 2024 · 2 comments
Closed

Cannot use FFI::load on CRLF header file with apache2handler #14215

DoorLocker1141 opened this issue May 13, 2024 · 2 comments

Comments

@DoorLocker1141
Copy link
DoorLocker1141 commented May 13, 2024

Description

With this PHP file:

<?php
header('Content-Type: text/plain');

printf("php version : %s\n", phpversion());
printf("PHP_SAPI : %s\n", PHP_SAPI);

$header_path = __DIR__.'\\header.h';

$ffi = FFI::load($header_path);

$error_code = $ffi->GetLastError();

printf("error code is %d\n", $error_code);

And this header file (saved with CRLF line endings) :

#define FFI_LIB "Kernel32.dll"




typedef unsigned long DWORD;
DWORD GetLastError(void);



I get this output:

php version : 8.1.27
PHP_SAPI : apache2handler

Fatal error: Uncaught FFI\Exception: Failed loading 'header.h', cannot read_file in bug_ffi.php:10
Stack trace:
#0bug_ffi.php(10): FFI::load('hea...')
#1 {main}
  thrown in bug_ffi.php on line 10

But if I launch the script directly, I get this :

php .\bug_ffi.php
php version : 8.1.27
PHP_SAPI : cli
error code is 0

What seems to be happening here is that the zend_ffi_load function, which is used by FFI::load(), use stat to get the header size, then use open and read to read the file.
Then it checks if the byte amount returned from read is equal to the file size from stat.
If it's not, then it throws an error Failed loading '%s', cannot read_file.
The problem is that the file was opened in O_TEXT mode, so line endings were converted from CRLF to LF, and the byte amount returned is lower than expected.

I've checked PHP's source code, and it seems that the CGI and CLI main functions have some code to set _fmode to _O_BINARY, which is probably why I don't get the error when I run the script directly.

The sapi/apache2handler/ folder doesn't seems to have any mention of _fmode.

PHP Version

PHP 8.1.27
Edit : It also happens on PHP 8.3.7.

Operating System

Windows 10
Note that I'm using the WAMP version of PHP.

@DoorLocker1141
Copy link
Author

Sorry, I accidentally created the issue before writing the description.

@nielsdos
Copy link
Member

8.1 is closed for bugfixes, so lowest branch we can fix this in is 8.2. I'll have a look today.

nielsdos added a commit to nielsdos/php-src that referenced this issue May 13, 2024
…2handler

Some modules may reset _fmode, which causes mangling of line endings.
Always be explicit like we do in other places where the native open call
is used.
nielsdos added a commit that referenced this issue May 14, 2024
* PHP-8.2:
  Fix GH-14215: Cannot use FFI::load on CRLF header file with apache2handler
nielsdos added a commit that referenced this issue May 14, 2024
* PHP-8.3:
  Fix GH-14215: Cannot use FFI::load on CRLF header file with apache2handler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants