Skip to content

Commit 4d94716

Browse files
committed
Fix bug #79075: FFI header parser chokes on comments
The directives for FFI should be first in the file, which is fine, however sometimes there can be comments or whitespace before or between these defines. One practical example is for license information or when a user adds newlines "by accident". In these cases, it's quite confusing that the directives do not work properly. To solve this, make the zend_ffi_parse_directives() aware of comments.
1 parent 1800cad commit 4d94716

File tree

4 files changed

+104
-66
lines changed

4 files changed

+104
-66
lines changed

ext/ffi/ffi.c

Lines changed: 64 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -4958,107 +4958,105 @@ ZEND_METHOD(FFI_CType, getFuncParameterType) /* {{{ */
49584958
}
49594959
/* }}} */
49604960

4961+
static bool zend_ffi_is_horizontal_ws(char p)
4962+
{
4963+
return p == ' ' || p == '\t';
4964+
}
4965+
4966+
static char *zend_ffi_skip_ws_and_comments(char *p, bool allow_standalone_newline)
4967+
{
4968+
while (true) {
4969+
if (zend_ffi_is_horizontal_ws(*p)) {
4970+
p++;
4971+
} else if (allow_standalone_newline && (*p == '\r' || *p == '\n' || *p == '\f' || *p == '\v')) {
4972+
p++;
4973+
} else if (allow_standalone_newline && *p == '/' && p[1] == '/') {
4974+
p += 2;
4975+
while (*p && *p != '\r' && *p != '\n') {
4976+
p++;
4977+
}
4978+
} else if (*p == '/' && p[1] == '*') {
4979+
p += 2;
4980+
while (*p && (*p != '*' || p[1] != '/')) {
4981+
p++;
4982+
}
4983+
p += 2;
4984+
} else {
4985+
break;
4986+
}
4987+
}
4988+
4989+
return p;
4990+
}
4991+
49614992
static char *zend_ffi_parse_directives(const char *filename, char *code_pos, char **scope_name, char **lib, bool preload) /* {{{ */
49624993
{
49634994
char *p;
49644995

4996+
code_pos = zend_ffi_skip_ws_and_comments(code_pos, true);
4997+
49654998
*scope_name = NULL;
49664999
*lib = NULL;
49675000
while (*code_pos == '#') {
4968-
if (strncmp(code_pos, "#define FFI_SCOPE", sizeof("#define FFI_SCOPE") - 1) == 0
4969-
&& (code_pos[sizeof("#define FFI_SCOPE") - 1] == ' '
4970-
|| code_pos[sizeof("#define FFI_SCOPE") - 1] == '\t')) {
4971-
p = code_pos + sizeof("#define FFI_SCOPE");
4972-
while (*p == ' ' || *p == '\t') {
4973-
p++;
4974-
}
4975-
if (*p != '"') {
4976-
if (preload) {
4977-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_SCOPE define", filename);
4978-
} else {
4979-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_SCOPE define", filename);
4980-
}
4981-
return NULL;
4982-
}
4983-
p++;
4984-
if (*scope_name) {
4985-
if (preload) {
4986-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', FFI_SCOPE defined twice", filename);
4987-
} else {
4988-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', FFI_SCOPE defined twice", filename);
4989-
}
4990-
return NULL;
4991-
}
4992-
*scope_name = p;
4993-
while (1) {
4994-
if (*p == '\"') {
4995-
*p = 0;
5001+
if (strncmp(code_pos, ZEND_STRL("#define")) == 0
5002+
&& zend_ffi_is_horizontal_ws(code_pos[sizeof("#define") - 1])) {
5003+
p = zend_ffi_skip_ws_and_comments(code_pos + sizeof("#define"), false);
5004+
5005+
char **target = NULL;
5006+
const char *target_name = NULL;
5007+
if (strncmp(p, ZEND_STRL("FFI_SCOPE")) == 0
5008+
&& zend_ffi_is_horizontal_ws(p[sizeof("FFI_SCOPE") - 1])) {
5009+
p = zend_ffi_skip_ws_and_comments(p + sizeof("FFI_SCOPE"), false);
5010+
target = scope_name;
5011+
target_name = "FFI_SCOPE";
5012+
} else if (strncmp(p, ZEND_STRL("FFI_LIB")) == 0
5013+
&& zend_ffi_is_horizontal_ws(p[sizeof("FFI_LIB") - 1])) {
5014+
p = zend_ffi_skip_ws_and_comments(p + sizeof("FFI_LIB"), false);
5015+
target = lib;
5016+
target_name = "FFI_LIB";
5017+
} else {
5018+
while (*p && *p != '\n' && *p != '\r') {
49965019
p++;
4997-
break;
4998-
} else if (*p <= ' ') {
4999-
if (preload) {
5000-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_SCOPE define", filename);
5001-
} else {
5002-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_SCOPE define", filename);
5003-
}
5004-
return NULL;
50055020
}
5006-
p++;
5007-
}
5008-
while (*p == ' ' || *p == '\t') {
5009-
p++;
5010-
}
5011-
while (*p == '\r' || *p == '\n') {
5012-
p++;
5013-
}
5014-
code_pos = p;
5015-
} else if (strncmp(code_pos, "#define FFI_LIB", sizeof("#define FFI_LIB") - 1) == 0
5016-
&& (code_pos[sizeof("#define FFI_LIB") - 1] == ' '
5017-
|| code_pos[sizeof("#define FFI_LIB") - 1] == '\t')) {
5018-
p = code_pos + sizeof("#define FFI_LIB");
5019-
while (*p == ' ' || *p == '\t') {
5020-
p++;
5021+
code_pos = zend_ffi_skip_ws_and_comments(p, true);
5022+
continue;
50215023
}
5024+
50225025
if (*p != '"') {
50235026
if (preload) {
5024-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_LIB define", filename);
5027+
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad %s define", filename, target_name);
50255028
} else {
5026-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_LIB define", filename);
5029+
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad %s define", filename, target_name);
50275030
}
50285031
return NULL;
50295032
}
50305033
p++;
5031-
if (*lib) {
5034+
if (*target) {
50325035
if (preload) {
5033-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', FFI_LIB defined twice", filename);
5036+
zend_error(E_WARNING, "FFI: failed pre-loading '%s', %s defined twice", filename, target_name);
50345037
} else {
5035-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', FFI_LIB defined twice", filename);
5038+
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', %s defined twice", filename, target_name);
50365039
}
50375040
return NULL;
50385041
}
5039-
*lib = p;
5042+
*target = p;
50405043
while (1) {
50415044
if (*p == '\"') {
50425045
*p = 0;
50435046
p++;
50445047
break;
50455048
} else if (*p <= ' ') {
50465049
if (preload) {
5047-
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_LIB define", filename);
5050+
zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad %s define", filename, target_name);
50485051
} else {
5049-
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_LIB define", filename);
5052+
zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad %s define", filename, target_name);
50505053
}
50515054
return NULL;
50525055
}
50535056
p++;
50545057
}
5055-
while (*p == ' ' || *p == '\t') {
5056-
p++;
5057-
}
5058-
while (*p == '\r' || *p == '\n') {
5059-
p++;
5060-
}
5061-
code_pos = p;
5058+
5059+
code_pos = zend_ffi_skip_ws_and_comments(p, true);
50625060
} else {
50635061
break;
50645062
}

ext/ffi/tests/bug79075.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* Multiline comment
3+
*/
4+
// whitespace line
5+
6+
#define ignore_this_line 1
7+
//
8+
#define /* inline */ FFI_SCOPE /* multi-
9+
line */ "bug79075" /* end
10+
*/
11+
12+
int printf(const char *format, ...);

ext/ffi/tests/bug79075.inc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
FFI::load(__DIR__ . "/bug79075.h");

ext/ffi/tests/bug79075.phpt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #79075 (FFI header parser chokes on comments)
3+
--EXTENSIONS--
4+
ffi
5+
opcache
6+
posix
7+
--SKIPIF--
8+
<?php
9+
if (substr(PHP_OS, 0, 3) == 'WIN') die('skip not for Windows');
10+
if (posix_geteuid() == 0) die('skip Cannot run test as root.');
11+
?>
12+
--INI--
13+
ffi.enable=1
14+
opcache.enable=1
15+
opcache.enable_cli=1
16+
opcache.optimization_level=-1
17+
opcache.preload={PWD}/bug79075.inc
18+
opcache.file_cache_only=0
19+
--FILE--
20+
<?php
21+
$ffi = FFI::scope("bug79075");
22+
$ffi->printf("Hello World from %s!\n", "PHP");
23+
?>
24+
--EXPECT--
25+
Hello World from PHP!

0 commit comments

Comments
 (0)