Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions mailparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,9 @@ PHP_FUNCTION(mailparse_msg_parse)
}

mailparse_fetch_mimepart_resource(part, arg);
if (part == NULL) {
RETURN_FALSE;
}

if (FAILURE == php_mimepart_parse(part, ZSTR_VAL(data), ZSTR_LEN(data))) {
RETURN_FALSE;
Expand Down Expand Up @@ -1189,6 +1192,9 @@ PHP_FUNCTION(mailparse_msg_get_structure)
}

mailparse_fetch_mimepart_resource(part, arg);
if (part == NULL) {
RETURN_FALSE;
}

array_init(return_value);
php_mimepart_enum_parts(part, &get_structure_callback, return_value);
Expand Down Expand Up @@ -1314,6 +1320,9 @@ static void mailparse_do_extract(INTERNAL_FUNCTION_PARAMETERS, int decode, int i
}

mailparse_fetch_mimepart_resource(part, zpart);
if (part == NULL) {
RETURN_FALSE;
}

/* filename can be a filename or a stream */
if (Z_TYPE_P(filename) == IS_RESOURCE) {
Expand Down Expand Up @@ -1574,6 +1583,9 @@ PHP_FUNCTION(mailparse_msg_get_part_data)
}

mailparse_fetch_mimepart_resource(part, arg);
if (part == NULL) {
RETURN_FALSE;
}

mailparse_get_part_data(part, return_value);
}
Expand All @@ -1592,6 +1604,9 @@ PHP_FUNCTION(mailparse_msg_get_part)
}

mailparse_fetch_mimepart_resource(part, arg);
if (part == NULL) {
RETURN_FALSE;
}

foundpart = php_mimepart_find_by_name(part, ZSTR_VAL(mimesection));

Expand Down
26 changes: 26 additions & 0 deletions tests/msg_funcs_reject_wrong_resource.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
mailparse_msg_* reject a non-mimepart resource instead of crashing
--SKIPIF--
<?php if (!extension_loaded("mailparse")) print "skip"; ?>
--FILE--
<?php
/* Passing a wrong-type resource (a plain stream handle) used to dereference a
* NULL mimepart and segfault. It must now fail cleanly: a TypeError on PHP 8,
* a warning + false on PHP 7. Either way the process survives. */
$fp = tmpfile();

foreach (array("mailparse_msg_parse", "mailparse_msg_get_structure",
"mailparse_msg_get_part_data", "mailparse_msg_get_part",
"mailparse_msg_extract_part") as $fn) {
try {
@$fn($fp, "x");
} catch (\Throwable $e) {
/* TypeError on PHP 8 */
}
}

fclose($fp);
echo "survived\n";
?>
--EXPECT--
survived
Loading