1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-10-23 01:52:48 +03:00
Commit Graph

536 Commits

Author SHA1 Message Date
Nick Wellnhofer
d0d6174e81 valid: Rework xmlAddID 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
d57c57ed2f tree: Improve argument check in xmlTextConcat 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
16c2955733 tree: Remove unused node types 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
f960c60d18 tree: Make namespace comparison more consistent
The API allows NULL namespace URIs, so we should match them
consistently. Simply use xmlStrEqual which already takes NULL strings
into account.
2024-03-15 19:47:07 +01:00
Nick Wellnhofer
d1cc6f7df2 tree: Don't allow NULL name in xmlSetNsProp 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
2840e33c5e tree: Allocate XML namespace statically 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
696faeb474 tree: Rework xmlNodeListGetString
Use string buffer to avoid quadratic complexity.

Handle entities with xmlBufGetNodeContent.

Report malloc failures.
2024-03-15 19:47:07 +01:00
Nick Wellnhofer
4196454818 tree: Rework xmlTextMerge
Return NULL on error. Check for malloc failure. Check that nodes are
distinct.
2024-03-15 19:47:07 +01:00
Nick Wellnhofer
a3713f78e3 tree: Rework xmlNodeSetName
Disallow xmlNodeSetName on DTD nodes. DTD nodes don't store the name in
a dictionary. Calling xmlNodeSetName with a DTD node could result in an
invalid free.

This function doesn't report errors but we can make sure that name
isn't set to NULL.
2024-03-15 19:47:07 +01:00
Nick Wellnhofer
77c713501b tree: Simplify xmlAddChild with text parent 2024-03-15 19:47:07 +01:00
Nick Wellnhofer
7e46242568 tree: Don't allow misuse of xmlAddChild
xmlAddChild assumes that the child is unlinked. If the child is already
linked, return an error instead of corrupting the tree.
2024-03-15 19:47:07 +01:00
Nick Wellnhofer
2c214a508d tree: Fix xmlAddPropSibling with duplicate attributes
Look up existing attribute before unlinking new attribute. This makes
it easier for the fuzzer to detect which attribute will de deleted if
there are multiple attributes with the same name.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
2e7650830d tree: Fix indentation in xmlAddPropSibling 2024-03-15 19:47:06 +01:00
Nick Wellnhofer
16c0374a7d tree: Fix xmlAddSibling with last sibling
If the node to be added was already at the correct position, the tree
could be corrupted.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
74ca2f5952 tree: Move type check in xmlAddChild
Avoid aborting halfway after changing parent pointer if node types
don't match when adding attributes.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
29db988165 tree: Fix xmlDocSetRootElement with multiple top-level elements
Fix xmlDocSetRootElement when setting the original root if multiple
top-level elements are present.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
4b698dbaec tree: Only allow elements in xmlDocSetRootElement 2024-03-15 19:47:06 +01:00
Nick Wellnhofer
d5f5060246 tree: Disallow setting content of entity reference nodes
The content of entity reference nodes points to the entity declaration
and isn't freed. Changing the content would result in a memory leak.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
77f2012ca7 tree: Rework xmlReconciliateNs 2024-03-15 19:47:06 +01:00
Nick Wellnhofer
af66a6b59b tree: Unlink DTD in xmlStaticCopyNodeList
Avoid tree corruption when copying within a document.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
bb22cfb900 tree: Unlink DTD in xmlFreeNodeList
Avoid dangling next/prev pointers.
2024-03-15 19:47:06 +01:00
Nick Wellnhofer
a581f65194 tree: Check for integer overflow in xmlStringGetNodeList
This function is called with unvalidated strings from functions like
xmlNewDocProp, xmlNewDocNode or xmlNodeSetContent, so we have to check
for integer overflow after all.
2024-02-21 12:12:30 +01:00
Nick Wellnhofer
6aae176798 tree: Fix error condition in xmlNodeListGetString
Don't return NULL in case of undeclared entities.
2024-02-01 15:18:26 +01:00
Nick Wellnhofer
d025cfbb4b parser: Always copy content from entity to target.
Make sure that references from IDs are updated.

Note that if there are IDs with the same value in a document, the last
one will now be returned. IDs should be unique, but maybe this should be
addressed.
2023-12-29 01:22:11 +01:00
Nick Wellnhofer
c49572e57d malloc-fail: Fix erroneous report in xmlStringGetNodeList
The parser can produce invalid attribute content in recovery mode.
Unless this is fixed, xmlStringGetNodeList should ignore such errors
silently.
2023-12-23 15:10:15 +01:00
Nick Wellnhofer
0ea47327c2 malloc-fail: Fix memory leak in xmlNodeGetBaseSafe
Short-lived regression.
2023-12-13 14:58:53 +01:00
Nick Wellnhofer
5c06f4e384 malloc-fail: Fix erroneous reports in xmlNodeListGetString
Short-lived regression.
2023-12-12 15:19:07 +01:00
Nick Wellnhofer
aca16fb3d4 tree: Report malloc failures
Fix many places where malloc failures aren't reported.

Make some API function return an error code. Changing the return type
from void to int is technically an ABI break but should be safe on most
platforms.

- xmlNodeSetContent
- xmlNodeSetContentLen
- xmlNodeAddContent
- xmlNodeAddContentLen
- xmlNodeSetBase

Introduce new API functions that return a separate error code if a
memory allocation fails.

- xmlNodeGetAttrValue
- xmlNodeGetBaseSafe
- xmlGetNsListSafe

Introduce private functions xmlTreeEnsureXMLDecl and xmlSplitQName4.

Don't report low-level errors to the global error handler.

Fix tree

Introduce xmlGetNsListSafe

Fix tree
2023-12-11 22:13:05 +01:00
Nick Wellnhofer
502971cc23 tree: Another fix related to #538
Should fix #639.
2023-12-01 19:44:37 +01:00
Nick Wellnhofer
8707838e69 tree: Fix #583 again
Only set doc->intSubset after successful copy to avoid dangling pointers
in error case.
2023-11-28 13:45:49 +01:00
Nick Wellnhofer
de3f70146d tree: Fix regression when copying DTDs
This reverts commit d39f78069d.

Fixes #634.
2023-11-28 13:30:56 +01:00
Nick Wellnhofer
97e99f4112 parser: Acknowledge that entities with namespaces are broken
Entities which reference out-of-scope namespace have always been broken.
xmlParseBalancedChunkMemoryInternal tried to reuse the namespaces
currently in scope but these namespaces were ignored by the SAX handler.
Besides, there could be different namespaces in scope when expanding the
entity again. For example:

    <!DOCTYPE doc [
      <!ENTITY ent "<ns:elem/>">
    ]>
    <doc>
      <decl1 xmlns:ns="urn:ns1">
        &ent;
      </decl1>
      <decl2 xmlns:ns="urn:ns2">
        &ent;
      </decl2>
    </doc>

Add some comments outlining possible solutions to this problem.

For now, we stop copying namespaces to the temporary parser context
in xmlParseBalancedChunkMemoryInternal. This has never really worked
and the recent changes contained a partial fix which uncovered other
problems like a use-after-free with the XML Reader interface, found
by OSS-Fuzz.
2023-10-05 17:41:46 +02:00
Nick Wellnhofer
8c084ebdc7 doc: Make apibuild.py happy 2023-09-21 22:57:33 +02:00
Nick Wellnhofer
9b5cce7a71 include: Remove more unnecessary includes 2023-09-21 01:50:53 +02:00
Nick Wellnhofer
11a1839ddd globals: Move remaining globals back to correct header files
This undoes a lot of damage.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
dc3382ef97 globals: Move xmlRegisterNodeDefault to tree.c
Code in globals.c must not try to access globals itself since the
accessor macros aren't defined and we would only see the main
variable.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
4e1c13ebfd debug: Remove debugging code
This is barely useful these days and only clutters the code base.
2023-09-19 17:35:09 +02:00
Nick Wellnhofer
d39f78069d tree: Fix copying of DTDs
- Don't create multiple DTD nodes.
- Fix UAF if malloc fails.
- Skip DTD nodes if tree module is disabled.

Fixes #583.
2023-08-23 20:43:14 +02:00
Nick Wellnhofer
b8961df65d SAX: Always validate xml:ids
The behavior shouldn't depend on mostly random configuration options.
2023-05-09 03:25:24 +02:00
Nick Wellnhofer
dbc893f588 malloc-fail: Fix memory leak in xmlCopyNamespaceList
Found with libFuzzer, see #344.
2023-03-08 13:17:47 +01:00
Nick Wellnhofer
a442d16a5f malloc-fail: Fix memory leak in xmlGetNsList
Found with libFuzzer, see #344.
2023-02-27 17:18:02 +01:00
Nick Wellnhofer
bc7740b3c3 malloc-fail: Fix memory leak in xmlCopyPropList
Found with libFuzzer, see #344.
2023-02-17 17:16:52 +01:00
Nick Wellnhofer
e6401b68df tree: Fix recursion check in xmlStringGetNodeList
Use the new entity flag to check for recursion.
2023-01-17 14:01:23 +01:00
Nick Wellnhofer
481d79d44c entities: Add XML_ENT_PARSED flag
To check whether an entity was already parsed, the code previously
tested whether "checked" was non-zero or "children" was non-null. The
"children" check could be unreliable because an empty entity also
results in an empty (NULL) node list. Use a separate flag to make this
check more reliable.
2022-12-19 15:26:46 +01:00
Nick Wellnhofer
2059df5358 buf: Deprecate static/immutable buffers 2022-11-20 21:16:03 +01:00
Nick Wellnhofer
b45927095e malloc-fail: Fix memory leak in xmlStringGetNodeList
Also make sure to return NULL on error instead of a partial node list.

Found with libFuzzer, see #344.
2022-11-02 16:22:54 +01:00
Nick Wellnhofer
dd50cfeb61 malloc-fail: Fix memory leak in xmlNewDocNodeEatName
Found with libFuzzer, see #344.
2022-11-02 15:58:31 +01:00
Nick Wellnhofer
fa361de0b7 malloc-fail: Fix memory leak in xmlNewPropInternal
Also fixes a memory leak if called with a non-element node.

Found with libFuzzer, see #344.
2022-11-02 15:57:54 +01:00
Nick Wellnhofer
a22bd982bf malloc-fail: Fix memory leak in xmlStaticCopyNodeList
Found with libFuzzer, see #344.
2022-11-02 15:57:53 +01:00
Nick Wellnhofer
2fc8d12327 xinclude: Make xmlXIncludeCopyNode non-recursive
Avoid call stack overflows.

Also switch to xmlStaticCopyNode which avoids duplicate namespace
definitions.
2022-10-23 18:52:56 +02:00