Clean up unnecessary ternary expressions and simplify some returns

- Simplify conditions
- Use ZEND_HASH_APPLY_* instead of hard-coded booleans
- Use ZEND_NORMALIZE_BOOL
- Drop sign in favor of ZEND_NORMALIZE_BOOL
This commit is contained in:
Gabriel Caruso 2018-08-19 01:32:00 -03:00 committed by Peter Kokot
parent 6c16f9b69c
commit cdd8368d6f
26 changed files with 39 additions and 63 deletions

View file

@ -1576,7 +1576,7 @@ int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem) /* {{{ */
ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */ ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers) /* {{{ */
{ {
zend_bool persistent_hashes = (ce->type == ZEND_INTERNAL_CLASS) ? 1 : 0; zend_bool persistent_hashes = ce->type == ZEND_INTERNAL_CLASS;
ce->refcount = 1; ce->refcount = 1;
ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED; ce->ce_flags = ZEND_ACC_CONSTANTS_UPDATED;

View file

@ -83,9 +83,9 @@ static int clean_module_constant(zval *el, void *arg)
int module_number = *(int *)arg; int module_number = *(int *)arg;
if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) { if (ZEND_CONSTANT_MODULE_NUMBER(c) == module_number) {
return 1; return ZEND_HASH_APPLY_REMOVE;
} else { } else {
return 0; return ZEND_HASH_APPLY_KEEP;
} }
} }

View file

@ -845,10 +845,7 @@ static zend_bool zend_verify_weak_scalar_type_hint(zend_uchar type_hint, zval *a
zend_string *dest; zend_string *dest;
/* on success "arg" is converted to IS_STRING */ /* on success "arg" is converted to IS_STRING */
if (!zend_parse_arg_str_weak(arg, &dest)) { return zend_parse_arg_str_weak(arg, &dest);
return 0;
}
return 1;
} }
default: default:
return 0; return 0;

View file

@ -36,11 +36,8 @@ static int zend_remove_ini_entries(zval *el, void *arg) /* {{{ */
{ {
zend_ini_entry *ini_entry = (zend_ini_entry *)Z_PTR_P(el); zend_ini_entry *ini_entry = (zend_ini_entry *)Z_PTR_P(el);
int module_number = *(int *)arg; int module_number = *(int *)arg;
if (ini_entry->module_number == module_number) {
return 1; return ini_entry->module_number == module_number;
} else {
return 0;
}
} }
/* }}} */ /* }}} */

View file

@ -245,11 +245,8 @@ void zend_destroy_rsrc_list(HashTable *ht)
static int clean_module_resource(zval *zv, void *arg) static int clean_module_resource(zval *zv, void *arg)
{ {
int resource_id = *(int *)arg; int resource_id = *(int *)arg;
if (Z_RES_TYPE_P(zv) == resource_id) {
return 1; return Z_RES_TYPE_P(zv) == resource_id;
} else {
return 0;
}
} }

View file

@ -186,7 +186,7 @@ ZEND_API int zend_stream_fixup(zend_file_handle *file_handle, char **buf, size_t
return FAILURE; return FAILURE;
} }
memset(&file_handle->handle.stream.mmap, 0, sizeof(zend_mmap)); memset(&file_handle->handle.stream.mmap, 0, sizeof(zend_mmap));
file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle)) ? 1 : 0; file_handle->handle.stream.isatty = isatty(fileno((FILE *)file_handle->handle.stream.handle));
file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader; file_handle->handle.stream.reader = (zend_stream_reader_t)zend_stream_stdio_reader;
file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer; file_handle->handle.stream.closer = (zend_stream_closer_t)zend_stream_stdio_closer;
file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer; file_handle->handle.stream.fsizer = (zend_stream_fsizer_t)zend_stream_stdio_fsizer;

View file

@ -665,7 +665,7 @@ static size_t tsrm_realpath_r(char *path, size_t start, size_t len, int *ll, tim
if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) { if(pbuffer->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget; reparsetarget = pbuffer->SymbolicLinkReparseBuffer.ReparseTarget;
isabsolute = (pbuffer->SymbolicLinkReparseBuffer.Flags == 0) ? 1 : 0; isabsolute = pbuffer->SymbolicLinkReparseBuffer.Flags == 0;
#if VIRTUAL_CWD_DEBUG #if VIRTUAL_CWD_DEBUG
printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR)); printname = php_win32_ioutil_w_to_any(reparsetarget + pbuffer->MountPointReparseBuffer.PrintNameOffset / sizeof(WCHAR));
if (!printname) { if (!printname) {

View file

@ -50,7 +50,7 @@ zend_string
int index, signch; int index, signch;
/* Allocate the string memory. */ /* Allocate the string memory. */
signch = ( num->n_sign == PLUS ? 0 : 1 ); /* Number of sign chars. */ signch = num->n_sign != PLUS; /* Number of sign chars. */
if (scale > 0) if (scale > 0)
str = zend_string_alloc(num->n_len + scale + signch + 1, 0); str = zend_string_alloc(num->n_len + scale + signch + 1, 0);
else else

View file

@ -595,11 +595,8 @@ static int _php_curl_multi_setopt(php_curlm *mh, zend_long option, zval *zvalue,
} }
SAVE_CURLM_ERROR(mh, error); SAVE_CURLM_ERROR(mh, error);
if (error != CURLM_OK) {
return 1; return error != CURLM_OK;
} else {
return 0;
}
} }
/* }}} */ /* }}} */

View file

@ -86,11 +86,8 @@ static int _php_curl_share_setopt(php_curlsh *sh, zend_long option, zval *zvalue
} }
SAVE_CURLSH_ERROR(sh, error); SAVE_CURLSH_ERROR(sh, error);
if (error != CURLSHE_OK) {
return 1; return error != CURLSHE_OK;
} else {
return 0;
}
} }
/* }}} */ /* }}} */

View file

@ -51,7 +51,7 @@ $ipv6_test = array(
); );
foreach ($ipv6_test as $ip => $exp) { foreach ($ipv6_test as $ip => $exp) {
$out = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6); $out = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6);
$out = (int) ($out === false ? 0 : 1); $out = $out !== false;
if ($exp != $out) { if ($exp != $out) {
echo "$ip failed (expected ", $exp?"true":"false", ", got ", echo "$ip failed (expected ", $exp?"true":"false", ", got ",
$out?"true":"false", ")\n"; $out?"true":"false", ")\n";

View file

@ -4074,11 +4074,8 @@ int _php_imap_mail(char *to, char *subject, char *message, char *headers, char *
} }
fprintf(sendmail, "\n%s\n", message); fprintf(sendmail, "\n%s\n", message);
ret = pclose(sendmail); ret = pclose(sendmail);
if (ret == -1) {
return 0; return ret != -1;
} else {
return 1;
}
} else { } else {
php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program"); php_error_docref(NULL, E_WARNING, "Could not execute mail delivery program");
return 0; return 0;

View file

@ -869,7 +869,7 @@ int _php_ibase_attach_db(char **args, size_t *len, zend_long *largs, isc_db_hand
buf_len -= dpb_len; buf_len -= dpb_len;
} }
if (largs[SYNC] && buf_len > 0) { if (largs[SYNC] && buf_len > 0) {
dpb_len = slprintf(dpb, buf_len, "%c\1%c", isc_dpb_force_write, largs[SYNC] == isc_spb_prp_wm_sync ? 1 : 0); dpb_len = slprintf(dpb, buf_len, "%c\1%c", isc_dpb_force_write, largs[SYNC] == isc_spb_prp_wm_sync);
dpb += dpb_len; dpb += dpb_len;
buf_len -= dpb_len; buf_len -= dpb_len;
} }

View file

@ -116,7 +116,7 @@ static void internal_parse_to_localtime(IntlDateFormatter_object *dfo, char* tex
/* Is in DST? */ /* Is in DST? */
isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo)); isInDST = ucal_inDaylightTime(parsed_calendar , &INTL_DATA_ERROR_CODE(dfo));
INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." ); INTL_METHOD_CHECK_STATUS( dfo, "Date parsing - localtime failed : while checking if currently in DST." );
add_assoc_long( return_value, CALENDAR_ISDST,(isInDST==1?1:0)); add_assoc_long( return_value, CALENDAR_ISDST,isInDST==1);
} }
/* }}} */ /* }}} */

View file

@ -1578,7 +1578,7 @@ MYSQLND_METHOD(mysqlnd_conn_data, change_user)(MYSQLND_CONN_DATA * const conn,
DBG_ENTER("mysqlnd_conn_data::change_user"); DBG_ENTER("mysqlnd_conn_data::change_user");
DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u", DBG_INF_FMT("conn=%llu user=%s passwd=%s db=%s silent=%u",
conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", (silent == TRUE)?1:0 ); conn->thread_id, user?user:"", passwd?"***":"null", db?db:"", silent == TRUE);
if (PASS != conn->m->local_tx_start(conn, this_func)) { if (PASS != conn->m->local_tx_start(conn, this_func)) {
goto end; goto end;

View file

@ -2004,7 +2004,7 @@ static PHP_METHOD(PDOStatement, setFetchMode)
RETVAL_BOOL( RETVAL_BOOL(
pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU, pdo_stmt_setup_fetch_mode(INTERNAL_FUNCTION_PARAM_PASSTHRU,
stmt, 0) == SUCCESS ? 1 : 0 stmt, 0) == SUCCESS
); );
} }
/* }}} */ /* }}} */

View file

@ -388,7 +388,7 @@ static int pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
PDO_DBG_RETURN(1); PDO_DBG_RETURN(1);
case PDO_ATTR_DEFAULT_STR_PARAM: case PDO_ATTR_DEFAULT_STR_PARAM:
((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL ? 1 : 0; ((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL;
PDO_DBG_RETURN(1); PDO_DBG_RETURN(1);
case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY: case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
@ -617,7 +617,7 @@ static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare); PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
H->assume_national_character_set_strings = pdo_attr_lval(driver_options, H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL ? 1 : 0; PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
#ifndef PDO_USE_MYSQLND #ifndef PDO_USE_MYSQLND
H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size); H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);

View file

@ -553,7 +553,7 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, char **ptr, zend_ulon
break; break;
case PDO_PARAM_BOOL: case PDO_PARAM_BOOL:
S->cols[colno].boolval = **ptr == 't' ? 1: 0; S->cols[colno].boolval = **ptr == 't';
*ptr = (char *) &(S->cols[colno].boolval); *ptr = (char *) &(S->cols[colno].boolval);
*len = sizeof(zend_bool); *len = sizeof(zend_bool);
break; break;

View file

@ -3618,7 +3618,7 @@ static void phar_add_file(phar_archive_data **pphar, char *filename, size_t file
php_stream_statbuf ssb; php_stream_statbuf ssb;
if (filename_len >= sizeof(".phar")-1) { if (filename_len >= sizeof(".phar")-1) {
start_pos = ('/' == filename[0] ? 1 : 0); /* account for any leading slash: multiple-leads handled elsewhere */ start_pos = '/' == filename[0]; /* account for any leading slash: multiple-leads handled elsewhere */
if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) { if (!memcmp(&filename[start_pos], ".phar", sizeof(".phar")-1) && (filename[start_pos+5] == '/' || filename[start_pos+5] == '\\' || filename[start_pos+5] == '\0')) {
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory"); zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Cannot create any files in magic \".phar\" directory");
return; return;

View file

@ -178,7 +178,7 @@ static int spl_ptr_heap_zval_max_cmp(zval *a, zval *b, zval *object) { /* {{{ */
/* exception or call failure */ /* exception or call failure */
return 0; return 0;
} }
return lval > 0 ? 1 : (lval < 0 ? -1 : 0); return ZEND_NORMALIZE_BOOL(lval);
} }
} }
@ -202,7 +202,7 @@ static int spl_ptr_heap_zval_min_cmp(zval *a, zval *b, zval *object) { /* {{{ */
/* exception or call failure */ /* exception or call failure */
return 0; return 0;
} }
return lval > 0 ? 1 : (lval < 0 ? -1 : 0); return ZEND_NORMALIZE_BOOL(lval);
} }
} }
@ -230,7 +230,7 @@ static int spl_ptr_pqueue_elem_cmp(zval *a_zv, zval *b_zv, zval *object) { /* {{
/* exception or call failure */ /* exception or call failure */
return 0; return 0;
} }
return lval > 0 ? 1 : (lval < 0 ? -1 : 0); return ZEND_NORMALIZE_BOOL(lval);
} }
} }

View file

@ -352,7 +352,7 @@ static int spl_object_storage_compare_info(zval *e1, zval *e2) /* {{{ */
return 1; return 1;
} }
return Z_LVAL(result) > 0 ? 1 : (Z_LVAL(result) < 0 ? -1 : 0); return ZEND_NORMALIZE_BOOL(Z_LVAL(result));
} }
/* }}} */ /* }}} */

View file

@ -170,7 +170,7 @@ static int php_array_key_compare(const void *a, const void *b) /* {{{ */
} }
} }
} }
return l1 > l2 ? 1 : (l1 < l2 ? -1 : 0); return ZEND_NORMALIZE_BOOL(l1 > l2);
} }
/* }}} */ /* }}} */
@ -978,7 +978,7 @@ static int php_array_user_compare(const void *a, const void *b) /* {{{ */
zval_ptr_dtor(&retval); zval_ptr_dtor(&retval);
zval_ptr_dtor(&args[1]); zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[0]); zval_ptr_dtor(&args[0]);
return ret < 0 ? -1 : ret > 0 ? 1 : 0; return ZEND_NORMALIZE_BOOL(ret);
} else { } else {
zval_ptr_dtor(&args[1]); zval_ptr_dtor(&args[1]);
zval_ptr_dtor(&args[0]); zval_ptr_dtor(&args[0]);
@ -1101,7 +1101,7 @@ static int php_array_user_key_compare(const void *a, const void *b) /* {{{ */
zval_ptr_dtor(&args[0]); zval_ptr_dtor(&args[0]);
zval_ptr_dtor(&args[1]); zval_ptr_dtor(&args[1]);
return result < 0 ? -1 : result > 0 ? 1 : 0; return ZEND_NORMALIZE_BOOL(result);
} }
/* }}} */ /* }}} */
@ -4583,7 +4583,7 @@ static int zval_user_compare(zval *a, zval *b) /* {{{ */
if (zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache)) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) { if (zend_call_function(&BG(user_compare_fci), &BG(user_compare_fci_cache)) == SUCCESS && Z_TYPE(retval) != IS_UNDEF) {
zend_long ret = zval_get_long(&retval); zend_long ret = zval_get_long(&retval);
zval_ptr_dtor(&retval); zval_ptr_dtor(&retval);
return ret < 0 ? -1 : ret > 0 ? 1 : 0; return ZEND_NORMALIZE_BOOL(ret);
} else { } else {
return 0; return 0;
} }

View file

@ -4340,7 +4340,7 @@ static int parse_opts(char * opts, opt_struct ** result)
(*opts >= 97 && *opts <= 122) /* a - z */ (*opts >= 97 && *opts <= 122) /* a - z */
) { ) {
paras->opt_char = *opts; paras->opt_char = *opts;
paras->need_param = (*(++opts) == ':') ? 1 : 0; paras->need_param = *(++opts) == ':';
paras->opt_name = NULL; paras->opt_name = NULL;
if (paras->need_param == 1) { if (paras->need_param == 1) {
opts++; opts++;

View file

@ -24,8 +24,6 @@
#include "php.h" #include "php.h"
#include "php_versioning.h" #include "php_versioning.h"
#define sign(n) ((n)<0?-1:((n)>0?1:0))
/* {{{ php_canonicalize_version() */ /* {{{ php_canonicalize_version() */
PHPAPI char * PHPAPI char *
@ -115,7 +113,7 @@ compare_special_version_forms(char *form1, char *form2)
break; break;
} }
} }
return sign(found1 - found2); return ZEND_NORMALIZE_BOOL(found1 - found2);
} }
/* }}} */ /* }}} */
@ -160,7 +158,7 @@ php_version_compare(const char *orig_ver1, const char *orig_ver2)
/* compare element numerically */ /* compare element numerically */
l1 = strtol(p1, NULL, 10); l1 = strtol(p1, NULL, 10);
l2 = strtol(p2, NULL, 10); l2 = strtol(p2, NULL, 10);
compare = sign(l1 - l2); compare = ZEND_NORMALIZE_BOOL(l1 - l2);
} else if (!isdigit(*p1) && !isdigit(*p2)) { } else if (!isdigit(*p1) && !isdigit(*p2)) {
/* compare element names */ /* compare element names */
compare = compare_special_version_forms(p1, p2); compare = compare_special_version_forms(p1, p2);

View file

@ -281,11 +281,7 @@ static int fill_buffer(multipart_buffer *self)
/* eof if we are out of bytes, or if we hit the final boundary */ /* eof if we are out of bytes, or if we hit the final boundary */
static int multipart_buffer_eof(multipart_buffer *self) static int multipart_buffer_eof(multipart_buffer *self)
{ {
if ( (self->bytes_in_buffer == 0 && fill_buffer(self) < 1) ) { return self->bytes_in_buffer == 0 && fill_buffer(self) < 1;
return 1;
} else {
return 0;
}
} }
/* create new multipart_buffer structure */ /* create new multipart_buffer structure */

View file

@ -46,7 +46,7 @@ PHPAPI char *_php_stream_mmap_range(php_stream *stream, size_t offset, size_t le
PHPAPI int _php_stream_mmap_unmap(php_stream *stream) PHPAPI int _php_stream_mmap_unmap(php_stream *stream)
{ {
return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK ? 1 : 0; return php_stream_set_option(stream, PHP_STREAM_OPTION_MMAP_API, PHP_STREAM_MMAP_UNMAP, NULL) == PHP_STREAM_OPTION_RETURN_OK;
} }
PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden) PHPAPI int _php_stream_mmap_unmap_ex(php_stream *stream, zend_off_t readden)