From be8fd5c35357abb898cc6da3ff355de28ef86417 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 10 Mar 2024 14:34:08 +0100 Subject: [PATCH] Fix 32-bit characterdata failures --- ext/dom/characterdata.c | 6 +- ...racterData_insertData_negative_offset.phpt | 27 +------- ...Data_insertData_negative_offset_mod32.phpt | 52 ++++++++++++++++ ...racterData_replaceData_negative_count.phpt | 29 +-------- ...Data_replaceData_negative_count_mod32.phpt | 48 +++++++++++++++ ...Data_substringData_negative_arguments.phpt | 35 +---------- ...ubstringData_negative_arguments_mod32.phpt | 61 +++++++++++++++++++ 7 files changed, 170 insertions(+), 88 deletions(-) create mode 100644 ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset_mod32.phpt create mode 100644 ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count_mod32.phpt create mode 100644 ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments_mod32.phpt diff --git a/ext/dom/characterdata.c b/ext/dom/characterdata.c index 6bd5811fdffa..3e7029e8f1bd 100644 --- a/ext/dom/characterdata.c +++ b/ext/dom/characterdata.c @@ -140,7 +140,7 @@ PHP_METHOD(DOMCharacterData, substringData) RETURN_FALSE; } - if ((offset + count) > length) { + if (count > length - offset) { count = length - offset; } @@ -305,7 +305,7 @@ static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool re substring = NULL; } - if ((offset + count) > length) { + if (count > length - offset) { count = length - offset; } @@ -379,7 +379,7 @@ static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool r substring = NULL; } - if ((offset + count) > length) { + if (count > length - offset) { count = length - offset; } diff --git a/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset.phpt b/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset.phpt index 4166c64f5449..f00625df3521 100644 --- a/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset.phpt +++ b/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset.phpt @@ -5,8 +5,6 @@ dom --FILE-- createComment("foobarbaz"); try { @@ -15,34 +13,11 @@ try { echo $e->getMessage(), "\n"; } echo $dom->saveHTML($comment), "\n"; -$comment->insertData(-(2**32 - 1), "A"); -echo $dom->saveHTML($comment), "\n"; - -echo "--- Legacy behaviour ---\n"; - -$dom = new DOMDocument; -$comment = $dom->createComment("foobarbaz"); -try { - $comment->insertData(-1, "A"); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} -echo $dom->saveHTML($comment), "\n"; -try { - $comment->insertData(-(2**32 - 1), "A"); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} +$comment->insertData(1, "A"); echo $dom->saveHTML($comment), "\n"; ?> --EXPECT-- ---- Modern behaviour --- Index Size Error ---- Legacy behaviour --- -Index Size Error - -Index Size Error - diff --git a/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset_mod32.phpt b/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset_mod32.phpt new file mode 100644 index 000000000000..5c0be698bda8 --- /dev/null +++ b/ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset_mod32.phpt @@ -0,0 +1,52 @@ +--TEST-- +insertData() negative offset (mod 32) +--EXTENSIONS-- +dom +--SKIPIF-- + +--FILE-- +createComment("foobarbaz"); +try { + $comment->insertData(-1, "A"); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; +$comment->insertData(-(2**32 - 1), "A"); +echo $dom->saveHTML($comment), "\n"; + +echo "--- Legacy behaviour ---\n"; + +$dom = new DOMDocument; +$comment = $dom->createComment("foobarbaz"); +try { + $comment->insertData(-1, "A"); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; +try { + $comment->insertData(-(2**32 - 1), "A"); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; + +?> +--EXPECT-- +--- Modern behaviour --- +Index Size Error + + +--- Legacy behaviour --- +Index Size Error + +Index Size Error + diff --git a/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count.phpt b/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count.phpt index d673cb25198f..96402847f73c 100644 --- a/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count.phpt +++ b/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count.phpt @@ -5,40 +5,15 @@ dom --FILE-- createComment("foobarbaz"); $comment->replaceData(0, -1, "A"); echo $dom->saveHTML($comment), "\n"; $comment = $dom->createComment("foobarbaz"); -$comment->replaceData(2, -(2**32 - 2), "A"); -echo $dom->saveHTML($comment), "\n"; - -echo "--- Legacy behaviour ---\n"; - -$dom = new DOMDocument; -$comment = $dom->createComment("foobarbaz"); -try { - $comment->replaceData(0, -1, "A"); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} -echo $dom->saveHTML($comment), "\n"; -try { - $comment->replaceData(2, -(2**32 - 2), "A"); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} +$comment->replaceData(2, -2, "A"); echo $dom->saveHTML($comment), "\n"; ?> --EXPECT-- ---- Modern behaviour --- - ---- Legacy behaviour --- -Index Size Error - -Index Size Error - + diff --git a/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count_mod32.phpt b/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count_mod32.phpt new file mode 100644 index 000000000000..9382ed254975 --- /dev/null +++ b/ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count_mod32.phpt @@ -0,0 +1,48 @@ +--TEST-- +replaceData() negative count (mod 32) +--EXTENSIONS-- +dom +--SKIPIF-- + +--FILE-- +createComment("foobarbaz"); +$comment->replaceData(0, -1, "A"); +echo $dom->saveHTML($comment), "\n"; +$comment = $dom->createComment("foobarbaz"); +$comment->replaceData(2, -(2**32 - 2), "A"); +echo $dom->saveHTML($comment), "\n"; + +echo "--- Legacy behaviour ---\n"; + +$dom = new DOMDocument; +$comment = $dom->createComment("foobarbaz"); +try { + $comment->replaceData(0, -1, "A"); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; +try { + $comment->replaceData(2, -(2**32 - 2), "A"); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; + +?> +--EXPECT-- +--- Modern behaviour --- + + +--- Legacy behaviour --- +Index Size Error + +Index Size Error + diff --git a/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments.phpt b/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments.phpt index 756e3cf07678..a0b5414bb898 100644 --- a/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments.phpt +++ b/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments.phpt @@ -5,35 +5,14 @@ dom --FILE-- createComment("foobarbaz"); var_dump($comment->substringData(0, -1)); echo $dom->saveHTML($comment), "\n"; -var_dump($comment->substringData(2, -(2**32 - 2))); -echo $dom->saveHTML($comment), "\n"; -var_dump($comment->substringData(-(2**32 - 2), 2)); -echo $dom->saveHTML($comment), "\n"; - -echo "--- Legacy behaviour ---\n"; - -$dom = new DOMDocument; -$comment = $dom->createComment("foobarbaz"); -try { - var_dump($comment->substringData(0, -1)); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} -echo $dom->saveHTML($comment), "\n"; -try { - var_dump($comment->substringData(2, -(2**32 - 2))); -} catch (DOMException $e) { - echo $e->getMessage(), "\n"; -} +var_dump($comment->substringData(2, -2)); echo $dom->saveHTML($comment), "\n"; try { - var_dump($comment->substringData(-(2**32 - 2), 2)); + var_dump($comment->substringData(-2, 2)); } catch (DOMException $e) { echo $e->getMessage(), "\n"; } @@ -41,17 +20,9 @@ echo $dom->saveHTML($comment), "\n"; ?> --EXPECT-- ---- Modern behaviour --- string(9) "foobarbaz" -string(2) "ob" - -string(2) "ob" - ---- Legacy behaviour --- -Index Size Error - -Index Size Error +string(7) "obarbaz" Index Size Error diff --git a/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments_mod32.phpt b/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments_mod32.phpt new file mode 100644 index 000000000000..c29eb0b77929 --- /dev/null +++ b/ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments_mod32.phpt @@ -0,0 +1,61 @@ +--TEST-- +substringData() negative arguments (mod 32) +--EXTENSIONS-- +dom +--SKIPIF-- + +--FILE-- +createComment("foobarbaz"); +var_dump($comment->substringData(0, -1)); +echo $dom->saveHTML($comment), "\n"; +var_dump($comment->substringData(2, -(2**32 - 2))); +echo $dom->saveHTML($comment), "\n"; +var_dump($comment->substringData(-(2**32 - 2), 2)); +echo $dom->saveHTML($comment), "\n"; + +echo "--- Legacy behaviour ---\n"; + +$dom = new DOMDocument; +$comment = $dom->createComment("foobarbaz"); +try { + var_dump($comment->substringData(0, -1)); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; +try { + var_dump($comment->substringData(2, -(2**32 - 2))); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; +try { + var_dump($comment->substringData(-(2**32 - 2), 2)); +} catch (DOMException $e) { + echo $e->getMessage(), "\n"; +} +echo $dom->saveHTML($comment), "\n"; + +?> +--EXPECT-- +--- Modern behaviour --- +string(9) "foobarbaz" + +string(2) "ob" + +string(2) "ob" + +--- Legacy behaviour --- +Index Size Error + +Index Size Error + +Index Size Error +