Skip to content

Commit

Permalink
ext/mbstring: move unsigned 32 bit integer tests to a new test (#12891)
Browse files Browse the repository at this point in the history
And only run it on 64 bit architectures as those are floats on 32 bit.
  • Loading branch information
Girgias authored Dec 7, 2023
1 parent b0f7df1 commit 7684a3d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 27 deletions.
22 changes: 0 additions & 22 deletions ext/mbstring/tests/mb_decode_numericentity.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -103,26 +103,9 @@ test("Successive hex entities", "&#x322", "22", [0, 0xFFFF, 0, 0xFFFF], 'AS
test("Starting entity immediately after decimal entity which is too long", "&#10000000000A", "&#10000000000A", [0, 0xFFFF, 0, 0xFFFF], 'ASCII');
test("Starting entity immediately after hex entity which is too long", "&#x111111111A", "&#x111111111A", [0, 0xFFFF, 0, 0xFFFF], 'ASCII');

$ucs4_test1 = mb_convert_encoding("&#1000000000A", 'UCS-4BE', 'ASCII');
testNonAscii("Starting entity immediately after valid decimal entity which is just within maximum length", $ucs4_test1, "\x3B\x9A\xCA\x00\x00\x00\x00A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'UCS-4BE');
$ucs4_test2 = mb_convert_encoding("&#x11111111A", 'UCS-4BE', 'ASCII');
testNonAscii("Starting entity immediately after valid hex entity which is just within maximum length", $ucs4_test2, "\x11\x11\x11\x11\x00\x00\x00A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'UCS-4BE');

test("Starting entity immediately after invalid decimal entity", "&#0A", "&#0A", [0x1, 0xFFFF, 0, 0xFFFF], 'ASCII');
test("Starting entity immediately after invalid hex entity", "&#x0A", "&#x0A", [0x1, 0xFFFF, 0, 0xFFFF], 'ASCII');

test("Starting entity immediately after too-big decimal entity", "&#7001492542A", "&#7001492542A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'ASCII');

// If the numeric entity decodes to 0xFFFFFFFF, that should be passed through
// Originally, the new implementation of mb_decode_numericentity used -1 as a marker indicating
// that the entity could not be successfully decoded, so if the entity decoded successfully to
// 0xFFFFFFFF (-1), it would be treated as an invalid entity
test("Regression test (entity which decodes to 0xFFFFFFFF)", "", "?", [0xFFFFFF86, 0xFFFFFFFF, 0xF, 0xFC015448], 'HZ');

// With the legacy conversion filters, a trailing & could be truncated by mb_decode_numericentity,
// because some text encodings did not properly invoke the next flush function in the chain
test("Regression test (truncation of successive & with JIS encoding)", "&&&", "&&&", [0x20FF37FF, 0x7202F569, 0xC4090023, 0xF160], "JIS");

// Previously, signed arithmetic was used on convmap entries
test("Regression test (convmap entries are now treated as unsigned)", "&#7,", "?,", [0x22FFFF11, 0xBF111189, 0x67726511, 0x1161E719], "ASCII");

Expand Down Expand Up @@ -194,11 +177,6 @@ Successive &#65: string(9) "&#65A" => string(2) "AA" (Good)
Successive hex entities: string(11) "&#x322" => string(2) "22" (Good)
Starting entity immediately after decimal entity which is too long: string(18) "&#10000000000A" => string(14) "&#10000000000A" (Good)
Starting entity immediately after hex entity which is too long: string(17) "&#x111111111A" => string(13) "&#x111111111A" (Good)
Starting entity immediately after valid decimal entity which is just within maximum length: 000000260000002300000031000000300000003000000030000000300000003000000030000000300000003000000030000000260000002300000036000000350000003b => 3b9aca0000000041 (Good)
Starting entity immediately after valid hex entity which is just within maximum length: 0000002600000023000000780000003100000031000000310000003100000031000000310000003100000031000000260000002300000036000000350000003b => 1111111100000041 (Good)
Starting entity immediately after invalid decimal entity: string(8) "&#0A" => string(4) "&#0A" (Good)
Starting entity immediately after invalid hex entity: string(9) "&#x0A" => string(5) "&#x0A" (Good)
Starting entity immediately after too-big decimal entity: string(17) "&#7001492542A" => string(13) "&#7001492542A" (Good)
Regression test (entity which decodes to 0xFFFFFFFF): string(5) "" => string(1) "?" (Good)
Regression test (truncation of successive & with JIS encoding): string(3) "&&&" => string(3) "&&&" (Good)
Regression test (convmap entries are now treated as unsigned): string(4) "&#7," => string(2) "?," (Good)
58 changes: 58 additions & 0 deletions ext/mbstring/tests/mb_decode_numericentity_large_ints.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
--TEST--
mb_decode_numericentity() with 0xFFFFFFFF in conversion map
--EXTENSIONS--
mbstring
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
--FILE--
<?php

function varDumpToString($var)
{
ob_start();
var_dump($var);
return trim(ob_get_clean());
}

function test($desc, $str, $expected, $convmap, $encoding) {
$result = mb_decode_numericentity($str, $convmap, $encoding);
echo $desc, ": ", varDumpToString($str), " => ", varDumpToString($result);
if ($result === $expected)
echo " (Good)\n";
else
echo " (BAD; expected ", varDumpToString($expected), ")\n";
}

function testNonAscii($desc, $str, $expected, $convmap, $encoding) {
$result = mb_decode_numericentity($str, $convmap, $encoding);
echo $desc, ": ", bin2hex($str), " => ", bin2hex($result);
if ($result === $expected)
echo " (Good)\n";
else
echo " (BAD; expected ", bin2hex($expected), ")\n";
}

$ucs4_test1 = mb_convert_encoding("&#1000000000&#65;", 'UCS-4BE', 'ASCII');
testNonAscii("Starting entity immediately after valid decimal entity which is just within maximum length", $ucs4_test1, "\x3B\x9A\xCA\x00\x00\x00\x00A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'UCS-4BE');
$ucs4_test2 = mb_convert_encoding("&#x11111111&#65;", 'UCS-4BE', 'ASCII');
testNonAscii("Starting entity immediately after valid hex entity which is just within maximum length", $ucs4_test2, "\x11\x11\x11\x11\x00\x00\x00A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'UCS-4BE');

test("Starting entity immediately after too-big decimal entity", "&#7001492542&#65;", "&#7001492542A", [0, 0xFFFFFFFF, 0, 0xFFFFFFFF], 'ASCII');

// If the numeric entity decodes to 0xFFFFFFFF, that should be passed through
// Originally, the new implementation of mb_decode_numericentity used -1 as a marker indicating
// that the entity could not be successfully decoded, so if the entity decoded successfully to
// 0xFFFFFFFF (-1), it would be treated as an invalid entity
test("Regression test (entity which decodes to 0xFFFFFFFF)", "&#xe;", "?", [0xFFFFFF86, 0xFFFFFFFF, 0xF, 0xFC015448], 'HZ');

// With the legacy conversion filters, a trailing & could be truncated by mb_decode_numericentity,
// because some text encodings did not properly invoke the next flush function in the chain
test("Regression test (truncation of successive & with JIS encoding)", "&&&", "&&&", [0x20FF37FF, 0x7202F569, 0xC4090023, 0xF160], "JIS");

?>
--EXPECT--
Starting entity immediately after valid decimal entity which is just within maximum length: 000000260000002300000031000000300000003000000030000000300000003000000030000000300000003000000030000000260000002300000036000000350000003b => 3b9aca0000000041 (Good)
Starting entity immediately after valid hex entity which is just within maximum length: 0000002600000023000000780000003100000031000000310000003100000031000000310000003100000031000000260000002300000036000000350000003b => 1111111100000041 (Good)
Starting entity immediately after too-big decimal entity: string(17) "&#7001492542&#65;" => string(13) "&#7001492542A" (Good)
Regression test (entity which decodes to 0xFFFFFFFF): string(5) "&#xe;" => string(1) "?" (Good)
Regression test (truncation of successive & with JIS encoding): string(3) "&&&" => string(3) "&&&" (Good)
5 changes: 0 additions & 5 deletions ext/mbstring/tests/mb_encode_numericentity.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ echo "11 (hex): " . mb_encode_numericentity($iso2022jp, $convmap, "ISO-2022-JP",
$convmap = [0x2b, 0x2d4, 0x75656500, 0x656d2c53];
echo "12: " . mb_encode_numericentity("m", $convmap, "ASCII") . "\n";

// Regression test; the old implementation could only emit hexadecimal entities with about 5 digits
$convmap = [0xffffffff, 0xffffffff, 0x540a0af7, 0x5a485054];
echo "13: " . mb_encode_numericentity("\xFF", $convmap, "ASCII", true) . "\n";

?>
--EXPECT--
1: &#161;&#162;&#163;&#164;&#165;&#166;&#167;&#168;&#169;&#170;&#171;&#172;&#173;&#174;&#175;&#176;&#177;&#178;&#179;&#180;&#181;&#182;&#183;&#184;&#185;&#186;&#187;&#188;&#189;&#190;&#191;&#192;&#193;&#194;&#195;&#196;&#197;&#198;&#199;&#200;&#201;&#202;&#203;&#204;&#205;&#206;&#207;&#208;&#209;&#210;&#211;&#212;&#213;&#214;&#215;&#216;&#217;&#218;&#219;&#220;&#221;&#222;&#223;&#224;&#225;&#226;&#227;&#228;&#229;&#230;&#231;&#232;&#233;&#234;&#235;&#236;&#237;&#238;&#239;&#240;&#241;&#242;&#243;&#244;&#245;&#246;&#247;&#248;&#249;&#250;&#251;&#252;&#253;&#254;&#255;
Expand All @@ -78,4 +74,3 @@ echo "13: " . mb_encode_numericentity("\xFF", $convmap, "ASCII", true) . "\n";
11: &#12288;&#163;&#65;&#66;&#67;
11 (hex): &#x3000;&#xA3;&#x41;&#x42;&#x43;
12: &#1701127233;
13: &#x50080054;
16 changes: 16 additions & 0 deletions ext/mbstring/tests/mb_encode_numericentity_large_ints.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
mb_encode_numericentity() with 0xFFFFFFFF in conversion map
--EXTENSIONS--
mbstring
--SKIPIF--
<?php if (PHP_INT_SIZE != 8) die("skip this test is for 64bit platform only"); ?>
--FILE--
<?php

// Regression test; the old implementation could only emit hexadecimal entities with about 5 digits
$convmap = [0xffffffff, 0xffffffff, 0x540a0af7, 0x5a485054];
echo "13: " . mb_encode_numericentity("\xFF", $convmap, "ASCII", true) . "\n";

?>
--EXPECT--
13: &#x50080054;

0 comments on commit 7684a3d

Please sign in to comment.