Skip to content

Commit e26c8da

Browse files
author
Dik Takken
committed
Make handling of NULL bytes in file paths more consistent (WIP)
Not all extensions consistently throw exceptions when the user passes a path name containing null bytes. Also, some extensions would throw a ValueError while others would throw a TypeError. Error messages also varied. Now a ValueError is thrown after all failed path checks, at least for as far as these occur in functions that are exposed to userland.
1 parent 3d14880 commit e26c8da

File tree

3 files changed

+16
-11
lines changed

3 files changed

+16
-11
lines changed

ext/dom/document.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ static xmlDocPtr dom_document_parser(zval *id, int mode, char *source, size_t so
11971197
if (mode == DOM_LOAD_FILE) {
11981198
char *file_dest;
11991199
if (CHECK_NULL_PATH(source, source_len)) {
1200+
zend_value_error("Path to document must not contain any null bytes");
12001201
return NULL;
12011202
}
12021203
file_dest = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN);
@@ -1319,8 +1320,12 @@ static void dom_parse_document(INTERNAL_FUNCTION_PARAMETERS, int mode) {
13191320

13201321
newdoc = dom_document_parser(id, mode, source, source_len, options);
13211322

1322-
if (!newdoc)
1323+
if (!newdoc) {
1324+
if (EG(exception)) {
1325+
RETURN_THROWS();
1326+
}
13231327
RETURN_FALSE;
1328+
}
13241329

13251330
if (id != NULL) {
13261331
intern = Z_DOMOBJ_P(id);
@@ -1633,8 +1638,8 @@ static void _dom_document_schema_validate(INTERNAL_FUNCTION_PARAMETERS, int type
16331638
switch (type) {
16341639
case DOM_LOAD_FILE:
16351640
if (CHECK_NULL_PATH(source, source_len)) {
1636-
php_error_docref(NULL, E_WARNING, "Invalid Schema file source");
1637-
RETURN_FALSE;
1641+
zend_argument_value_error(1, "must not contain any null bytes");
1642+
RETURN_THROWS();
16381643
}
16391644
valid_file = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN);
16401645
if (!valid_file) {
@@ -1734,8 +1739,8 @@ static void _dom_document_relaxNG_validate(INTERNAL_FUNCTION_PARAMETERS, int typ
17341739
switch (type) {
17351740
case DOM_LOAD_FILE:
17361741
if (CHECK_NULL_PATH(source, source_len)) {
1737-
php_error_docref(NULL, E_WARNING, "Invalid RelaxNG file source");
1738-
RETURN_FALSE;
1742+
zend_argument_value_error(1, "must not contain any null bytes");
1743+
RETURN_THROWS();
17391744
}
17401745
valid_file = _dom_get_valid_file_path(source, resolved_path, MAXPATHLEN);
17411746
if (!valid_file) {
@@ -1834,8 +1839,8 @@ static void dom_load_html(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
18341839

18351840
if (mode == DOM_LOAD_FILE) {
18361841
if (CHECK_NULL_PATH(source, source_len)) {
1837-
php_error_docref(NULL, E_WARNING, "Invalid file source");
1838-
RETURN_FALSE;
1842+
zend_argument_value_error(1, "must not contain any null bytes");
1843+
RETURN_THROWS();
18391844
}
18401845
ctxt = htmlCreateFileParserCtxt(source, NULL);
18411846
} else {

ext/fileinfo/fileinfo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
444444
goto clean;
445445
}
446446
if (CHECK_NULL_PATH(buffer, buffer_len)) {
447-
zend_argument_type_error(1, "must not contain null bytes");
447+
zend_argument_type_error(1, "must not contain any null bytes");
448448
goto clean;
449449
}
450450

ext/sqlite3/sqlite3.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,7 +1248,7 @@ PHP_METHOD(SQLite3, openBlob)
12481248
}
12491249

12501250
if (ZEND_NUM_ARGS() >= 4 && CHECK_NULL_PATH(dbname, dbname_len)) {
1251-
zend_argument_type_error(4, "must not contain null bytes");
1251+
zend_argument_value_error(4, "must not contain any null bytes");
12521252
RETURN_THROWS();
12531253
}
12541254

@@ -1351,12 +1351,12 @@ PHP_METHOD(SQLite3, backup)
13511351
}
13521352

13531353
if (ZEND_NUM_ARGS() >= 2 && CHECK_NULL_PATH(source_dbname, source_dbname_length)) {
1354-
zend_argument_type_error(2, "must not contain null bytes");
1354+
zend_argument_value_error(2, "must not contain any null bytes");
13551355
RETURN_THROWS();
13561356
}
13571357

13581358
if (ZEND_NUM_ARGS() >= 3 && CHECK_NULL_PATH(destination_dbname, destination_dbname_length)) {
1359-
zend_argument_type_error(3, "must not contain null bytes");
1359+
zend_argument_value_error(3, "must not contain any null bytes");
13601360
RETURN_THROWS();
13611361
}
13621362

0 commit comments

Comments
 (0)