Skip to content

Commit 50d899d

Browse files
committed
Merge branch 'refactoring2' of github.com:zendtech/php into refactoring2
2 parents b763baf + ef01b79 commit 50d899d

File tree

5 files changed

+90
-57
lines changed

5 files changed

+90
-57
lines changed

Zend/zend_API.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,19 +3156,20 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
31563156

31573157
if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
31583158
obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
3159-
if (UNEXPECTED(Z_ISREF_P(obj))) {
3160-
obj = Z_REFVAL_P(obj);
3161-
}
31623159
method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
3163-
if (UNEXPECTED(Z_ISREF_P(method))) {
3164-
method = Z_REFVAL_P(method);
3165-
}
31663160
}
31673161
if (obj && method &&
31683162
(Z_TYPE_P(obj) == IS_OBJECT ||
31693163
Z_TYPE_P(obj) == IS_STRING) &&
31703164
Z_TYPE_P(method) == IS_STRING) {
31713165

3166+
if (UNEXPECTED(Z_ISREF_P(obj))) {
3167+
obj = Z_REFVAL_P(obj);
3168+
}
3169+
if (UNEXPECTED(Z_ISREF_P(method))) {
3170+
method = Z_REFVAL_P(method);
3171+
}
3172+
31723173
if (Z_TYPE_P(obj) == IS_STRING) {
31733174
if (callable_name) {
31743175
char *ptr;
@@ -3235,7 +3236,9 @@ ZEND_API zend_bool zend_is_callable_ex(zval *callable, zval *object_ptr, uint ch
32353236

32363237
} else {
32373238
if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
3238-
if (!obj || (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT)) {
3239+
if (!obj || (Z_ISREF_P(obj)?
3240+
(Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT) :
3241+
(Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT))) {
32393242
if (error) zend_spprintf(error, 0, "first array member is not a valid class name or object");
32403243
} else {
32413244
if (error) zend_spprintf(error, 0, "second array member is not a valid method");

Zend/zend_API.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,18 +677,18 @@ END_EXTERN_C()
677677
\
678678
if ((orig_var = zend_hash_str_find(symtable, (name), (name_length))) != NULL \
679679
&& Z_ISREF_P(orig_var)) { \
680-
Z_SET_REFCOUNT_P(var, Z_REFCOUNT_P(orig_var)); \
681-
/*???Z_SET_ISREF_P(var);*/ \
682-
\
683-
if (_refcount) { \
684-
Z_SET_REFCOUNT_P(var, Z_REFCOUNT_P(var) + _refcount - 1); \
680+
if (Z_REFCOUNTED_P(var)) { \
681+
Z_SET_REFCOUNT_P(var, Z_REFCOUNT_P(orig_var)); \
682+
if (_refcount) { \
683+
Z_SET_REFCOUNT_P(var, Z_REFCOUNT_P(var) + _refcount - 1); \
684+
} \
685685
} \
686686
zval_dtor(orig_var); \
687687
ZVAL_COPY_VALUE(orig_var, var); \
688688
/*???FREE_ZVAL(var);*/ \
689689
} else { \
690690
/*???Z_SET_ISREF_TO_P(var, _is_ref);*/ \
691-
if (_refcount) { \
691+
if (_refcount && Z_REFCOUNTED_P(var)) { \
692692
Z_SET_REFCOUNT_P(var, _refcount); \
693693
} \
694694
zend_hash_str_update(symtable, (name), (name_length), var); \

Zend/zend_operators.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,8 @@ static void increment_string(zval *str) /* {{{ */
18681868
} else if (Z_REFCOUNT_P(str) > 1) {
18691869
Z_DELREF_P(str);
18701870
Z_STR_P(str) = STR_DUP(Z_STR_P(str), 0);
1871+
} else {
1872+
STR_FORGET_HASH_VAL(Z_STR_P(str));
18711873
}
18721874
s = Z_STRVAL_P(str);
18731875

ext/standard/array.c

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,15 @@ static int php_array_natural_general_compare(const void *a, const void *b, int f
411411

412412
fval = &f->val;
413413
sval = &s->val;
414+
415+
if (Z_ISREF_P(fval)) {
416+
fval = Z_REFVAL_P(fval);
417+
}
418+
419+
if (Z_ISREF_P(sval)) {
420+
sval = Z_REFVAL_P(sval);
421+
}
422+
414423
ZVAL_COPY_VALUE(&first, fval);
415424
ZVAL_COPY_VALUE(&second, sval);
416425

@@ -1392,7 +1401,7 @@ PHP_FUNCTION(extract)
13921401
}
13931402
} else {
13941403
ZVAL_DUP(&data, entry);
1395-
ZEND_SET_SYMBOL_WITH_LENGTH(EG(active_symbol_table), Z_STRVAL(final_name), Z_STRLEN(final_name) + 1, &data, 1, 0);
1404+
ZEND_SET_SYMBOL_WITH_LENGTH(EG(active_symbol_table), Z_STRVAL(final_name), Z_STRLEN(final_name), &data, 1, 0);
13961405
}
13971406
count++;
13981407
}
@@ -1525,6 +1534,10 @@ PHP_FUNCTION(array_fill_keys)
15251534
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(keys), &pos);
15261535
while ((entry = zend_hash_get_current_data_ex(Z_ARRVAL_P(keys), &pos)) != NULL) {
15271536

1537+
if (UNEXPECTED(Z_ISREF_P(entry))) {
1538+
entry = Z_REFVAL_P(entry);
1539+
}
1540+
15281541
if (Z_TYPE_P(entry) == IS_LONG) {
15291542
zval_add_ref(val);
15301543
zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(entry), val);
@@ -1816,7 +1829,9 @@ PHPAPI HashTable* php_splice(HashTable *in_hash, int offset, int length, zval *l
18161829
pos++;
18171830
/* Get entry and increase reference count */
18181831
entry = &p->val;
1819-
Z_ADDREF_P(entry);
1832+
if (Z_REFCOUNTED_P(entry)) {
1833+
Z_ADDREF_P(entry);
1834+
}
18201835

18211836
/* Update output hash depending on key type */
18221837
if (p->key == NULL) {
@@ -1833,7 +1848,9 @@ PHPAPI HashTable* php_splice(HashTable *in_hash, int offset, int length, zval *l
18331848
if (Z_TYPE(p->val) == IS_UNDEF) continue;
18341849
pos++;
18351850
entry = &p->val;
1836-
Z_ADDREF_P(entry);
1851+
if (Z_REFCOUNTED_P(entry)) {
1852+
Z_ADDREF_P(entry);
1853+
}
18371854
if (p->key == NULL) {
18381855
zend_hash_next_index_insert(removed, entry);
18391856
} else {
@@ -1859,7 +1876,9 @@ PHPAPI HashTable* php_splice(HashTable *in_hash, int offset, int length, zval *l
18591876
p = in_hash->arData + idx;
18601877
if (Z_TYPE(p->val) == IS_UNDEF) continue;
18611878
entry = &p->val;
1862-
if (Z_REFCOUNTED_P(entry)) Z_ADDREF_P(entry);
1879+
if (Z_REFCOUNTED_P(entry)) {
1880+
Z_ADDREF_P(entry);
1881+
}
18631882
if (p->key == NULL) {
18641883
zend_hash_next_index_insert(out_hash, entry);
18651884
} else {
@@ -2284,7 +2303,9 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src TSRMLS_DC
22842303
(dest_entry = zend_hash_find(dest, string_key)) == NULL ||
22852304
Z_TYPE_P(dest_entry) != IS_ARRAY) {
22862305

2287-
Z_ADDREF_P(src_entry);
2306+
if (Z_REFCOUNTED_P(src_entry)) {
2307+
Z_ADDREF_P(src_entry);
2308+
}
22882309
zend_hash_update(dest, string_key, src_entry);
22892310

22902311
continue;
@@ -2296,7 +2317,9 @@ PHPAPI int php_array_replace_recursive(HashTable *dest, HashTable *src TSRMLS_DC
22962317
(dest_entry = zend_hash_index_find(dest, num_key)) == NULL ||
22972318
Z_TYPE_P(dest_entry) != IS_ARRAY) {
22982319

2299-
Z_ADDREF_P(src_entry);
2320+
if (Z_REFCOUNTED_P(src_entry)) {
2321+
Z_ADDREF_P(src_entry);
2322+
}
23002323
zend_hash_index_update(dest, num_key, src_entry);
23012324

23022325
continue;
@@ -2591,16 +2614,16 @@ PHP_FUNCTION(array_column)
25912614
zkeyval = zend_hash_index_find(ht, Z_LVAL_P(zkey));
25922615
}
25932616

2594-
Z_ADDREF_P(zcolval);
2617+
if (Z_REFCOUNTED_P(zcolval)) {
2618+
Z_ADDREF_P(zcolval);
2619+
}
25952620
if (zkeyval && Z_TYPE_P(zkeyval) == IS_STRING) {
2596-
//???
25972621
add_assoc_zval(return_value, Z_STRVAL_P(zkeyval), zcolval);
25982622
} else if (zkeyval && Z_TYPE_P(zkeyval) == IS_LONG) {
25992623
add_index_zval(return_value, Z_LVAL_P(zkeyval), zcolval);
26002624
} else if (zkeyval && Z_TYPE_P(zkeyval) == IS_OBJECT) {
26012625
SEPARATE_ZVAL(zkeyval);
26022626
convert_to_string(zkeyval);
2603-
//???
26042627
add_assoc_zval(return_value, Z_STRVAL_P(zkeyval), zcolval);
26052628
} else {
26062629
add_next_index_zval(return_value, zcolval);
@@ -2785,7 +2808,7 @@ PHP_FUNCTION(array_change_key_case)
27852808
php_strtolower(new_key->val, new_key->len);
27862809
}
27872810
zend_hash_update(Z_ARRVAL_P(return_value), new_key, entry);
2788-
STR_FREE(new_key);
2811+
STR_RELEASE(new_key);
27892812
break;
27902813
}
27912814

@@ -2968,8 +2991,7 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
29682991
for (i = 0; i < argc; i++) {
29692992
if (Z_TYPE(args[i]) != IS_ARRAY) {
29702993
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d is not an array", i + 1);
2971-
RETVAL_NULL();
2972-
goto out;
2994+
RETURN_NULL();
29732995
}
29742996
}
29752997

@@ -2990,7 +3012,9 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
29903012
}
29913013
}
29923014
if (ok) {
2993-
Z_ADDREF(p->val);
3015+
if (Z_REFCOUNTED(p->val)) {
3016+
Z_ADDREF(p->val);
3017+
}
29943018
zend_hash_index_update(Z_ARRVAL_P(return_value), p->h, &p->val);
29953019
}
29963020
} else {
@@ -3005,13 +3029,13 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
30053029
}
30063030
}
30073031
if (ok) {
3008-
Z_ADDREF(p->val);
3032+
if (Z_REFCOUNTED(p->val)) {
3033+
Z_ADDREF(p->val);
3034+
}
30093035
zend_hash_update(Z_ARRVAL_P(return_value), p->key, &p->val);
30103036
}
30113037
}
30123038
}
3013-
out:
3014-
efree(args);
30153039
}
30163040
/* }}} */
30173041

@@ -3409,7 +3433,9 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
34093433
}
34103434
}
34113435
if (ok) {
3412-
Z_ADDREF(p->val);
3436+
if (Z_REFCOUNTED(p->val)) {
3437+
Z_ADDREF(p->val);
3438+
}
34133439
zend_hash_index_update(Z_ARRVAL_P(return_value), p->h, &p->val);
34143440
}
34153441
} else {
@@ -3424,7 +3450,9 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
34243450
}
34253451
}
34263452
if (ok) {
3427-
Z_ADDREF(p->val);
3453+
if (Z_REFCOUNTED(p->val)) {
3454+
Z_ADDREF(p->val);
3455+
}
34283456
zend_hash_update(Z_ARRVAL_P(return_value), p->key, &p->val);
34293457
}
34303458
}
@@ -3795,7 +3823,6 @@ PHPAPI int php_multisort_compare(const void *a, const void *b TSRMLS_DC) /* {{{
37953823
for (k = 0; k < MULTISORT_LAST; k++) \
37963824
efree(ARRAYG(multisort_flags)[k]); \
37973825
efree(arrays); \
3798-
efree(args); \
37993826
RETURN_FALSE;
38003827

38013828
/* {{{ proto bool array_multisort(array ar1 [, SORT_ASC|SORT_DESC [, SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_NATURAL|SORT_FLAG_CASE]] [, array ar2 [, SORT_ASC|SORT_DESC [, SORT_REGULAR|SORT_NUMERIC|SORT_STRING|SORT_NATURAL|SORT_FLAG_CASE]], ...])
@@ -4168,7 +4195,6 @@ PHP_FUNCTION(array_filter)
41684195
zval *operand;
41694196
zval args[2];
41704197
zval retval;
4171-
//??? zval *key = NULL;
41724198
zend_bool have_callback = 0;
41734199
long use_type = 0;
41744200
zend_string *string_key;
@@ -4190,16 +4216,7 @@ PHP_FUNCTION(array_filter)
41904216
have_callback = 1;
41914217
fci.no_separation = 0;
41924218
fci.retval = &retval;
4193-
4194-
if (use_type == ARRAY_FILTER_USE_BOTH) {
4195-
fci.param_count = 2;
4196-
//??? args[1] = &key;
4197-
} else {
4198-
fci.param_count = 1;
4199-
if (use_type == ARRAY_FILTER_USE_KEY) {
4200-
//??? args[0] = &key;
4201-
}
4202-
}
4219+
fci.param_count = 1;
42034220
}
42044221

42054222
for (zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(array), &pos);
@@ -4213,28 +4230,40 @@ PHP_FUNCTION(array_filter)
42134230
/* Set up the key */
42144231
switch (key_type) {
42154232
case HASH_KEY_IS_LONG:
4216-
ZVAL_LONG(&args[0], num_key);
4233+
if (use_type == ARRAY_FILTER_USE_BOTH) {
4234+
fci.param_count = 2;
4235+
ZVAL_LONG(&args[1], num_key);
4236+
} else if (use_type == ARRAY_FILTER_USE_KEY) {
4237+
ZVAL_LONG(&args[0], num_key);
4238+
}
42174239
break;
42184240

42194241
case HASH_KEY_IS_STRING:
4220-
ZVAL_STR(&args[0], STR_COPY(string_key));
4242+
if (use_type == ARRAY_FILTER_USE_BOTH) {
4243+
ZVAL_STR(&args[1], STR_COPY(string_key));
4244+
} else if (use_type == ARRAY_FILTER_USE_KEY) {
4245+
ZVAL_STR(&args[0], STR_COPY(string_key));
4246+
}
42214247
break;
42224248
}
42234249
}
4224-
42254250
if (use_type != ARRAY_FILTER_USE_KEY) {
42264251
ZVAL_COPY_VALUE(&args[0], operand);
42274252
}
42284253
fci.params = args;
42294254

4230-
if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
4231-
int retval_true = zend_is_true(&retval TSRMLS_CC);
4255+
if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS) {
4256+
if (!ZVAL_IS_UNDEF(&retval)) {
4257+
int retval_true = zend_is_true(&retval TSRMLS_CC);
42324258

4233-
zval_ptr_dtor(&retval);
4234-
if (use_type) {
4235-
zval_ptr_dtor(&args[0]);
4236-
}
4237-
if (!retval_true) {
4259+
zval_ptr_dtor(&retval);
4260+
if (use_type) {
4261+
zval_ptr_dtor(&args[0]);
4262+
}
4263+
if (!retval_true) {
4264+
continue;
4265+
}
4266+
} else {
42384267
continue;
42394268
}
42404269
} else {
@@ -4287,7 +4316,6 @@ PHP_FUNCTION(array_map)
42874316
for (i = 0; i < n_arrays; i++) {
42884317
if (Z_TYPE(arrays[i]) != IS_ARRAY) {
42894318
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Argument #%d should be an array", i + 2);
4290-
efree(args);
42914319
efree(array_len);
42924320
efree(array_pos);
42934321
return;
@@ -4307,7 +4335,6 @@ PHP_FUNCTION(array_map)
43074335
RETVAL_ZVAL(args[0], 1, 0);
43084336
efree(array_len);
43094337
efree(array_pos);
4310-
efree(args);
43114338
return;
43124339
}
43134340

@@ -4359,7 +4386,6 @@ PHP_FUNCTION(array_map)
43594386
if (zend_call_function(&fci, &fci_cache TSRMLS_CC) != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
43604387
php_error_docref(NULL TSRMLS_CC, E_WARNING, "An error occurred while invoking the map callback");
43614388
efree(array_len);
4362-
efree(args);
43634389
efree(array_pos);
43644390
zval_dtor(return_value);
43654391
efree(params);
@@ -4382,7 +4408,6 @@ PHP_FUNCTION(array_map)
43824408
efree(params);
43834409
efree(array_len);
43844410
efree(array_pos);
4385-
efree(args);
43864411
}
43874412
/* }}} */
43884413

ext/standard/streamsfuncs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,12 @@ PHP_FUNCTION(stream_socket_client)
120120
tv.tv_usec = conv % 1000000;
121121
#endif
122122
if (zerrno) {
123+
zerrno = Z_REFVAL_P(zerrno);
123124
zval_dtor(zerrno);
124125
ZVAL_LONG(zerrno, 0);
125126
}
126127
if (zerrstr) {
128+
zerrstr = Z_REFVAL_P(zerrstr);
127129
zval_dtor(zerrstr);
128130
ZVAL_EMPTY_STRING(zerrstr);
129131
}
@@ -156,6 +158,7 @@ PHP_FUNCTION(stream_socket_client)
156158
zval_dtor(zerrstr);
157159
//??? ZVAL_STRING(zerrstr, errstr, 0);
158160
ZVAL_STRING(zerrstr, errstr);
161+
efree(errstr);
159162
} else if (errstr) {
160163
efree(errstr);
161164
}

0 commit comments

Comments
 (0)