Skip to content

Commit f04e505

Browse files
committed
Avoid allocations in DOMElement::getAttribute()
1 parent 9880c33 commit f04e505

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

ext/dom/element.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ PHP_METHOD(DOMElement, getAttribute)
205205
dom_object *intern;
206206
xmlNodePtr attr;
207207
size_t name_len;
208+
bool should_free;
208209

209210
id = ZEND_THIS;
210211
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &name, &name_len) == FAILURE) {
@@ -218,20 +219,25 @@ PHP_METHOD(DOMElement, getAttribute)
218219
switch (attr->type) {
219220
case XML_ATTRIBUTE_NODE:
220221
value = xmlNodeListGetString(attr->doc, attr->children, 1);
222+
should_free = true;
221223
break;
222224
case XML_NAMESPACE_DECL:
223-
value = xmlStrdup(((xmlNsPtr)attr)->href);
225+
value = (xmlChar *) ((xmlNsPtr)attr)->href;
226+
should_free = false;
224227
break;
225228
default:
226-
value = xmlStrdup(((xmlAttributePtr)attr)->defaultValue);
229+
value = (xmlChar *) ((xmlAttributePtr)attr)->defaultValue;
230+
should_free = false;
227231
}
228232
}
229233

230234
if (value == NULL) {
231235
RETURN_EMPTY_STRING();
232236
} else {
233237
RETVAL_STRING((char *)value);
234-
xmlFree(value);
238+
if (should_free) {
239+
xmlFree(value);
240+
}
235241
}
236242
}
237243
/* }}} end dom_element_get_attribute */

0 commit comments

Comments
 (0)