Skip to content

Commit c461b60

Browse files
authored
refactor: change zend_is_true to return bool (#14301)
Previously this returned `int`. Many functions actually take advantage of the fact this returns exactly 0 or 1. For instance, `main/streams/xp_socket.c` does: sockopts |= STREAM_SOCKOP_IPV6_V6ONLY_ENABLED * zend_is_true(tmpzval); And `Zend/zend_compile.c` does: child = &ast->child[2 - zend_is_true(zend_ast_get_zval(ast->child[0]))]; I changed a few places trivially from `int` to `bool`, but there are still many places such as the object handlers which return `int` that should eventually be `bool`.
1 parent 2ab114e commit c461b60

20 files changed

+57
-49
lines changed

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
8686
for internal functions. If you need a cache slot for both internal and user
8787
functions, you may obtain a slot for each through the corresponding function.
8888

89+
* zend_is_true now returns bool rather than int. Note that on PHP 8 this has
90+
always returned 0 or 1, so conversion should be trivial.
8991

9092
========================
9193
2. Build system changes

Zend/Optimizer/pass1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
309309
case ZEND_JMPZ:
310310
case ZEND_JMPNZ:
311311
if (opline->op1_type == IS_CONST) {
312-
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
312+
bool should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
313313

314314
if (opline->opcode == ZEND_JMPZ) {
315315
should_jmp = !should_jmp;

Zend/zend_object_handlers.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,11 +1057,12 @@ ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *
10571057
}
10581058
/* }}} */
10591059

1060+
// todo: make zend_std_has_dimension return bool as well
10601061
ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
10611062
{
10621063
zend_class_entry *ce = object->ce;
10631064
zval retval, tmp_offset;
1064-
int result;
1065+
bool result;
10651066

10661067
zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
10671068
if (EXPECTED(funcs)) {
@@ -1081,6 +1082,7 @@ ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check
10811082
zend_bad_array_access(ce);
10821083
return 0;
10831084
}
1085+
10841086
return result;
10851087
}
10861088
/* }}} */
@@ -1810,9 +1812,10 @@ ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
18101812
return ZEND_UNCOMPARABLE;
18111813
}
18121814

1815+
// todo: make zend_std_has_property return bool as well
18131816
ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
18141817
{
1815-
int result;
1818+
bool result;
18161819
zval *value = NULL;
18171820
uintptr_t property_offset;
18181821
const zend_property_info *prop_info = NULL;
@@ -1826,7 +1829,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18261829
}
18271830
if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) {
18281831
/* Skip __isset() for uninitialized typed properties */
1829-
result = 0;
1832+
result = false;
18301833
goto exit;
18311834
}
18321835
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
@@ -1862,17 +1865,17 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18621865
result = (Z_TYPE_P(value) != IS_NULL);
18631866
} else {
18641867
ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
1865-
result = 1;
1868+
result = true;
18661869
}
18671870
goto exit;
18681871
}
18691872
}
18701873
} else if (UNEXPECTED(EG(exception))) {
1871-
result = 0;
1874+
result = false;
18721875
goto exit;
18731876
}
18741877

1875-
result = 0;
1878+
result = false;
18761879
if ((has_set_exists != ZEND_PROPERTY_EXISTS) && zobj->ce->__isset) {
18771880
uint32_t *guard = zend_get_property_guard(zobj, name);
18781881

@@ -1893,7 +1896,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
18931896
result = i_zend_is_true(&rv);
18941897
zval_ptr_dtor(&rv);
18951898
} else {
1896-
result = 0;
1899+
result = false;
18971900
}
18981901
}
18991902
(*guard) &= ~IN_ISSET;

Zend/zend_operators.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2818,9 +2818,9 @@ ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
28182818
}
28192819
/* }}} */
28202820

2821-
ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
2821+
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
28222822
{
2823-
return (int) i_zend_is_true(op);
2823+
return i_zend_is_true(op);
28242824
}
28252825
/* }}} */
28262826

Zend/zend_operators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ static zend_always_inline bool try_convert_to_string(zval *op) {
372372
#define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }
373373

374374

375-
ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op);
375+
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op);
376376
ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op);
377377

378378
#define zval_is_true(op) \

Zend/zend_vm_def.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7313,8 +7313,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
73137313
{
73147314
USE_OPLINE
73157315
zval *value;
7316-
/* Should be bool result? as below got: result = (opline->extended_value & ZEND_ISEMPTY) */
7317-
int result;
7316+
bool result;
73187317
zval *varname;
73197318
zend_string *name, *tmp_name;
73207319
HashTable *target_symbol_table;
@@ -7351,7 +7350,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
73517350
}
73527351
}
73537352

7354-
ZEND_VM_SMART_BRANCH(result, 1);
7353+
ZEND_VM_SMART_BRANCH(result, true);
73557354
}
73567355

73577356
/* No specialization for op_types (CONST|TMPVAR|CV, UNUSED|CLASS_FETCH|CONST|VAR) */

Zend/zend_vm_execute.h

Lines changed: 6 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Zend/zend_weakrefs.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
405405
zend_hash_index_add_new(&wm->ht, obj_key, value);
406406
}
407407

408+
// todo: make zend_weakmap_has_dimension return bool as well
408409
/* int return and check_empty due to Object Handler API */
409410
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
410411
{

ext/dom/php_dom.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,13 @@ zval *dom_write_property(zend_object *object, zend_string *name, zval *value, vo
404404
return zend_std_write_property(object, name, value, cache_slot);
405405
}
406406

407+
// todo: make dom_property_exists return bool as well
407408
/* {{{ dom_property_exists */
408409
static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
409410
{
410411
dom_object *obj = php_dom_obj_from_obj(object);
411412
dom_prop_handler *hnd = NULL;
412-
int retval = 0;
413+
bool retval = false;
413414

414415
if (obj->prop_handler != NULL) {
415416
hnd = zend_hash_find_ptr(obj->prop_handler, name);
@@ -418,7 +419,7 @@ static int dom_property_exists(zend_object *object, zend_string *name, int check
418419
zval tmp;
419420

420421
if (check_empty == 2) {
421-
retval = 1;
422+
retval = true;
422423
} else if (hnd->read_func(obj, &tmp) == SUCCESS) {
423424
if (check_empty == 1) {
424425
retval = zend_is_true(&tmp);
@@ -1441,7 +1442,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
14411442
dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);
14421443

14431444
php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks);
1444-
intern->register_node_ns = 1;
1445+
intern->register_node_ns = true;
14451446

14461447
intern->dom.prop_handler = &dom_xpath_prop_handlers;
14471448

ext/dom/php_dom.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ extern zend_module_entry dom_module_entry;
6666

6767
typedef struct _dom_xpath_object {
6868
php_dom_xpath_callbacks xpath_callbacks;
69-
int register_node_ns;
69+
bool register_node_ns;
7070
dom_object dom;
7171
} dom_xpath_object;
7272

ext/pdo/pdo_stmt.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2344,6 +2344,7 @@ static void row_dim_write(zend_object *object, zval *member, zval *value)
23442344
}
23452345
}
23462346

2347+
// todo: make row_prop_exists return bool as well
23472348
static int row_prop_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
23482349
{
23492350
pdo_row_t *row = (pdo_row_t *)object;
@@ -2363,11 +2364,14 @@ static int row_prop_exists(zend_object *object, zend_string *name, int check_emp
23632364
return false;
23642365
}
23652366
ZEND_ASSERT(retval == &tmp_val);
2366-
int res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
2367+
bool res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
23672368
zval_ptr_dtor_nogc(retval);
2369+
23682370
return res;
23692371
}
23702372

2373+
2374+
// todo: make row_dim_exists return bool as well
23712375
static int row_dim_exists(zend_object *object, zval *offset, int check_empty)
23722376
{
23732377
if (Z_TYPE_P(offset) == IS_LONG) {
@@ -2386,7 +2390,7 @@ static int row_dim_exists(zend_object *object, zval *offset, int check_empty)
23862390
return false;
23872391
}
23882392
ZEND_ASSERT(retval == &tmp_val);
2389-
int res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
2393+
bool res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
23902394
zval_ptr_dtor_nogc(retval);
23912395
return res;
23922396
} else {

ext/spl/spl_directory.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ PHP_METHOD(DirectoryIterator, seek)
851851
}
852852

853853
while (intern->u.dir.index < pos) {
854-
bool valid = 0;
854+
bool valid = false;
855855
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
856856
valid = zend_is_true(&retval);
857857
zval_ptr_dtor(&retval);

ext/spl/spl_iterators.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
263263
zend_class_entry *ce;
264264
zval retval, child;
265265
zend_object_iterator *sub_iter;
266-
int has_children;
267266

268267
SPL_FETCH_SUB_ITERATOR(iterator, object);
269268

@@ -308,7 +307,7 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
308307
}
309308
}
310309
if (Z_TYPE(retval) != IS_UNDEF) {
311-
has_children = zend_is_true(&retval);
310+
bool has_children = zend_is_true(&retval);
312311
zval_ptr_dtor(&retval);
313312
if (has_children) {
314313
if (object->max_depth == -1 || object->max_depth > object->level) {

ext/spl/spl_observer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,7 @@ PHP_METHOD(SplObjectStorage, attach)
440440
spl_object_storage_attach(intern, obj, inf);
441441
} /* }}} */
442442

443+
// todo: make spl_object_storage_has_dimension return bool as well
443444
static int spl_object_storage_has_dimension(zend_object *object, zval *offset, int check_empty)
444445
{
445446
spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);

ext/standard/array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6548,7 +6548,7 @@ PHP_FUNCTION(array_filter)
65486548
fci.params = args;
65496549

65506550
if (zend_call_function(&fci, &fci_cache) == SUCCESS) {
6551-
int retval_true;
6551+
bool retval_true;
65526552

65536553
zval_ptr_dtor(&args[0]);
65546554
if (use_type == ARRAY_FILTER_USE_BOTH) {

ext/standard/filters.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,20 +1113,20 @@ static php_conv_err_t php_conv_get_ulong_prop_ex(const HashTable *ht, zend_ulong
11131113
}
11141114
}
11151115

1116-
static php_conv_err_t php_conv_get_bool_prop_ex(const HashTable *ht, int *pretval, char *field_name, size_t field_name_len)
1116+
static php_conv_err_t php_conv_get_bool_prop_ex(const HashTable *ht, bool *pretval, char *field_name, size_t field_name_len)
11171117
{
11181118
zval *tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1);
11191119
if (tmpval != NULL) {
11201120
*pretval = zend_is_true(tmpval);
11211121
return PHP_CONV_ERR_SUCCESS;
11221122
} else {
1123-
*pretval = 0;
1123+
*pretval = false;
11241124
return PHP_CONV_ERR_NOT_FOUND;
11251125
}
11261126
}
11271127

11281128
/* XXX this might need an additional fix so it uses size_t, whereby unsigned is quite big so leaving as is for now */
1129-
static int php_conv_get_uint_prop_ex(const HashTable *ht, unsigned int *pretval, char *field_name, size_t field_name_len)
1129+
static php_conv_err_t php_conv_get_uint_prop_ex(const HashTable *ht, unsigned int *pretval, char *field_name, size_t field_name_len)
11301130
{
11311131
zend_ulong l;
11321132
php_conv_err_t err;
@@ -1206,8 +1206,8 @@ static php_conv *php_conv_open(int conv_mode, const HashTable *options, int pers
12061206
int opts = 0;
12071207

12081208
if (options != NULL) {
1209-
int opt_binary = 0;
1210-
int opt_force_encode_first = 0;
1209+
bool opt_binary = false;
1210+
bool opt_force_encode_first = false;
12111211

12121212
GET_STR_PROP(options, lbchars, lbchars_len, "line-break-chars", 0);
12131213
GET_UINT_PROP(options, line_len, "line-length");

ext/standard/http_fopen_wrapper.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
136136
zend_string *transport_string;
137137
zend_string *errstr = NULL;
138138
int have_header = 0;
139-
bool request_fulluri = 0, ignore_errors = 0;
139+
bool request_fulluri = false, ignore_errors = false;
140140
struct timeval timeout;
141141
char *user_headers = NULL;
142142
int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
@@ -171,7 +171,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
171171
return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
172172
}
173173
/* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
174-
request_fulluri = 1;
174+
request_fulluri = true;
175175
use_ssl = 0;
176176
use_proxy = 1;
177177
transport_string = zend_string_copy(Z_STR_P(tmpzval));
@@ -801,7 +801,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
801801

802802
/* create filter to decode response body */
803803
if (!(options & STREAM_ONLY_GET_HEADERS)) {
804-
zend_long decode = 1;
804+
bool decode = true;
805805

806806
if (context && (tmpzval = php_stream_context_get_option(context, "http", "auto_decode")) != NULL) {
807807
decode = zend_is_true(tmpzval);

ext/xsl/xsltprocessor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
197197
zval *id, *docp = NULL;
198198
xmlDoc *doc = NULL, *newdoc = NULL;
199199
xsltStylesheetPtr sheetp;
200-
int clone_docu = 0;
200+
bool clone_docu = false;
201201
xmlNode *nodep = NULL;
202202
zval *cloneDocu, rv;
203203
zend_string *member;
@@ -257,7 +257,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
257257
cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
258258
clone_docu = zend_is_true(cloneDocu);
259259
zend_string_release_ex(member, 0);
260-
if (clone_docu == 0) {
260+
if (!clone_docu) {
261261
/* Check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation.
262262
* xsl:key elements may only occur at the top level. Furthermore, all elements at the top level must be in a
263263
* namespace (if not, then the stylesheet is not well-formed and this function will have returned false earlier). */

0 commit comments

Comments
 (0)