Skip to content

Commit 02ed2e2

Browse files
Avoid addref/delref where possible
1 parent 34132a7 commit 02ed2e2

File tree

4 files changed

+46
-74
lines changed

4 files changed

+46
-74
lines changed

Zend/zend_execute.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2635,6 +2635,7 @@ static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *d
26352635
GC_ADDREF(ht);
26362636
}
26372637

2638+
value->str = ZSTR_EMPTY_ALLOC();
26382639
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
26392640

26402641
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
@@ -2645,8 +2646,6 @@ static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *d
26452646
if (EG(exception)) {
26462647
return IS_NULL;
26472648
}
2648-
2649-
value->str = ZSTR_EMPTY_ALLOC();
26502649
return IS_STRING;
26512650
case IS_DOUBLE:
26522651
value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
@@ -2722,6 +2721,7 @@ static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval
27222721
GC_ADDREF(ht);
27232722
}
27242723

2724+
value->str = ZSTR_EMPTY_ALLOC();
27252725
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
27262726

27272727
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
@@ -2733,7 +2733,6 @@ static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval
27332733
if (EG(exception)) {
27342734
return IS_NULL;
27352735
}
2736-
value->str = ZSTR_EMPTY_ALLOC();
27372736
return IS_STRING;
27382737
case IS_DOUBLE:
27392738
value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
@@ -3230,6 +3229,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable
32303229
GC_ADDREF(ht);
32313230
}
32323231

3232+
zval *result = zend_hash_find_known_hash(ht, ZSTR_EMPTY_ALLOC());
32333233
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
32343234

32353235
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
@@ -3241,7 +3241,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable
32413241
return NULL;
32423242
}
32433243

3244-
return zend_hash_find_known_hash(ht, ZSTR_EMPTY_ALLOC());
3244+
return result;
32453245
} else if (Z_TYPE_P(offset) == IS_FALSE) {
32463246
hval = 0;
32473247
goto num_idx;

ext/opcache/jit/zend_jit_helpers.c

Lines changed: 40 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -500,33 +500,16 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_r_helper(zend_array *ht, zval *dim,
500500
}
501501
ZEND_FALLTHROUGH;
502502
case IS_NULL:
503-
/* The array may be destroyed while throwing the notice.
504-
* Temporarily increase the refcount to detect this situation. */
505-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
506-
GC_ADDREF(ht);
503+
retval = zend_hash_find(ht, ZSTR_EMPTY_ALLOC());
504+
if (!retval) {
505+
zend_error(E_WARNING, "Undefined array key \"\"");
506+
ZVAL_NULL(result);
507+
} else {
508+
ZVAL_COPY_DEREF(result, retval);
507509
}
508-
execute_data = EG(current_execute_data);
509-
opline = EX(opline);
510+
510511
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
511-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
512-
zend_array_destroy(ht);
513-
if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
514-
if (EG(exception)) {
515-
ZVAL_UNDEF(EX_VAR(opline->result.var));
516-
} else {
517-
ZVAL_NULL(EX_VAR(opline->result.var));
518-
}
519-
}
520-
return;
521-
}
522-
if (EG(exception)) {
523-
if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
524-
ZVAL_UNDEF(EX_VAR(opline->result.var));
525-
}
526-
return;
527-
}
528-
offset_key = ZSTR_EMPTY_ALLOC();
529-
goto str_index;
512+
return;
530513
case IS_DOUBLE:
531514
hval = zend_dval_to_lval(Z_DVAL_P(dim));
532515
if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) {
@@ -667,33 +650,15 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_is_helper(zend_array *ht, zval *dim
667650
}
668651
ZEND_FALLTHROUGH;
669652
case IS_NULL:
670-
/* The array may be destroyed while throwing the notice.
671-
* Temporarily increase the refcount to detect this situation. */
672-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
673-
GC_ADDREF(ht);
653+
retval = zend_hash_find(ht, ZSTR_EMPTY_ALLOC());
654+
if (!retval) {
655+
ZVAL_NULL(result);
656+
} else {
657+
ZVAL_COPY_DEREF(result, retval);
674658
}
675-
execute_data = EG(current_execute_data);
676-
opline = EX(opline);
659+
677660
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
678-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
679-
zend_array_destroy(ht);
680-
if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
681-
if (EG(exception)) {
682-
ZVAL_UNDEF(EX_VAR(opline->result.var));
683-
} else {
684-
ZVAL_NULL(EX_VAR(opline->result.var));
685-
}
686-
}
687-
return;
688-
}
689-
if (EG(exception)) {
690-
if (opline->result_type & (IS_VAR | IS_TMP_VAR)) {
691-
ZVAL_UNDEF(EX_VAR(opline->result.var));
692-
}
693-
return;
694-
}
695-
offset_key = ZSTR_EMPTY_ALLOC();
696-
goto str_index;
661+
return;
697662
case IS_DOUBLE:
698663
hval = zend_dval_to_lval(Z_DVAL_P(dim));
699664
if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) {
@@ -819,22 +784,21 @@ static int ZEND_FASTCALL zend_jit_fetch_dim_isset_helper(zend_array *ht, zval *d
819784
return 0;
820785
}
821786
ZEND_FALLTHROUGH;
822-
case IS_NULL:
823-
/* The array may be destroyed while throwing the notice.
824-
* Temporarily increase the refcount to detect this situation. */
825-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
826-
GC_ADDREF(ht);
787+
case IS_NULL: {
788+
int result;
789+
retval = zend_hash_find(ht, ZSTR_EMPTY_ALLOC());
790+
if (!retval) {
791+
result = 0;
792+
} else {
793+
if (UNEXPECTED(Z_TYPE_P(retval) == IS_REFERENCE)) {
794+
retval = Z_REFVAL_P(retval);
795+
}
796+
result = Z_TYPE_P(retval) > IS_NULL;
827797
}
798+
828799
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
829-
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
830-
zend_array_destroy(ht);
831-
return 0;
832-
}
833-
if (EG(exception)) {
834-
return 0;
835-
}
836-
offset_key = ZSTR_EMPTY_ALLOC();
837-
goto str_index;
800+
return result;
801+
}
838802
case IS_DOUBLE:
839803
hval = zend_dval_to_lval(Z_DVAL_P(dim));
840804
if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) {
@@ -948,6 +912,13 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_rw_helper(zend_array *ht, zval *di
948912
}
949913
execute_data = EG(current_execute_data);
950914
opline = EX(opline);
915+
916+
retval = zend_hash_find(ht, ZSTR_EMPTY_ALLOC());
917+
if (!retval) {
918+
/* Key may be released while throwing the undefined index warning. */
919+
retval = zend_undefined_index_write(ht, ZSTR_EMPTY_ALLOC());
920+
}
921+
951922
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
952923
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
953924
if (!GC_REFCOUNT(ht)) {
@@ -968,8 +939,7 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_rw_helper(zend_array *ht, zval *di
968939
}
969940
return NULL;
970941
}
971-
offset_key = ZSTR_EMPTY_ALLOC();
972-
goto str_index;
942+
return retval;
973943
case IS_DOUBLE:
974944
hval = zend_dval_to_lval(Z_DVAL_P(dim));
975945
if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) {
@@ -1108,6 +1078,9 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim
11081078
}
11091079
execute_data = EG(current_execute_data);
11101080
opline = EX(opline);
1081+
1082+
retval = zend_hash_lookup(ht, ZSTR_EMPTY_ALLOC());
1083+
11111084
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
11121085
if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
11131086
if (!GC_REFCOUNT(ht)) {
@@ -1132,8 +1105,7 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim
11321105
}
11331106
return NULL;
11341107
}
1135-
offset_key = ZSTR_EMPTY_ALLOC();
1136-
goto str_index;
1108+
return retval;
11371109
case IS_DOUBLE:
11381110
hval = zend_dval_to_lval(Z_DVAL_P(dim));
11391111
if (!zend_is_long_compatible(Z_DVAL_P(dim), hval)) {

ext/spl/spl_array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,8 @@ static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zva
280280
try_again:
281281
switch (Z_TYPE_P(offset)) {
282282
case IS_NULL:
283-
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
284283
key->key = ZSTR_EMPTY_ALLOC();
284+
zend_error(E_DEPRECATED, "Using null as an array offset is deprecated, use an empty string instead");
285285
return SUCCESS;
286286
case IS_STRING:
287287
key->key = Z_STR_P(offset);

ext/standard/array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6981,8 +6981,8 @@ PHP_FUNCTION(array_key_exists)
69816981
RETVAL_BOOL(zend_hash_index_exists(ht, Z_LVAL_P(key)));
69826982
break;
69836983
case IS_NULL:
6984-
zend_error(E_DEPRECATED, "Using null as the key parameter for array_key_exists() is deprecated, use an empty string instead");
69856984
RETVAL_BOOL(zend_hash_exists(ht, ZSTR_EMPTY_ALLOC()));
6985+
zend_error(E_DEPRECATED, "Using null as the key parameter for array_key_exists() is deprecated, use an empty string instead");
69866986
break;
69876987
case IS_DOUBLE:
69886988
RETVAL_BOOL(zend_hash_index_exists(ht, zend_dval_to_lval_safe(Z_DVAL_P(key))));

0 commit comments

Comments
 (0)