Skip to content

Commit 4536963

Browse files
lexbor: Fix the allocation and move sizes of lexbor_array_obj_t
lexbor_array_obj_init() and lexbor_array_obj_expand() allocated 8 times the memory they need on 64-bit platforms, and lexbor_array_obj_delete() moved 8 times the bytes of the remaining entries. This is patch 0025, merged upstream as lexbor/lexbor@f4cbbcd. ext/dom uses such an array as its work list when it converts the parsed tree to libxml2, so gh25118.phpt needs a lower memory_limit to still fail during that conversion.
1 parent 919991e commit 4536963

28 files changed

Lines changed: 85 additions & 32 deletions

File tree

‎NEWS‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ PHP NEWS
5757
after a dot segment in a path, replacement file drive paths, the output
5858
encoding used for percent-encoding, the URLSearchParams tail pointer and
5959
fragment serialization without a query. (alexandre-daubois)
60+
. Merge patch lexbor/lexbor@f4cbbcd, reducing the memory used by
61+
Dom\HTMLDocument to parse documents with many sibling nodes.
62+
(Nicolas Grekas)
6063

6164
- LibXML:
6265
. Clear the libxml last error in libxml_use_internal_errors(false).

‎ext/dom/tests/gh25118.phpt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ dom
55
--SKIPIF--
66
<?php
77
if (PHP_INT_SIZE != 8) die("skip 64-bit only");
8+
if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
89
?>
910
--INI--
10-
memory_limit=32M
11+
memory_limit=26M
1112
--FILE--
1213
<?php
1314
$html = '<template><p>t</p><template><b>n</b></template></template><main>' . str_repeat('<i></i>', 100000) . '</main>';
1415
Dom\HTMLDocument::createFromString($html, LIBXML_NOERROR);
1516
?>
1617
--EXPECTF--
17-
Fatal error: Allowed memory size of 33554432 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d
18+
Fatal error: Allowed memory size of 27262976 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d

‎ext/lexbor/lexbor/core/array_obj.c‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ lexbor_array_obj_init(lexbor_array_obj_t *array,
2929
array->size = size;
3030
array->struct_size = struct_size;
3131

32-
array->list = lexbor_malloc(sizeof(uint8_t *)
33-
* (array->size * struct_size));
32+
array->list = lexbor_malloc(array->size * struct_size);
3433
if (array->list == NULL) {
3534
return LXB_STATUS_ERROR_MEMORY_ALLOCATION;
3635
}
@@ -77,8 +76,7 @@ lexbor_array_obj_expand(lexbor_array_obj_t *array, size_t up_to)
7776

7877
new_size = array->length + up_to;
7978

80-
list = lexbor_realloc(array->list, sizeof(uint8_t *)
81-
* (new_size * array->struct_size));
79+
list = lexbor_realloc(array->list, new_size * array->struct_size);
8280
if (list == NULL) {
8381
return NULL;
8482
}
@@ -170,8 +168,7 @@ lexbor_array_obj_delete(lexbor_array_obj_t *array, size_t begin, size_t length)
170168

171169
memmove(&array->list[ begin * array->struct_size ],
172170
&array->list[ end_len * array->struct_size ],
173-
sizeof(uint8_t *)
174-
* ((array->length - end_len) * array->struct_size));
171+
(array->length - end_len) * array->struct_size);
175172

176173
array->length -= length;
177174
}

‎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 ++

‎ext/lexbor/patches/0007-Add-lxb_url_is_special-to-the-public-API-362.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: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
33
Date: Sun, 17 May 2026 22:17:14 +0200
4-
Subject: [PATCH 07/24] Add lxb_url_is_special() to the public API (#362)
4+
Subject: [PATCH 07/25] Add lxb_url_is_special() to the public API (#362)
55

66
As https://wiki.php.net/rfc/uri_followup#uri_type_detection relies on this information.
77
---

0 commit comments

Comments
 (0)