@@ -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
173177failed :
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}
0 commit comments