Skip to content

Commit 3efb356

Browse files
committed
Merge branch 'PHP-8.6'
* PHP-8.6: Fix GH-25305: NULL dereference in lxb_dom_document_destroy on lexbor document init failure
2 parents 7815b6c + d4d19ca commit 3efb356

29 files changed

Lines changed: 157 additions & 33 deletions

File tree

‎ext/dom/tests/gh25305.phpt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor document init failure)
3+
--CREDITS--
4+
YuanchengJiang
5+
--EXTENSIONS--
6+
dom
7+
--SKIPIF--
8+
<?php
9+
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
10+
?>
11+
--INI--
12+
memory_limit=2M
13+
--FILE--
14+
<?php
15+
$str = str_repeat("a", (int)(1024 * 1024 * 1.25));
16+
Dom\HTMLDocument::createFromString("<p>x</p>");
17+
?>
18+
--EXPECTF--
19+
Fatal error: Allowed memory size of 2097152 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d%A

‎ext/lexbor/lexbor/dom/interfaces/document.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,
124124

125125
document->css = NULL;
126126

127+
/*
128+
* Set before the allocations: lxb_dom_document_destroy() frees a document
129+
* that owns itself, and dereferences the owner of any other.
130+
*/
131+
node->owner_document = document;
132+
127133
/* For nodes */
128134
document->mraw = lexbor_mraw_create();
129135
status = lexbor_mraw_init(document->mraw, (4096 * 8));
@@ -166,18 +172,17 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,
166172
goto failed;
167173
}
168174

169-
node->owner_document = document;
170-
171175
return LXB_STATUS_OK;
172176

173177
failed:
174178

175-
lexbor_mraw_destroy(document->mraw, true);
176-
lexbor_mraw_destroy(document->text, true);
177-
lexbor_hash_destroy(document->tags, true);
178-
lexbor_hash_destroy(document->ns, true);
179-
lexbor_hash_destroy(document->attrs, true);
180-
lexbor_hash_destroy(document->prefix, true);
179+
/* Reset, so that lxb_dom_document_destroy() does not free them again. */
180+
document->mraw = lexbor_mraw_destroy(document->mraw, true);
181+
document->text = lexbor_mraw_destroy(document->text, true);
182+
document->tags = lexbor_hash_destroy(document->tags, true);
183+
document->ns = lexbor_hash_destroy(document->ns, true);
184+
document->attrs = lexbor_hash_destroy(document->attrs, true);
185+
document->prefix = lexbor_hash_destroy(document->prefix, true);
181186

182187
return LXB_STATUS_ERROR;
183188
}

‎ext/lexbor/lexbor/html/interfaces/document.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ lxb_html_document_parser_prepare(lxb_html_document_t *document)
278278
status = lxb_html_parser_init(doc->parser);
279279

280280
if (status != LXB_STATUS_OK) {
281-
lxb_html_parser_destroy(doc->parser);
281+
doc->parser = lxb_html_parser_destroy(doc->parser);
282282
return status;
283283
}
284284

‎ext/lexbor/lexbor/html/parser.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
4040
parser->tkz = lxb_html_tokenizer_create();
4141
lxb_status_t status = lxb_html_tokenizer_init(parser->tkz);
4242

43+
/*
44+
* A tokenizer or a tree that failed to initialize has no reference yet,
45+
* so lxb_html_parser_destroy() could not release it: destroy it here.
46+
*/
4347
if (status != LXB_STATUS_OK) {
48+
parser->tkz = lxb_html_tokenizer_destroy(parser->tkz);
4449
return status;
4550
}
4651

@@ -51,6 +56,7 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
5156
status = lxb_html_tree_init(parser->tree, parser->tkz);
5257

5358
if (status != LXB_STATUS_OK) {
59+
parser->tree = lxb_html_tree_destroy(parser->tree);
5460
return status;
5561
}
5662

‎ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Sat, 26 Aug 2023 15:08:59 +0200
4-
Subject: [PATCH 01/24] Expose line and column information for use in PHP
4+
Subject: [PATCH 01/25] Expose line and column information for use in PHP
55

66
---
77
source/lexbor/dom/interfaces/node.h | 2 ++

‎ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Mon, 14 Aug 2023 20:18:51 +0200
4-
Subject: [PATCH 02/24] Track implied added nodes for options use in PHP
4+
Subject: [PATCH 02/25] Track implied added nodes for options use in PHP
55

66
---
77
source/lexbor/html/tree.h | 3 +++

‎ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Thu, 24 Aug 2023 22:57:48 +0200
4-
Subject: [PATCH 03/24] Patch utilities and data structure to be able to
4+
Subject: [PATCH 03/25] Patch utilities and data structure to be able to
55
generate smaller lookup tables
66

77
Changed the generation script to check if everything fits in 32-bits.

‎ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Wed, 29 Nov 2023 21:26:47 +0100
4-
Subject: [PATCH 04/24] Remove unused upper case tag static data
4+
Subject: [PATCH 04/25] Remove unused upper case tag static data
55

66
---
77
source/lexbor/tag/res.h | 2 ++

‎ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Wed, 29 Nov 2023 21:29:31 +0100
4-
Subject: [PATCH 05/24] Shrink size of static binary search tree
4+
Subject: [PATCH 05/25] Shrink size of static binary search tree
55

66
This also makes it more efficient on the data cache.
77
---

‎ext/lexbor/patches/0006-Patch-out-unused-CSS-style-code.patch‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
22
From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
33
Date: Sun, 7 Jan 2024 21:59:28 +0100
4-
Subject: [PATCH 06/24] Patch out unused CSS style code
4+
Subject: [PATCH 06/25] Patch out unused CSS style code
55

66
---
77
source/lexbor/css/rule.h | 2 ++

0 commit comments

Comments
 (0)