You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-commits@axis.apache.org by ro...@apache.org on 2026/01/14 16:00:23 UTC

(axis-axis2-c-core) 07/07: AXIS2C-1701: Fix memory leak on duplicate XML namespace declaration

This is an automated email from the ASF dual-hosted git repository.

robertlazarski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/axis-axis2-c-core.git

commit fd12cc34de036c1657a33486b527dae4ef626bdf
Author: Robert Lazarski <ro...@gmail.com>
AuthorDate: Wed Jan 14 05:55:25 2026 -1000

    AXIS2C-1701: Fix memory leak on duplicate XML namespace declaration
    
    When parsing XML with namespace declarations that duplicate namespaces from
    parent elements (e.g., xmlns="..." redeclared on child elements), the
    namespace objects would leak.
    
    The root cause was axiom_element_declare_namespace() using
    axiom_element_find_namespace() which searches the parent hierarchy. When a
    namespace was found in a parent, it returned SUCCESS without taking ownership
    of the passed namespace. The caller (om_stax_builder) assumes SUCCESS means
    the element now owns the namespace, so it wouldn't free it.
    
    Fix by using axiom_element_find_declared_namespace() which only checks the
    current element's local declarations. If a namespace is only declared in a
    parent (not locally), we now redeclare it on the current element, properly
    taking ownership via axiom_namespace_increment_ref().
    
    Co-Authored-By: Claude Opus 4.5 <no...@anthropic.com>
---
 axiom/src/om/om_element.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/axiom/src/om/om_element.c b/axiom/src/om/om_element.c
index 0f95cbb29..335cc6f3e 100644
--- a/axiom/src/om/om_element.c
+++ b/axiom/src/om/om_element.c
@@ -445,8 +445,13 @@ axiom_element_declare_namespace(
     uri = axiom_namespace_get_uri(ns, env);
     prefix = axiom_namespace_get_prefix(ns, env);
 
-    /* If namespace already declared, return */
-    declared_ns = axiom_element_find_namespace(om_element, env, node, uri, prefix);
+    /* AXIS2C-1701: Only check the current element for existing namespace declaration.
+     * Previously this used axiom_element_find_namespace() which searches parent elements
+     * too. If a namespace was found in a parent, we'd return SUCCESS without taking
+     * ownership of the passed namespace, causing a memory leak since the caller expects
+     * us to take ownership on SUCCESS. By only checking the current element, we allow
+     * redeclaration of namespaces from parents, which properly takes ownership. */
+    declared_ns = axiom_element_find_declared_namespace(om_element, env, uri, prefix);
     if (declared_ns)
         return AXIS2_SUCCESS;