Commit 268a2e5319c for php

commit 268a2e5319caa87dc0a0871428ffd9aee3daf9fb
Author: David Carlier <devnexen@gmail.com>
Date:   Sun Nov 8 14:59:05 2026 +0000

    Fix GH-25305: NULL dereference in lxb_dom_document_destroy on lexbor document init failure

    lxb_dom_document_init() set owner_document only after all of the
    document's memory pools were created. When one of these allocations
    failed, which the fallible lexbor allocators make possible once
    memory_limit is reached, the error path of
    lxb_html_document_interface_create() called lxb_dom_document_destroy()
    with a NULL owner_document and dereferenced it. The failure path also left
    dangling pointers to the destroyed pools.

    Set owner_document before allocating and reset the pool pointers on
    failure, carried as a lexbor patch until lexbor/lexbor#448 is merged. Only
    the source/lexbor part of the upstream change is carried.

    Close GH-25320

diff --git a/NEWS b/NEWS
index b138439b8f3..24571d4b3ac 100644
--- a/NEWS
+++ b/NEWS
@@ -37,6 +37,8 @@ PHP                                                                        NEWS
 - DOM:
   . Fixed bug GH-25306 (Assertion status == LXB_STATUS_OK in dom_parse_selector
     when lexbor allocation fails). (ndossche)
+  . Fixed bug GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor
+    document init failure). (David Carlier)

 - Filter:
   . Fixed stack overflow in filter_var() and filter_var_array() with deeply
diff --git a/ext/dom/tests/gh25305.phpt b/ext/dom/tests/gh25305.phpt
new file mode 100644
index 00000000000..02344a82f24
--- /dev/null
+++ b/ext/dom/tests/gh25305.phpt
@@ -0,0 +1,19 @@
+--TEST--
+GH-25305 (NULL dereference in lxb_dom_document_destroy on lexbor document init failure)
+--CREDITS--
+YuanchengJiang
+--EXTENSIONS--
+dom
+--SKIPIF--
+<?php
+if (getenv("USE_ZEND_ALLOC") === "0") die("skip Zend MM disabled");
+?>
+--INI--
+memory_limit=2M
+--FILE--
+<?php
+$str = str_repeat("a", (int)(1024 * 1024 * 1.25));
+Dom\HTMLDocument::createFromString("<p>x</p>");
+?>
+--EXPECTF--
+Fatal error: Allowed memory size of 2097152 bytes exhausted %S(tried to allocate %d bytes) in %s on line %d%A
diff --git a/ext/lexbor/lexbor/dom/interfaces/document.c b/ext/lexbor/lexbor/dom/interfaces/document.c
index 43eeba0203f..6428354c12b 100644
--- a/ext/lexbor/lexbor/dom/interfaces/document.c
+++ b/ext/lexbor/lexbor/dom/interfaces/document.c
@@ -124,6 +124,12 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,

     document->css = NULL;

+    /*
+     * Set before the allocations: lxb_dom_document_destroy() frees a document
+     * that owns itself, and dereferences the owner of any other.
+     */
+    node->owner_document = document;
+
     /* For nodes */
     document->mraw = lexbor_mraw_create();
     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,
         goto failed;
     }

-    node->owner_document = document;
-
     return LXB_STATUS_OK;

 failed:

-    lexbor_mraw_destroy(document->mraw, true);
-    lexbor_mraw_destroy(document->text, true);
-    lexbor_hash_destroy(document->tags, true);
-    lexbor_hash_destroy(document->ns, true);
-    lexbor_hash_destroy(document->attrs, true);
-    lexbor_hash_destroy(document->prefix, true);
+    /* Reset, so that lxb_dom_document_destroy() does not free them again. */
+    document->mraw = lexbor_mraw_destroy(document->mraw, true);
+    document->text = lexbor_mraw_destroy(document->text, true);
+    document->tags = lexbor_hash_destroy(document->tags, true);
+    document->ns = lexbor_hash_destroy(document->ns, true);
+    document->attrs = lexbor_hash_destroy(document->attrs, true);
+    document->prefix = lexbor_hash_destroy(document->prefix, true);

     return LXB_STATUS_ERROR;
 }
diff --git a/ext/lexbor/lexbor/html/interfaces/document.c b/ext/lexbor/lexbor/html/interfaces/document.c
index b40c6c8692f..5059050f5db 100644
--- a/ext/lexbor/lexbor/html/interfaces/document.c
+++ b/ext/lexbor/lexbor/html/interfaces/document.c
@@ -278,7 +278,7 @@ lxb_html_document_parser_prepare(lxb_html_document_t *document)
         status = lxb_html_parser_init(doc->parser);

         if (status != LXB_STATUS_OK) {
-            lxb_html_parser_destroy(doc->parser);
+            doc->parser = lxb_html_parser_destroy(doc->parser);
             return status;
         }

diff --git a/ext/lexbor/lexbor/html/parser.c b/ext/lexbor/lexbor/html/parser.c
index 7d051045101..62e73701034 100644
--- a/ext/lexbor/lexbor/html/parser.c
+++ b/ext/lexbor/lexbor/html/parser.c
@@ -40,7 +40,12 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
     parser->tkz = lxb_html_tokenizer_create();
     lxb_status_t status = lxb_html_tokenizer_init(parser->tkz);

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

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

     if (status != LXB_STATUS_OK) {
+        parser->tree = lxb_html_tree_destroy(parser->tree);
         return status;
     }

diff --git a/ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch b/ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch
index 56079940029..9cb75078bd2 100644
--- a/ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch
+++ b/ext/lexbor/patches/0001-Expose-line-and-column-information-for-use-in-PHP.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Sat, 26 Aug 2023 15:08:59 +0200
-Subject: [PATCH 01/21] Expose line and column information for use in PHP
+Subject: [PATCH 01/22] Expose line and column information for use in PHP

 ---
  source/lexbor/dom/interfaces/node.h  |  2 ++
diff --git a/ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch b/ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch
index 8dc8cb984d2..2bf5350d5c4 100644
--- a/ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch
+++ b/ext/lexbor/patches/0002-Track-implied-added-nodes-for-options-use-in-PHP.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Mon, 14 Aug 2023 20:18:51 +0200
-Subject: [PATCH 02/21] Track implied added nodes for options use in PHP
+Subject: [PATCH 02/22] Track implied added nodes for options use in PHP

 ---
  source/lexbor/html/tree.h                            | 3 +++
diff --git a/ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch b/ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch
index f93d9fe8f86..2f13684510e 100644
--- a/ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch
+++ b/ext/lexbor/patches/0003-Patch-utilities-and-data-structure-to-be-able-to-gen.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Thu, 24 Aug 2023 22:57:48 +0200
-Subject: [PATCH 03/21] Patch utilities and data structure to be able to
+Subject: [PATCH 03/22] Patch utilities and data structure to be able to
  generate smaller lookup tables

 Changed the generation script to check if everything fits in 32-bits.
diff --git a/ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch b/ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch
index 35bec95e6b9..c5f7ef52167 100644
--- a/ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch
+++ b/ext/lexbor/patches/0004-Remove-unused-upper-case-tag-static-data.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Wed, 29 Nov 2023 21:26:47 +0100
-Subject: [PATCH 04/21] Remove unused upper case tag static data
+Subject: [PATCH 04/22] Remove unused upper case tag static data

 ---
  source/lexbor/tag/res.h | 2 ++
diff --git a/ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch b/ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch
index 68f2d4d379e..4d158dbc771 100644
--- a/ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch
+++ b/ext/lexbor/patches/0005-Shrink-size-of-static-binary-search-tree.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Wed, 29 Nov 2023 21:29:31 +0100
-Subject: [PATCH 05/21] Shrink size of static binary search tree
+Subject: [PATCH 05/22] Shrink size of static binary search tree

 This also makes it more efficient on the data cache.
 ---
diff --git a/ext/lexbor/patches/0006-Patch-out-unused-CSS-style-code.patch b/ext/lexbor/patches/0006-Patch-out-unused-CSS-style-code.patch
index 5d63d17116f..b95a36de6f4 100644
--- a/ext/lexbor/patches/0006-Patch-out-unused-CSS-style-code.patch
+++ b/ext/lexbor/patches/0006-Patch-out-unused-CSS-style-code.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com>
 Date: Sun, 7 Jan 2024 21:59:28 +0100
-Subject: [PATCH 06/21] Patch out unused CSS style code
+Subject: [PATCH 06/22] Patch out unused CSS style code

 ---
  source/lexbor/css/rule.h | 2 ++
diff --git a/ext/lexbor/patches/0007-URL-fixed-setters-for-empty-hosts.patch b/ext/lexbor/patches/0007-URL-fixed-setters-for-empty-hosts.patch
index 3e69d07f95c..7dc2f925b6d 100644
--- a/ext/lexbor/patches/0007-URL-fixed-setters-for-empty-hosts.patch
+++ b/ext/lexbor/patches/0007-URL-fixed-setters-for-empty-hosts.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Fri, 26 Jun 2026 18:55:56 +0300
-Subject: [PATCH 07/21] URL: fixed setters for empty hosts.
+Subject: [PATCH 07/22] URL: fixed setters for empty hosts.
 MIME-Version: 1.0
 Content-Type: text/plain; charset=UTF-8
 Content-Transfer-Encoding: 8bit
diff --git a/ext/lexbor/patches/0008-URL-fixed-uninitialized-memory-in-the-path-buffer-gr.patch b/ext/lexbor/patches/0008-URL-fixed-uninitialized-memory-in-the-path-buffer-gr.patch
index 3f7d7e1eab3..fc24314c743 100644
--- a/ext/lexbor/patches/0008-URL-fixed-uninitialized-memory-in-the-path-buffer-gr.patch
+++ b/ext/lexbor/patches/0008-URL-fixed-uninitialized-memory-in-the-path-buffer-gr.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Fri, 5 Jun 2026 22:13:32 +0300
-Subject: [PATCH 08/21] URL: fixed uninitialized memory in the path buffer
+Subject: [PATCH 08/22] URL: fixed uninitialized memory in the path buffer
  growth.

 When a path was long enough to outgrow the on-stack buffer, the first
diff --git a/ext/lexbor/patches/0009-Fix-parsing-for-URL-containing-empty-host-and-userin.patch b/ext/lexbor/patches/0009-Fix-parsing-for-URL-containing-empty-host-and-userin.patch
index c94e76622a4..d45d0b75886 100644
--- a/ext/lexbor/patches/0009-Fix-parsing-for-URL-containing-empty-host-and-userin.patch
+++ b/ext/lexbor/patches/0009-Fix-parsing-for-URL-containing-empty-host-and-userin.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
 Date: Thu, 9 Jul 2026 21:51:05 +0200
-Subject: [PATCH 09/21] Fix parsing for URL containing empty host and userinfo
+Subject: [PATCH 09/22] Fix parsing for URL containing empty host and userinfo

 The returned error code (LXB_URL_ERROR_TYPE_INVALID_CREDENTIALS) apparently contradicts the specification:

diff --git a/ext/lexbor/patches/0010-Percent-encode-the-caret-in-the-path.patch b/ext/lexbor/patches/0010-Percent-encode-the-caret-in-the-path.patch
index 69156c42159..7ff8d6f9582 100644
--- a/ext/lexbor/patches/0010-Percent-encode-the-caret-in-the-path.patch
+++ b/ext/lexbor/patches/0010-Percent-encode-the-caret-in-the-path.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
 Date: Fri, 10 Jul 2026 22:31:16 +0200
-Subject: [PATCH 10/21] Percent-encode the caret in the path
+Subject: [PATCH 10/22] Percent-encode the caret in the path

 The caret (^) is part of the path percent-encode set:

diff --git a/ext/lexbor/patches/0011-CSS-fixed-heap-buffer-overflow-in-lexbor-contains-pa.patch b/ext/lexbor/patches/0011-CSS-fixed-heap-buffer-overflow-in-lexbor-contains-pa.patch
index 32d52517acb..975f400b98f 100644
--- a/ext/lexbor/patches/0011-CSS-fixed-heap-buffer-overflow-in-lexbor-contains-pa.patch
+++ b/ext/lexbor/patches/0011-CSS-fixed-heap-buffer-overflow-in-lexbor-contains-pa.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Fri, 5 Jun 2026 22:34:23 +0300
-Subject: [PATCH 11/21] CSS: fixed heap buffer overflow in :lexbor-contains()
+Subject: [PATCH 11/22] CSS: fixed heap buffer overflow in :lexbor-contains()
  parsing.

 The contains string buffer was allocated by the size of the string
diff --git a/ext/lexbor/patches/0012-Encoding-fixed-buffer-overflows-in-malformed-decode-.patch b/ext/lexbor/patches/0012-Encoding-fixed-buffer-overflows-in-malformed-decode-.patch
index 6214cfbda10..bc243bc250e 100644
--- a/ext/lexbor/patches/0012-Encoding-fixed-buffer-overflows-in-malformed-decode-.patch
+++ b/ext/lexbor/patches/0012-Encoding-fixed-buffer-overflows-in-malformed-decode-.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Wed, 10 Jun 2026 19:50:10 +0300
-Subject: [PATCH 12/21] Encoding: fixed buffer overflows in malformed decode
+Subject: [PATCH 12/22] Encoding: fixed buffer overflows in malformed decode
  replay.

 Fixed out-of-bounds writes in buffering decoders when replacement output
diff --git a/ext/lexbor/patches/0013-URL-fixed-tail-pointer-in-URLSearchParams-for-delimi.patch b/ext/lexbor/patches/0013-URL-fixed-tail-pointer-in-URLSearchParams-for-delimi.patch
index 89adb8132fc..38a36be323e 100644
--- a/ext/lexbor/patches/0013-URL-fixed-tail-pointer-in-URLSearchParams-for-delimi.patch
+++ b/ext/lexbor/patches/0013-URL-fixed-tail-pointer-in-URLSearchParams-for-delimi.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Fri, 5 Jun 2026 21:46:26 +0300
-Subject: [PATCH 13/21] URL: fixed tail pointer in URLSearchParams for
+Subject: [PATCH 13/22] URL: fixed tail pointer in URLSearchParams for
  delimiter-free query.

 When a query had a single token without '=' or '&' (e.g. "?abc"), the
diff --git a/ext/lexbor/patches/0014-URL-Fix-parsing-of-usernames-containing.patch b/ext/lexbor/patches/0014-URL-Fix-parsing-of-usernames-containing.patch
index 9aa3b403994..9e1cc2fd442 100644
--- a/ext/lexbor/patches/0014-URL-Fix-parsing-of-usernames-containing.patch
+++ b/ext/lexbor/patches/0014-URL-Fix-parsing-of-usernames-containing.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= <tim@bastelstu.be>
 Date: Fri, 31 Jul 2026 20:56:18 +0200
-Subject: [PATCH 14/21] URL: Fix parsing of usernames containing `@`
+Subject: [PATCH 14/22] URL: Fix parsing of usernames containing `@`

 Fixes lexbor/lexbor#399.
 ---
diff --git a/ext/lexbor/patches/0015-Unicode-fixed-uninitialized-memory-in-IDNA-buffer-gr.patch b/ext/lexbor/patches/0015-Unicode-fixed-uninitialized-memory-in-IDNA-buffer-gr.patch
index 177022aaa53..a50bd4d5a25 100644
--- a/ext/lexbor/patches/0015-Unicode-fixed-uninitialized-memory-in-IDNA-buffer-gr.patch
+++ b/ext/lexbor/patches/0015-Unicode-fixed-uninitialized-memory-in-IDNA-buffer-gr.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Mon, 7 Sep 2026 22:11:39 +0300
-Subject: [PATCH 15/21] Unicode: fixed uninitialized memory in IDNA buffer
+Subject: [PATCH 15/22] Unicode: fixed uninitialized memory in IDNA buffer
  growth.

 When an IDNA buffer outgrew the stack allocation, the move to the heap
diff --git a/ext/lexbor/patches/0016-URL-encode-opaque-path-spaces-before-query-and-fragm.patch b/ext/lexbor/patches/0016-URL-encode-opaque-path-spaces-before-query-and-fragm.patch
index 4484e113231..bcf14d13987 100644
--- a/ext/lexbor/patches/0016-URL-encode-opaque-path-spaces-before-query-and-fragm.patch
+++ b/ext/lexbor/patches/0016-URL-encode-opaque-path-spaces-before-query-and-fragm.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
 Date: Fri, 11 Sep 2026 21:58:45 +0200
-Subject: [PATCH 16/21] URL: encode opaque path spaces before query and
+Subject: [PATCH 16/22] URL: encode opaque path spaces before query and
  fragment. (#405)
 MIME-Version: 1.0
 Content-Type: text/plain; charset=UTF-8
diff --git a/ext/lexbor/patches/0017-URL-Fix-lxb_url_serialize_fragment-without-a-query-4.patch b/ext/lexbor/patches/0017-URL-Fix-lxb_url_serialize_fragment-without-a-query-4.patch
index 98139927410..b645c0024a8 100644
--- a/ext/lexbor/patches/0017-URL-Fix-lxb_url_serialize_fragment-without-a-query-4.patch
+++ b/ext/lexbor/patches/0017-URL-Fix-lxb_url_serialize_fragment-without-a-query-4.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= <tim@tideways-gmbh.com>
 Date: Fri, 11 Sep 2026 22:45:33 +0200
-Subject: [PATCH 17/21] URL: Fix `lxb_url_serialize_fragment()` without a query
+Subject: [PATCH 17/22] URL: Fix `lxb_url_serialize_fragment()` without a query
  (#410)

 The `lxb_url_serialize_fragment()` function checked if the query contained data
diff --git a/ext/lexbor/patches/0018-URL-expose-component-reset-functions.-415.patch b/ext/lexbor/patches/0018-URL-expose-component-reset-functions.-415.patch
index c259de69dff..ed27f5fe6c7 100644
--- a/ext/lexbor/patches/0018-URL-expose-component-reset-functions.-415.patch
+++ b/ext/lexbor/patches/0018-URL-expose-component-reset-functions.-415.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
 Date: Tue, 22 Sep 2026 22:20:28 +0200
-Subject: [PATCH 18/21] URL: expose component reset functions. (#415)
+Subject: [PATCH 18/22] URL: expose component reset functions. (#415)

 * URL: expose component reset functions.

diff --git a/ext/lexbor/patches/0019-URL-Fix-parsing-of-query-and-fragment-after-a-dot-co.patch b/ext/lexbor/patches/0019-URL-Fix-parsing-of-query-and-fragment-after-a-dot-co.patch
index c2dd006b4b8..c1cd41a6b75 100644
--- a/ext/lexbor/patches/0019-URL-Fix-parsing-of-query-and-fragment-after-a-dot-co.patch
+++ b/ext/lexbor/patches/0019-URL-Fix-parsing-of-query-and-fragment-after-a-dot-co.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= <tim@tideways-gmbh.com>
 Date: Sat, 26 Sep 2026 18:41:05 +0200
-Subject: [PATCH 19/21] URL: Fix parsing of query and fragment after a
+Subject: [PATCH 19/22] URL: Fix parsing of query and fragment after a
  dot-component in path (#411)
 MIME-Version: 1.0
 Content-Type: text/plain; charset=UTF-8
diff --git a/ext/lexbor/patches/0020-URL-Keep-replacement-file-drive-paths-hierarchical-4.patch b/ext/lexbor/patches/0020-URL-Keep-replacement-file-drive-paths-hierarchical-4.patch
index 9871cb65064..22d897de5c6 100644
--- a/ext/lexbor/patches/0020-URL-Keep-replacement-file-drive-paths-hierarchical-4.patch
+++ b/ext/lexbor/patches/0020-URL-Keep-replacement-file-drive-paths-hierarchical-4.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: =?UTF-8?q?M=C3=A1t=C3=A9=20Kocsis?= <kocsismate@woohoolabs.com>
 Date: Mon, 21 Sep 2026 20:01:43 +0200
-Subject: [PATCH 20/21] URL: Keep replacement file drive paths hierarchical
+Subject: [PATCH 20/22] URL: Keep replacement file drive paths hierarchical
  (#423)

 WHATWG file state (https://url.spec.whatwg.org/#file-state) step 4.4.3.2 resets the path to an empty list. Do not mark it opaque, as that makes subsequent pathname updates silently do nothing.
diff --git a/ext/lexbor/patches/0021-URL-normalize-output-encoding-for-percent-encoding.patch b/ext/lexbor/patches/0021-URL-normalize-output-encoding-for-percent-encoding.patch
index 715d7c8241d..c1fe49461a2 100644
--- a/ext/lexbor/patches/0021-URL-normalize-output-encoding-for-percent-encoding.patch
+++ b/ext/lexbor/patches/0021-URL-normalize-output-encoding-for-percent-encoding.patch
@@ -1,7 +1,7 @@
 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
 From: Alexander Borisov <lex.borisov@gmail.com>
 Date: Thu, 24 Sep 2026 15:05:51 +0300
-Subject: [PATCH 21/21] URL: normalize output encoding for percent-encoding.
+Subject: [PATCH 21/22] URL: normalize output encoding for percent-encoding.

 ---
  source/lexbor/url/url.c | 34 +++++++++++++++++++++++++---------
diff --git a/ext/lexbor/patches/0022-HTML-fixed-crash-use-after-free-and-leaks-on-allocat.patch b/ext/lexbor/patches/0022-HTML-fixed-crash-use-after-free-and-leaks-on-allocat.patch
new file mode 100644
index 00000000000..14f242091a5
--- /dev/null
+++ b/ext/lexbor/patches/0022-HTML-fixed-crash-use-after-free-and-leaks-on-allocat.patch
@@ -0,0 +1,94 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: sjanel <67502353+sjanel@users.noreply.github.com>
+Date: Wed, 11 Nov 2026 20:26:42 +0100
+Subject: [PATCH 22/22] HTML: fixed crash, use-after-free and leaks on
+ allocation failure in document create and parse (#448)
+
+HTML: fixed crash, use-after-free and leaks on allocation failure.
+---
+ source/lexbor/dom/interfaces/document.c  | 21 +++++++++++++--------
+ source/lexbor/html/interfaces/document.c |  2 +-
+ source/lexbor/html/parser.c              |  6 ++++++
+ 3 files changed, 20 insertions(+), 9 deletions(-)
+
+diff --git a/source/lexbor/dom/interfaces/document.c b/source/lexbor/dom/interfaces/document.c
+index 43eeba0..6428354 100644
+--- a/source/lexbor/dom/interfaces/document.c
++++ b/source/lexbor/dom/interfaces/document.c
+@@ -124,6 +124,12 @@ lxb_dom_document_init(lxb_dom_document_t *document, lxb_dom_document_t *owner,
+
+     document->css = NULL;
+
++    /*
++     * Set before the allocations: lxb_dom_document_destroy() frees a document
++     * that owns itself, and dereferences the owner of any other.
++     */
++    node->owner_document = document;
++
+     /* For nodes */
+     document->mraw = lexbor_mraw_create();
+     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,
+         goto failed;
+     }
+
+-    node->owner_document = document;
+-
+     return LXB_STATUS_OK;
+
+ failed:
+
+-    lexbor_mraw_destroy(document->mraw, true);
+-    lexbor_mraw_destroy(document->text, true);
+-    lexbor_hash_destroy(document->tags, true);
+-    lexbor_hash_destroy(document->ns, true);
+-    lexbor_hash_destroy(document->attrs, true);
+-    lexbor_hash_destroy(document->prefix, true);
++    /* Reset, so that lxb_dom_document_destroy() does not free them again. */
++    document->mraw = lexbor_mraw_destroy(document->mraw, true);
++    document->text = lexbor_mraw_destroy(document->text, true);
++    document->tags = lexbor_hash_destroy(document->tags, true);
++    document->ns = lexbor_hash_destroy(document->ns, true);
++    document->attrs = lexbor_hash_destroy(document->attrs, true);
++    document->prefix = lexbor_hash_destroy(document->prefix, true);
+
+     return LXB_STATUS_ERROR;
+ }
+diff --git a/source/lexbor/html/interfaces/document.c b/source/lexbor/html/interfaces/document.c
+index b40c6c8..5059050 100644
+--- a/source/lexbor/html/interfaces/document.c
++++ b/source/lexbor/html/interfaces/document.c
+@@ -278,7 +278,7 @@ lxb_html_document_parser_prepare(lxb_html_document_t *document)
+         status = lxb_html_parser_init(doc->parser);
+
+         if (status != LXB_STATUS_OK) {
+-            lxb_html_parser_destroy(doc->parser);
++            doc->parser = lxb_html_parser_destroy(doc->parser);
+             return status;
+         }
+
+diff --git a/source/lexbor/html/parser.c b/source/lexbor/html/parser.c
+index 7d05104..62e7370 100644
+--- a/source/lexbor/html/parser.c
++++ b/source/lexbor/html/parser.c
+@@ -40,7 +40,12 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
+     parser->tkz = lxb_html_tokenizer_create();
+     lxb_status_t status = lxb_html_tokenizer_init(parser->tkz);
+
++    /*
++     * A tokenizer or a tree that failed to initialize has no reference yet,
++     * so lxb_html_parser_destroy() could not release it: destroy it here.
++     */
+     if (status != LXB_STATUS_OK) {
++        parser->tkz = lxb_html_tokenizer_destroy(parser->tkz);
+         return status;
+     }
+
+@@ -51,6 +56,7 @@ lxb_html_parser_init(lxb_html_parser_t *parser)
+     status = lxb_html_tree_init(parser->tree, parser->tkz);
+
+     if (status != LXB_STATUS_OK) {
++        parser->tree = lxb_html_tree_destroy(parser->tree);
+         return status;
+     }
+