Skip to content

Commit 1eacd4a

Browse files
authored
Avoid unnecessary string refcounting in ext/dom (#17889)
1 parent 6181901 commit 1eacd4a

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

ext/dom/document.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,12 @@ zend_result dom_document_version_write(dom_object *obj, zval *newval)
194194
{
195195
DOM_PROP_NODE(xmlDocPtr, docp, obj);
196196

197-
/* Cannot fail because the type is either null or a string. */
198-
zend_string *str = zval_get_string(newval);
197+
/* Type is ?string */
198+
zend_string *str = Z_TYPE_P(newval) == IS_NULL ? ZSTR_EMPTY_ALLOC() : Z_STR_P(newval);
199199

200200
if (php_dom_follow_spec_intern(obj)) {
201201
if (!zend_string_equals_literal(str, "1.0") && !zend_string_equals_literal(str, "1.1")) {
202202
zend_value_error("Invalid XML version");
203-
zend_string_release_ex(str, 0);
204203
return FAILURE;
205204
}
206205
}
@@ -211,7 +210,6 @@ zend_result dom_document_version_write(dom_object *obj, zval *newval)
211210

212211
docp->version = xmlStrdup((const xmlChar *) ZSTR_VAL(str));
213212

214-
zend_string_release_ex(str, 0);
215213
return SUCCESS;
216214
}
217215

@@ -394,16 +392,15 @@ zend_result dom_document_document_uri_write(dom_object *obj, zval *newval)
394392
{
395393
DOM_PROP_NODE(xmlDocPtr, docp, obj);
396394

397-
/* Cannot fail because the type is either null or a string. */
398-
zend_string *str = zval_get_string(newval);
395+
/* Type is ?string */
396+
zend_string *str = Z_TYPE_P(newval) == IS_NULL ? ZSTR_EMPTY_ALLOC() : Z_STR_P(newval);
399397

400398
if (docp->URL != NULL) {
401399
xmlFree(BAD_CAST docp->URL);
402400
}
403401

404402
docp->URL = xmlStrdup((const xmlChar *) ZSTR_VAL(str));
405403

406-
zend_string_release_ex(str, 0);
407404
return SUCCESS;
408405
}
409406

ext/dom/node.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ zend_result dom_node_node_value_write(dom_object *obj, zval *newval)
185185
{
186186
DOM_PROP_NODE(xmlNodePtr, nodep, obj);
187187

188-
/* Cannot fail because the type is either null or a string. */
189-
zend_string *str = zval_get_string(newval);
188+
/* Type is ?string */
189+
zend_string *str = Z_TYPE_P(newval) == IS_NULL ? ZSTR_EMPTY_ALLOC() : Z_STR_P(newval);
190190

191191
/* Access to Element node is implemented as a convenience method */
192192
switch (nodep->type) {
@@ -213,7 +213,6 @@ zend_result dom_node_node_value_write(dom_object *obj, zval *newval)
213213

214214
php_libxml_invalidate_node_list_cache(obj->document);
215215

216-
zend_string_release_ex(str, 0);
217216
return SUCCESS;
218217
}
219218

ext/dom/xpath_callbacks.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,14 @@ static zend_result php_dom_xpath_callback_ns_update_method_handler(
206206
ZVAL_PTR(&registered_value, fcc);
207207

208208
if (!key) {
209-
zend_string *str = zval_try_get_string(entry);
209+
zend_string *tmp_str;
210+
zend_string *str = zval_try_get_tmp_string(entry, &tmp_str);
210211
if (str && php_dom_xpath_is_callback_name_valid_and_throw(str, name_validation, true)) {
211212
zend_hash_update(&ns->functions, str, &registered_value);
212213
if (register_func) {
213214
register_func(ctxt, namespace, str);
214215
}
215-
zend_string_release_ex(str, false);
216+
zend_tmp_string_release(tmp_str);
216217
} else {
217218
zend_fcc_dtor(fcc);
218219
efree(fcc);
@@ -445,9 +446,10 @@ static zend_result php_dom_xpath_callback_dispatch(php_dom_xpath_callbacks *xpat
445446
zval_ptr_dtor(&callback_retval);
446447
return FAILURE;
447448
} else {
448-
zend_string *str = zval_get_string(&callback_retval);
449+
zend_string *tmp_str;
450+
zend_string *str = zval_get_tmp_string(&callback_retval, &tmp_str);
449451
valuePush(ctxt, xmlXPathNewString(BAD_CAST ZSTR_VAL(str)));
450-
zend_string_release_ex(str, 0);
452+
zend_tmp_string_release(tmp_str);
451453
}
452454
zval_ptr_dtor(&callback_retval);
453455
}

0 commit comments

Comments
 (0)