Skip to content

refactor: change zend_is_true to return bool #14301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ PHP 8.4 INTERNALS UPGRADE NOTES
for internal functions. If you need a cache slot for both internal and user
functions, you may obtain a slot for each through the corresponding function.

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

========================
2. Build system changes
Expand Down
2 changes: 1 addition & 1 deletion Zend/Optimizer/pass1.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ void zend_optimizer_pass1(zend_op_array *op_array, zend_optimizer_ctx *ctx)
case ZEND_JMPZ:
case ZEND_JMPNZ:
if (opline->op1_type == IS_CONST) {
int should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));
bool should_jmp = zend_is_true(&ZEND_OP1_LITERAL(opline));

if (opline->opcode == ZEND_JMPZ) {
should_jmp = !should_jmp;
Expand Down
17 changes: 10 additions & 7 deletions Zend/zend_object_handlers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,12 @@ ZEND_API void zend_std_write_dimension(zend_object *object, zval *offset, zval *
}
/* }}} */

// todo: make zend_std_has_dimension return bool as well
ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check_empty) /* {{{ */
{
zend_class_entry *ce = object->ce;
zval retval, tmp_offset;
int result;
bool result;

zend_class_arrayaccess_funcs *funcs = ce->arrayaccess_funcs_ptr;
if (EXPECTED(funcs)) {
Expand All @@ -1081,6 +1082,7 @@ ZEND_API int zend_std_has_dimension(zend_object *object, zval *offset, int check
zend_bad_array_access(ce);
return 0;
}

return result;
}
/* }}} */
Expand Down Expand Up @@ -1810,9 +1812,10 @@ ZEND_API int zend_objects_not_comparable(zval *o1, zval *o2)
return ZEND_UNCOMPARABLE;
}

// todo: make zend_std_has_property return bool as well
ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has_set_exists, void **cache_slot) /* {{{ */
{
int result;
bool result;
zval *value = NULL;
uintptr_t property_offset;
const zend_property_info *prop_info = NULL;
Expand All @@ -1826,7 +1829,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
}
if (UNEXPECTED(Z_PROP_FLAG_P(value) & IS_PROP_UNINIT)) {
/* Skip __isset() for uninitialized typed properties */
result = 0;
result = false;
goto exit;
}
} else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
Expand Down Expand Up @@ -1862,17 +1865,17 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
result = (Z_TYPE_P(value) != IS_NULL);
} else {
ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
result = 1;
result = true;
}
goto exit;
}
}
} else if (UNEXPECTED(EG(exception))) {
result = 0;
result = false;
goto exit;
}

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

Expand All @@ -1893,7 +1896,7 @@ ZEND_API int zend_std_has_property(zend_object *zobj, zend_string *name, int has
result = i_zend_is_true(&rv);
zval_ptr_dtor(&rv);
} else {
result = 0;
result = false;
}
}
(*guard) &= ~IN_ISSET;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_operators.c
Original file line number Diff line number Diff line change
Expand Up @@ -2818,9 +2818,9 @@ ZEND_API zend_result ZEND_FASTCALL decrement_function(zval *op1) /* {{{ */
}
/* }}} */

ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op) /* {{{ */
{
return (int) i_zend_is_true(op);
return i_zend_is_true(op);
}
/* }}} */

Expand Down
2 changes: 1 addition & 1 deletion Zend/zend_operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ static zend_always_inline bool try_convert_to_string(zval *op) {
#define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op)); }


ZEND_API int ZEND_FASTCALL zend_is_true(const zval *op);
ZEND_API bool ZEND_FASTCALL zend_is_true(const zval *op);
ZEND_API bool ZEND_FASTCALL zend_object_is_true(const zval *op);

#define zval_is_true(op) \
Expand Down
5 changes: 2 additions & 3 deletions Zend/zend_vm_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -7313,8 +7313,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
{
USE_OPLINE
zval *value;
/* Should be bool result? as below got: result = (opline->extended_value & ZEND_ISEMPTY) */
int result;
bool result;
zval *varname;
zend_string *name, *tmp_name;
HashTable *target_symbol_table;
Expand Down Expand Up @@ -7351,7 +7350,7 @@ ZEND_VM_HANDLER(114, ZEND_ISSET_ISEMPTY_VAR, CONST|TMPVAR|CV, UNUSED, VAR_FETCH|
}
}

ZEND_VM_SMART_BRANCH(result, 1);
ZEND_VM_SMART_BRANCH(result, true);
}

/* No specialization for op_types (CONST|TMPVAR|CV, UNUSED|CLASS_FETCH|CONST|VAR) */
Expand Down
15 changes: 6 additions & 9 deletions Zend/zend_vm_execute.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Zend/zend_weakrefs.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ static void zend_weakmap_write_dimension(zend_object *object, zval *offset, zval
zend_hash_index_add_new(&wm->ht, obj_key, value);
}

// todo: make zend_weakmap_has_dimension return bool as well
/* int return and check_empty due to Object Handler API */
static int zend_weakmap_has_dimension(zend_object *object, zval *offset, int check_empty)
{
Expand Down
7 changes: 4 additions & 3 deletions ext/dom/php_dom.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,13 @@ zval *dom_write_property(zend_object *object, zend_string *name, zval *value, vo
return zend_std_write_property(object, name, value, cache_slot);
}

// todo: make dom_property_exists return bool as well
/* {{{ dom_property_exists */
static int dom_property_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
{
dom_object *obj = php_dom_obj_from_obj(object);
dom_prop_handler *hnd = NULL;
int retval = 0;
bool retval = false;

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

if (check_empty == 2) {
retval = 1;
retval = true;
} else if (hnd->read_func(obj, &tmp) == SUCCESS) {
if (check_empty == 1) {
retval = zend_is_true(&tmp);
Expand Down Expand Up @@ -1441,7 +1442,7 @@ zend_object *dom_xpath_objects_new(zend_class_entry *class_type)
dom_xpath_object *intern = zend_object_alloc(sizeof(dom_xpath_object), class_type);

php_dom_xpath_callbacks_ctor(&intern->xpath_callbacks);
intern->register_node_ns = 1;
intern->register_node_ns = true;

intern->dom.prop_handler = &dom_xpath_prop_handlers;

Expand Down
2 changes: 1 addition & 1 deletion ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ extern zend_module_entry dom_module_entry;

typedef struct _dom_xpath_object {
php_dom_xpath_callbacks xpath_callbacks;
int register_node_ns;
bool register_node_ns;
dom_object dom;
} dom_xpath_object;

Expand Down
8 changes: 6 additions & 2 deletions ext/pdo/pdo_stmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -2344,6 +2344,7 @@ static void row_dim_write(zend_object *object, zval *member, zval *value)
}
}

// todo: make row_prop_exists return bool as well
static int row_prop_exists(zend_object *object, zend_string *name, int check_empty, void **cache_slot)
{
pdo_row_t *row = (pdo_row_t *)object;
Expand All @@ -2363,11 +2364,14 @@ static int row_prop_exists(zend_object *object, zend_string *name, int check_emp
return false;
}
ZEND_ASSERT(retval == &tmp_val);
int res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
bool res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
zval_ptr_dtor_nogc(retval);

return res;
}


// todo: make row_dim_exists return bool as well
static int row_dim_exists(zend_object *object, zval *offset, int check_empty)
{
if (Z_TYPE_P(offset) == IS_LONG) {
Expand All @@ -2386,7 +2390,7 @@ static int row_dim_exists(zend_object *object, zval *offset, int check_empty)
return false;
}
ZEND_ASSERT(retval == &tmp_val);
int res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
bool res = check_empty ? i_zend_is_true(retval) : Z_TYPE(tmp_val) != IS_NULL;
zval_ptr_dtor_nogc(retval);
return res;
} else {
Expand Down
2 changes: 1 addition & 1 deletion ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ PHP_METHOD(DirectoryIterator, seek)
}

while (intern->u.dir.index < pos) {
bool valid = 0;
bool valid = false;
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
valid = zend_is_true(&retval);
zval_ptr_dtor(&retval);
Expand Down
3 changes: 1 addition & 2 deletions ext/spl/spl_iterators.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
zend_class_entry *ce;
zval retval, child;
zend_object_iterator *sub_iter;
int has_children;

SPL_FETCH_SUB_ITERATOR(iterator, object);

Expand Down Expand Up @@ -308,7 +307,7 @@ static void spl_recursive_it_move_forward_ex(spl_recursive_it_object *object, zv
}
}
if (Z_TYPE(retval) != IS_UNDEF) {
has_children = zend_is_true(&retval);
bool has_children = zend_is_true(&retval);
zval_ptr_dtor(&retval);
if (has_children) {
if (object->max_depth == -1 || object->max_depth > object->level) {
Expand Down
1 change: 1 addition & 0 deletions ext/spl/spl_observer.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ PHP_METHOD(SplObjectStorage, attach)
spl_object_storage_attach(intern, obj, inf);
} /* }}} */

// todo: make spl_object_storage_has_dimension return bool as well
static int spl_object_storage_has_dimension(zend_object *object, zval *offset, int check_empty)
{
spl_SplObjectStorage *intern = spl_object_storage_from_obj(object);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -6548,7 +6548,7 @@ PHP_FUNCTION(array_filter)
fci.params = args;

if (zend_call_function(&fci, &fci_cache) == SUCCESS) {
int retval_true;
bool retval_true;

zval_ptr_dtor(&args[0]);
if (use_type == ARRAY_FILTER_USE_BOTH) {
Expand Down
10 changes: 5 additions & 5 deletions ext/standard/filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,20 +1113,20 @@ static php_conv_err_t php_conv_get_ulong_prop_ex(const HashTable *ht, zend_ulong
}
}

static php_conv_err_t php_conv_get_bool_prop_ex(const HashTable *ht, int *pretval, char *field_name, size_t field_name_len)
static php_conv_err_t php_conv_get_bool_prop_ex(const HashTable *ht, bool *pretval, char *field_name, size_t field_name_len)
{
zval *tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1);
if (tmpval != NULL) {
*pretval = zend_is_true(tmpval);
return PHP_CONV_ERR_SUCCESS;
} else {
*pretval = 0;
*pretval = false;
return PHP_CONV_ERR_NOT_FOUND;
}
}

/* XXX this might need an additional fix so it uses size_t, whereby unsigned is quite big so leaving as is for now */
static int php_conv_get_uint_prop_ex(const HashTable *ht, unsigned int *pretval, char *field_name, size_t field_name_len)
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)
{
zend_ulong l;
php_conv_err_t err;
Expand Down Expand Up @@ -1206,8 +1206,8 @@ static php_conv *php_conv_open(int conv_mode, const HashTable *options, int pers
int opts = 0;

if (options != NULL) {
int opt_binary = 0;
int opt_force_encode_first = 0;
bool opt_binary = false;
bool opt_force_encode_first = false;

GET_STR_PROP(options, lbchars, lbchars_len, "line-break-chars", 0);
GET_UINT_PROP(options, line_len, "line-length");
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/http_fopen_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
zend_string *transport_string;
zend_string *errstr = NULL;
int have_header = 0;
bool request_fulluri = 0, ignore_errors = 0;
bool request_fulluri = false, ignore_errors = false;
struct timeval timeout;
char *user_headers = NULL;
int header_init = ((flags & HTTP_WRAPPER_HEADER_INIT) != 0);
Expand Down Expand Up @@ -171,7 +171,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,
return php_stream_open_wrapper_ex(path, mode, REPORT_ERRORS, NULL, context);
}
/* Called from a non-http wrapper with http proxying requested (i.e. ftp) */
request_fulluri = 1;
request_fulluri = true;
use_ssl = 0;
use_proxy = 1;
transport_string = zend_string_copy(Z_STR_P(tmpzval));
Expand Down Expand Up @@ -801,7 +801,7 @@ static php_stream *php_stream_url_wrap_http_ex(php_stream_wrapper *wrapper,

/* create filter to decode response body */
if (!(options & STREAM_ONLY_GET_HEADERS)) {
zend_long decode = 1;
bool decode = true;

if (context && (tmpzval = php_stream_context_get_option(context, "http", "auto_decode")) != NULL) {
decode = zend_is_true(tmpzval);
Expand Down
4 changes: 2 additions & 2 deletions ext/xsl/xsltprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
zval *id, *docp = NULL;
xmlDoc *doc = NULL, *newdoc = NULL;
xsltStylesheetPtr sheetp;
int clone_docu = 0;
bool clone_docu = false;
xmlNode *nodep = NULL;
zval *cloneDocu, rv;
zend_string *member;
Expand Down Expand Up @@ -257,7 +257,7 @@ PHP_METHOD(XSLTProcessor, importStylesheet)
cloneDocu = zend_std_read_property(Z_OBJ_P(id), member, BP_VAR_R, NULL, &rv);
clone_docu = zend_is_true(cloneDocu);
zend_string_release_ex(member, 0);
if (clone_docu == 0) {
if (!clone_docu) {
/* Check if the stylesheet is using xsl:key, if yes, we have to clone the document _always_ before a transformation.
* xsl:key elements may only occur at the top level. Furthermore, all elements at the top level must be in a
* namespace (if not, then the stylesheet is not well-formed and this function will have returned false earlier). */
Expand Down
Loading
Loading