[php-src] PHP-8.6: Fix iconv_mime_encode() overflowing on a negative line-length (#25485)

From: Date: Fri, 27 Nov 2026 05:04:35 +0000
Subject: [php-src] PHP-8.6: Fix iconv_mime_encode() overflowing on a negative line-length (#25485)
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-144247@lists.php.net to get a copy of this message
Author: lacatoire (lacatoire) Committer: Weilin Du (LamentXU123) Date: 2026-11-27T12:51:52+08:00 Commit: https://github.com/php/php-src/commit/f603ffb0fc0250fe62b9ef1f29fa0166650323d6 Raw diff: https://github.com/php/php-src/commit/f603ffb0fc0250fe62b9ef1f29fa0166650323d6.diff Fix iconv_mime_encode() overflowing on a negative line-length (#25485) Clamp a negative line-length to zero before converting it to size_t. This avoids a fatal allocation overflow and preserves the existing false return for a zero line-length without introducing a warning. Closes #25485 Changed paths: A ext/iconv/tests/mime_encode_line_length_invalid.phpt M NEWS M ext/iconv/iconv.c Diff: diff --git a/NEWS b/NEWS index e12f30201909..d982a1008b1d 100644 --- a/NEWS +++ b/NEWS @@ -98,6 +98,10 @@ PHP NEWS . Fixed hash() and hash_file() hanging on invalid xxh3/xxh128 options. (lacatoire) +- Iconv: + . Fixed iconv_mime_encode() failing with an integer overflow error for a + negative line-length. (lacatoire) + - Intl: . Fixed Locale::lookup() ignoring the default locale when the language tag list is empty. (Weilin Du) diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c index 0212d1063c75..ca782de79a31 100644 --- a/ext/iconv/iconv.c +++ b/ext/iconv/iconv.c @@ -2087,6 +2087,9 @@ PHP_FUNCTION(iconv_mime_encode) if ((pzval = zend_hash_str_find_deref(Z_ARRVAL_P(pref), "line-length", sizeof("line-length") - 1)) != NULL) { line_len = zval_get_long(pzval); + if (line_len < 0) { + line_len = 0; + } } if ((pzval = zend_hash_str_find_deref(Z_ARRVAL_P(pref), "line-break-chars", sizeof("line-break-chars") - 1)) != NULL) { diff --git a/ext/iconv/tests/mime_encode_line_length_invalid.phpt b/ext/iconv/tests/mime_encode_line_length_invalid.phpt new file mode 100644 index 000000000000..76803bf471e1 --- /dev/null +++ b/ext/iconv/tests/mime_encode_line_length_invalid.phpt @@ -0,0 +1,22 @@ +--TEST-- +iconv_mime_encode(): a negative line-length behaves like 0 +--EXTENSIONS-- +iconv +--FILE-- +<?php +$o = ["input-charset" => "UTF-8", "output-charset" => "UTF-8"]; +foreach ([-1, 0, PHP_INT_MIN] as $len) { + var_dump(iconv_mime_encode("S", "héllo", $o + ["line-length" => $len])); +} +var_dump(iconv_mime_encode("S", "héllo", $o + ["line-length" => 76])); +?> +--EXPECTF-- +Warning: iconv_mime_encode(): Buffer length exceeded in %s on line %d +bool(false) + +Warning: iconv_mime_encode(): Buffer length exceeded in %s on line %d +bool(false) + +Warning: iconv_mime_encode(): Buffer length exceeded in %s on line %d +bool(false) +string(%d) "S: =?UTF-8?B?%s?="

« previous php.cvs (#144247) next »