Skip to content

Commit 37a685f

Browse files
committed
More fixes for nodelist array access
- testing for null property read - no zval copying if the type is already long - memory fix for master
1 parent 3e13c6d commit 37a685f

File tree

2 files changed

+54
-19
lines changed

2 files changed

+54
-19
lines changed

ext/dom/php_dom.c

+25-16
Original file line numberDiff line numberDiff line change
@@ -1679,16 +1679,30 @@ xmlNsPtr dom_get_nsdecl(xmlNode *node, xmlChar *localName) {
16791679
}
16801680
/* }}} end dom_get_nsdecl */
16811681

1682+
static inline long dom_get_long(zval *offset) /* {{{ */
1683+
{
1684+
if (Z_TYPE_P(offset) == IS_LONG) {
1685+
return Z_LVAL_P(offset);
1686+
} else {
1687+
zval tmp;
1688+
1689+
MAKE_COPY_ZVAL(&offset, &tmp);
1690+
convert_to_long(&tmp);
1691+
1692+
return Z_LVAL(tmp);
1693+
}
1694+
}
1695+
/* }}} */
1696+
16821697
zval *dom_nodelist_read_dimension(zval *object, zval *offset, int type TSRMLS_DC) /* {{{ */
16831698
{
1684-
zval *rv, offset_copy;
1699+
zval *rv, offset_copy = zval_used_for_init;
16851700

16861701
if (!offset) {
16871702
return NULL;
16881703
}
16891704

1690-
MAKE_COPY_ZVAL(&offset, &offset_copy);
1691-
convert_to_long(&offset_copy);
1705+
ZVAL_LONG(&offset_copy, dom_get_long(offset));
16921706

16931707
zend_call_method_with_1_params(&object, Z_OBJCE_P(object), NULL, "item", &rv, &offset_copy);
16941708

@@ -1699,23 +1713,18 @@ zval *dom_nodelist_read_dimension(zval *object, zval *offset, int type TSRMLS_DC
16991713

17001714
int dom_nodelist_has_dimension(zval *object, zval *member, int check_empty TSRMLS_DC)
17011715
{
1702-
zval *length, offset_copy;
1703-
int ret;
1704-
1705-
MAKE_COPY_ZVAL(&member, &offset_copy);
1706-
convert_to_long(&offset_copy);
1716+
long offset = dom_get_long(member);
17071717

1708-
if (Z_LVAL(offset_copy) < 0) {
1718+
if (offset < 0) {
17091719
return 0;
1710-
}
1711-
1712-
length = zend_read_property(Z_OBJCE_P(object), object, "length", sizeof("length") - 1, 0 TSRMLS_CC);
1713-
1714-
ret = Z_LVAL(offset_copy) < Z_LVAL_P(length);
1720+
} else {
1721+
zval *length = zend_read_property(Z_OBJCE_P(object), object, "length", sizeof("length") - 1, 0 TSRMLS_CC);
1722+
int ret = length && offset < Z_LVAL_P(length);
17151723

1716-
FREE_ZVAL(length);
1724+
FREE_ZVAL(length);
17171725

1718-
return ret;
1726+
return ret;
1727+
}
17191728
} /* }}} end dom_nodelist_has_dimension */
17201729

17211730
#endif /* HAVE_DOM */

ext/dom/tests/bug67949.phpt

+29-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,21 @@ var_dump($nodes[0]->textContent);
2222
var_dump($nodes[1]->textContent);
2323

2424
echo "testing offset not a long\n";
25-
$offset = 'test';
25+
$offset = ['test'];
26+
var_dump($offset);
27+
var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
28+
var_dump($offset);
29+
30+
$something = 'test';
31+
$offset = &$something;
32+
33+
var_dump($offset);
34+
var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
2635
var_dump($offset);
27-
var_dump($nodes[$offset]->textContent);
36+
37+
$offset = 'test';
2838
var_dump($offset);
29-
var_dump(isset($nodes[$offset]));
39+
var_dump(isset($nodes[$offset]), $nodes[$offset]->textContent);
3040
var_dump($offset);
3141

3242
echo "testing read_dimension with null offset\n";
@@ -49,13 +59,29 @@ string(4) "data"
4959
Notice: Trying to get property of non-object in %s on line %d
5060
NULL
5161
testing offset not a long
62+
array(1) {
63+
[0]=>
64+
string(4) "test"
65+
}
66+
67+
Notice: Trying to get property of non-object in %s on line %d
68+
bool(false)
69+
NULL
70+
array(1) {
71+
[0]=>
72+
string(4) "test"
73+
}
5274
string(4) "test"
75+
bool(true)
5376
string(4) "data"
5477
string(4) "test"
78+
string(4) "test"
5579
bool(true)
80+
string(4) "data"
5681
string(4) "test"
5782
testing read_dimension with null offset
5883
NULL
5984
testing attribute access
6085
string(4) "href"
6186
==DONE==
87+

0 commit comments

Comments
 (0)