From ca86c664b0d895a183b10305be4b670a234337ac Mon Sep 17 00:00:00 2001 From: Ilia Alshanetsky Date: Fri, 19 Jun 2026 08:46:05 -0400 Subject: [PATCH] Reject non-mimepart resources in mailparse_msg_* instead of crashing --- mailparse.c | 15 +++++++++++++ tests/msg_funcs_reject_wrong_resource.phpt | 26 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/msg_funcs_reject_wrong_resource.phpt diff --git a/mailparse.c b/mailparse.c index b690f9e..6210be0 100644 --- a/mailparse.c +++ b/mailparse.c @@ -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; @@ -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); @@ -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) { @@ -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); } @@ -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)); diff --git a/tests/msg_funcs_reject_wrong_resource.phpt b/tests/msg_funcs_reject_wrong_resource.phpt new file mode 100644 index 0000000..92f035d --- /dev/null +++ b/tests/msg_funcs_reject_wrong_resource.phpt @@ -0,0 +1,26 @@ +--TEST-- +mailparse_msg_* reject a non-mimepart resource instead of crashing +--SKIPIF-- + +--FILE-- + +--EXPECT-- +survived