Skip to content

Commit 3c33430

Browse files
authored
Fix 32-bit DOM characterdata failures (php#13663)
1 parent ef8dcbd commit 3c33430

7 files changed

+170
-88
lines changed

ext/dom/characterdata.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ PHP_METHOD(DOMCharacterData, substringData)
140140
RETURN_FALSE;
141141
}
142142

143-
if ((offset + count) > length) {
143+
if (count > length - offset) {
144144
count = length - offset;
145145
}
146146

@@ -305,7 +305,7 @@ static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool re
305305
substring = NULL;
306306
}
307307

308-
if ((offset + count) > length) {
308+
if (count > length - offset) {
309309
count = length - offset;
310310
}
311311

@@ -379,7 +379,7 @@ static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool r
379379
substring = NULL;
380380
}
381381

382-
if ((offset + count) > length) {
382+
if (count > length - offset) {
383383
count = length - offset;
384384
}
385385

ext/dom/tests/modern/spec/CharacterData_insertData_negative_offset.phpt

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ dom
55
--FILE--
66
<?php
77

8-
echo "--- Modern behaviour ---\n";
9-
108
$dom = DOM\HTMLDocument::createEmpty();
119
$comment = $dom->createComment("foobarbaz");
1210
try {
@@ -15,34 +13,11 @@ try {
1513
echo $e->getMessage(), "\n";
1614
}
1715
echo $dom->saveHTML($comment), "\n";
18-
$comment->insertData(-(2**32 - 1), "A");
19-
echo $dom->saveHTML($comment), "\n";
20-
21-
echo "--- Legacy behaviour ---\n";
22-
23-
$dom = new DOMDocument;
24-
$comment = $dom->createComment("foobarbaz");
25-
try {
26-
$comment->insertData(-1, "A");
27-
} catch (DOMException $e) {
28-
echo $e->getMessage(), "\n";
29-
}
30-
echo $dom->saveHTML($comment), "\n";
31-
try {
32-
$comment->insertData(-(2**32 - 1), "A");
33-
} catch (DOMException $e) {
34-
echo $e->getMessage(), "\n";
35-
}
16+
$comment->insertData(1, "A");
3617
echo $dom->saveHTML($comment), "\n";
3718

3819
?>
3920
--EXPECT--
40-
--- Modern behaviour ---
4121
Index Size Error
4222
<!--foobarbaz-->
4323
<!--fAoobarbaz-->
44-
--- Legacy behaviour ---
45-
Index Size Error
46-
<!--foobarbaz-->
47-
Index Size Error
48-
<!--foobarbaz-->
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
--TEST--
2+
insertData() negative offset (mod 32)
3+
--EXTENSIONS--
4+
dom
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE === 4) die('skip not for 32-bit');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
echo "--- Modern behaviour ---\n";
13+
14+
$dom = DOM\HTMLDocument::createEmpty();
15+
$comment = $dom->createComment("foobarbaz");
16+
try {
17+
$comment->insertData(-1, "A");
18+
} catch (DOMException $e) {
19+
echo $e->getMessage(), "\n";
20+
}
21+
echo $dom->saveHTML($comment), "\n";
22+
$comment->insertData(-(2**32 - 1), "A");
23+
echo $dom->saveHTML($comment), "\n";
24+
25+
echo "--- Legacy behaviour ---\n";
26+
27+
$dom = new DOMDocument;
28+
$comment = $dom->createComment("foobarbaz");
29+
try {
30+
$comment->insertData(-1, "A");
31+
} catch (DOMException $e) {
32+
echo $e->getMessage(), "\n";
33+
}
34+
echo $dom->saveHTML($comment), "\n";
35+
try {
36+
$comment->insertData(-(2**32 - 1), "A");
37+
} catch (DOMException $e) {
38+
echo $e->getMessage(), "\n";
39+
}
40+
echo $dom->saveHTML($comment), "\n";
41+
42+
?>
43+
--EXPECT--
44+
--- Modern behaviour ---
45+
Index Size Error
46+
<!--foobarbaz-->
47+
<!--fAoobarbaz-->
48+
--- Legacy behaviour ---
49+
Index Size Error
50+
<!--foobarbaz-->
51+
Index Size Error
52+
<!--foobarbaz-->

ext/dom/tests/modern/spec/CharacterData_replaceData_negative_count.phpt

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,15 @@ dom
55
--FILE--
66
<?php
77

8-
echo "--- Modern behaviour ---\n";
9-
108
$dom = DOM\HTMLDocument::createEmpty();
119
$comment = $dom->createComment("foobarbaz");
1210
$comment->replaceData(0, -1, "A");
1311
echo $dom->saveHTML($comment), "\n";
1412
$comment = $dom->createComment("foobarbaz");
15-
$comment->replaceData(2, -(2**32 - 2), "A");
16-
echo $dom->saveHTML($comment), "\n";
17-
18-
echo "--- Legacy behaviour ---\n";
19-
20-
$dom = new DOMDocument;
21-
$comment = $dom->createComment("foobarbaz");
22-
try {
23-
$comment->replaceData(0, -1, "A");
24-
} catch (DOMException $e) {
25-
echo $e->getMessage(), "\n";
26-
}
27-
echo $dom->saveHTML($comment), "\n";
28-
try {
29-
$comment->replaceData(2, -(2**32 - 2), "A");
30-
} catch (DOMException $e) {
31-
echo $e->getMessage(), "\n";
32-
}
13+
$comment->replaceData(2, -2, "A");
3314
echo $dom->saveHTML($comment), "\n";
3415

3516
?>
3617
--EXPECT--
37-
--- Modern behaviour ---
3818
<!--A-->
39-
<!--foAarbaz-->
40-
--- Legacy behaviour ---
41-
Index Size Error
42-
<!--foobarbaz-->
43-
Index Size Error
44-
<!--foobarbaz-->
19+
<!--foA-->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
replaceData() negative count (mod 32)
3+
--EXTENSIONS--
4+
dom
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE === 4) die('skip not for 32-bit');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
echo "--- Modern behaviour ---\n";
13+
14+
$dom = DOM\HTMLDocument::createEmpty();
15+
$comment = $dom->createComment("foobarbaz");
16+
$comment->replaceData(0, -1, "A");
17+
echo $dom->saveHTML($comment), "\n";
18+
$comment = $dom->createComment("foobarbaz");
19+
$comment->replaceData(2, -(2**32 - 2), "A");
20+
echo $dom->saveHTML($comment), "\n";
21+
22+
echo "--- Legacy behaviour ---\n";
23+
24+
$dom = new DOMDocument;
25+
$comment = $dom->createComment("foobarbaz");
26+
try {
27+
$comment->replaceData(0, -1, "A");
28+
} catch (DOMException $e) {
29+
echo $e->getMessage(), "\n";
30+
}
31+
echo $dom->saveHTML($comment), "\n";
32+
try {
33+
$comment->replaceData(2, -(2**32 - 2), "A");
34+
} catch (DOMException $e) {
35+
echo $e->getMessage(), "\n";
36+
}
37+
echo $dom->saveHTML($comment), "\n";
38+
39+
?>
40+
--EXPECT--
41+
--- Modern behaviour ---
42+
<!--A-->
43+
<!--foAarbaz-->
44+
--- Legacy behaviour ---
45+
Index Size Error
46+
<!--foobarbaz-->
47+
Index Size Error
48+
<!--foobarbaz-->

ext/dom/tests/modern/spec/CharacterData_substringData_negative_arguments.phpt

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,24 @@ dom
55
--FILE--
66
<?php
77

8-
echo "--- Modern behaviour ---\n";
9-
108
$dom = DOM\HTMLDocument::createEmpty();
119
$comment = $dom->createComment("foobarbaz");
1210
var_dump($comment->substringData(0, -1));
1311
echo $dom->saveHTML($comment), "\n";
14-
var_dump($comment->substringData(2, -(2**32 - 2)));
15-
echo $dom->saveHTML($comment), "\n";
16-
var_dump($comment->substringData(-(2**32 - 2), 2));
17-
echo $dom->saveHTML($comment), "\n";
18-
19-
echo "--- Legacy behaviour ---\n";
20-
21-
$dom = new DOMDocument;
22-
$comment = $dom->createComment("foobarbaz");
23-
try {
24-
var_dump($comment->substringData(0, -1));
25-
} catch (DOMException $e) {
26-
echo $e->getMessage(), "\n";
27-
}
28-
echo $dom->saveHTML($comment), "\n";
29-
try {
30-
var_dump($comment->substringData(2, -(2**32 - 2)));
31-
} catch (DOMException $e) {
32-
echo $e->getMessage(), "\n";
33-
}
12+
var_dump($comment->substringData(2, -2));
3413
echo $dom->saveHTML($comment), "\n";
3514
try {
36-
var_dump($comment->substringData(-(2**32 - 2), 2));
15+
var_dump($comment->substringData(-2, 2));
3716
} catch (DOMException $e) {
3817
echo $e->getMessage(), "\n";
3918
}
4019
echo $dom->saveHTML($comment), "\n";
4120

4221
?>
4322
--EXPECT--
44-
--- Modern behaviour ---
4523
string(9) "foobarbaz"
4624
<!--foobarbaz-->
47-
string(2) "ob"
48-
<!--foobarbaz-->
49-
string(2) "ob"
50-
<!--foobarbaz-->
51-
--- Legacy behaviour ---
52-
Index Size Error
53-
<!--foobarbaz-->
54-
Index Size Error
25+
string(7) "obarbaz"
5526
<!--foobarbaz-->
5627
Index Size Error
5728
<!--foobarbaz-->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
--TEST--
2+
substringData() negative arguments (mod 32)
3+
--EXTENSIONS--
4+
dom
5+
--SKIPIF--
6+
<?php
7+
if (PHP_INT_SIZE === 4) die('skip not for 32-bit');
8+
?>
9+
--FILE--
10+
<?php
11+
12+
echo "--- Modern behaviour ---\n";
13+
14+
$dom = DOM\HTMLDocument::createEmpty();
15+
$comment = $dom->createComment("foobarbaz");
16+
var_dump($comment->substringData(0, -1));
17+
echo $dom->saveHTML($comment), "\n";
18+
var_dump($comment->substringData(2, -(2**32 - 2)));
19+
echo $dom->saveHTML($comment), "\n";
20+
var_dump($comment->substringData(-(2**32 - 2), 2));
21+
echo $dom->saveHTML($comment), "\n";
22+
23+
echo "--- Legacy behaviour ---\n";
24+
25+
$dom = new DOMDocument;
26+
$comment = $dom->createComment("foobarbaz");
27+
try {
28+
var_dump($comment->substringData(0, -1));
29+
} catch (DOMException $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
echo $dom->saveHTML($comment), "\n";
33+
try {
34+
var_dump($comment->substringData(2, -(2**32 - 2)));
35+
} catch (DOMException $e) {
36+
echo $e->getMessage(), "\n";
37+
}
38+
echo $dom->saveHTML($comment), "\n";
39+
try {
40+
var_dump($comment->substringData(-(2**32 - 2), 2));
41+
} catch (DOMException $e) {
42+
echo $e->getMessage(), "\n";
43+
}
44+
echo $dom->saveHTML($comment), "\n";
45+
46+
?>
47+
--EXPECT--
48+
--- Modern behaviour ---
49+
string(9) "foobarbaz"
50+
<!--foobarbaz-->
51+
string(2) "ob"
52+
<!--foobarbaz-->
53+
string(2) "ob"
54+
<!--foobarbaz-->
55+
--- Legacy behaviour ---
56+
Index Size Error
57+
<!--foobarbaz-->
58+
Index Size Error
59+
<!--foobarbaz-->
60+
Index Size Error
61+
<!--foobarbaz-->

0 commit comments

Comments
 (0)