]> granicus.if.org Git - php/commitdiff
Clean up some type conversions
authorNikita Popov <nikic@php.net>
Sat, 25 Apr 2015 18:43:11 +0000 (20:43 +0200)
committerNikita Popov <nikic@php.net>
Mon, 27 Apr 2015 16:50:08 +0000 (18:50 +0200)
While at it also fix some type checks in iconv and drop dead and
unported code in standard/filters.

ext/bz2/bz2_filter.c
ext/iconv/iconv.c
ext/soap/php_encoding.c
ext/standard/array.c
ext/standard/filters.c
ext/standard/password.c
ext/wddx/wddx.c
ext/zip/php_zip.c
ext/zlib/zlib.c
ext/zlib/zlib_filter.c
main/php_ini.c

index c2710d99f93c6cf2bf6b750d413c0ba0e49b489e..c0e128a9e15a2abfe58f69c1a9809fb394fb19ba 100644 (file)
@@ -377,28 +377,21 @@ static php_stream_filter *php_bz2_filter_create(const char *filtername, zval *fi
                        if (Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) {
                                if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "blocks", sizeof("blocks")-1))) {
                                        /* How much memory to allocate (1 - 9) x 100kb */
-                                       zval tmp;
-
-                                       ZVAL_DUP(&tmp, tmpzval);
-                                       convert_to_long(&tmp);
-                                       if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > 9) {
-                                               php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate. (%pd)", Z_LVAL_P(tmpzval));
+                                       zend_long blocks = zval_get_long(tmpzval);
+                                       if (blocks < 1 || blocks > 9) {
+                                               php_error_docref(NULL, E_WARNING, "Invalid parameter given for number of blocks to allocate. (%pd)", blocks);
                                        } else {
-                                               blockSize100k = (int)Z_LVAL(tmp);
+                                               blockSize100k = (int) blocks;
                                        }
                                }
 
                                if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "work", sizeof("work")-1))) {
                                        /* Work Factor (0 - 250) */
-                                       zval tmp;
-
-                                       ZVAL_DUP(&tmp, tmpzval);
-                                       convert_to_long(&tmp);
-
-                                       if (Z_LVAL(tmp) < 0 || Z_LVAL(tmp) > 250) {
-                                               php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor. (%pd)", Z_LVAL(tmp));
+                                       zend_long work = zval_get_long(tmpzval);
+                                       if (work < 0 || work > 250) {
+                                               php_error_docref(NULL, E_WARNING, "Invalid parameter given for work factor. (%pd)", work);
                                        } else {
-                                               workFactor = (int)Z_LVAL(tmp);
+                                               workFactor = (int) work;
                                        }
                                }
                        }
index 638160cb690f35a703666a0cc151cab30a6f4b23..35e4d943d5b7633909a2c33c4a00e46575cdc914 100644 (file)
@@ -2214,39 +2214,31 @@ PHP_FUNCTION(iconv_mime_encode)
                        }
                }
 
-               if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "input-charset", sizeof("input-charset") - 1)) != NULL) {
+               if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "input-charset", sizeof("input-charset") - 1)) != NULL && Z_TYPE_P(pzval) == IS_STRING) {
                        if (Z_STRLEN_P(pzval) >= ICONV_CSNMAXLEN) {
                                php_error_docref(NULL, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
                                RETURN_FALSE;
                        }
 
-                       if (Z_TYPE_P(pzval) == IS_STRING && Z_STRLEN_P(pzval) > 0) {
+                       if (Z_STRLEN_P(pzval) > 0) {
                                in_charset = Z_STRVAL_P(pzval);
                        }
                }
 
 
-               if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "output-charset", sizeof("output-charset") - 1)) != NULL) {
+               if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "output-charset", sizeof("output-charset") - 1)) != NULL && Z_TYPE_P(pzval) == IS_STRING) {
                        if (Z_STRLEN_P(pzval) >= ICONV_CSNMAXLEN) {
                                php_error_docref(NULL, E_WARNING, "Charset parameter exceeds the maximum allowed length of %d characters", ICONV_CSNMAXLEN);
                                RETURN_FALSE;
                        }
 
-                       if (Z_TYPE_P(pzval) == IS_STRING && Z_STRLEN_P(pzval) > 0) {
+                       if (Z_STRLEN_P(pzval) > 0) {
                                out_charset = Z_STRVAL_P(pzval);
                        }
                }
 
                if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "line-length", sizeof("line-length") - 1)) != NULL) {
-                       zval val;
-
-                       if (Z_TYPE_P(pzval) != IS_LONG) {
-                               ZVAL_DUP(&val, pzval);
-                               convert_to_long(&val);
-                               pzval = &val;
-                       }
-
-                       line_len = Z_LVAL_P(pzval);
+                       line_len = zval_get_long(pzval);
                }
 
                if ((pzval = zend_hash_str_find(Z_ARRVAL_P(pref), "line-break-chars", sizeof("line-break-chars") - 1)) != NULL) {
index 993a2fa4fc615b1a0a0aa153da1a35edf0cc7bb8..9c25067f427bcdfa1305832196d5b1e7c3f02be2 100644 (file)
@@ -1065,15 +1065,9 @@ static xmlNodePtr to_xml_long(encodeTypePtr type, zval *data, int style, xmlNode
                snprintf(s, sizeof(s), "%0.0F",floor(Z_DVAL_P(data)));
                xmlNodeSetContent(ret, BAD_CAST(s));
        } else {
-               zval tmp;
-
-               ZVAL_DUP(&tmp, data);
-               if (Z_TYPE(tmp) != IS_LONG) {
-                       convert_to_long(&tmp);
-               }
-               convert_to_string(&tmp);
-               xmlNodeSetContentLen(ret, BAD_CAST(Z_STRVAL(tmp)), Z_STRLEN(tmp));
-               zval_dtor(&tmp);
+               zend_string *str = zend_long_to_str(zval_get_long(data));
+               xmlNodeSetContentLen(ret, BAD_CAST(str->val), str->len);
+               zend_string_release(str);
        }
 
        if (style == SOAP_ENCODED) {
index 1e354266a13a9ed68b05bfeae927c11e89d14274..a20a588b1ea99b4ee0f4567434b16910306818d8 100644 (file)
@@ -1470,12 +1470,9 @@ PHP_FUNCTION(extract)
                if (var_name) {
                        var_exists = zend_hash_exists_ind(symbol_table, var_name);
                } else if (extract_type == EXTR_PREFIX_ALL || extract_type == EXTR_PREFIX_INVALID) {
-                       zval num;
-
-                       ZVAL_LONG(&num, num_key);
-                       convert_to_string(&num);
-                       php_prefix_varname(&final_name, prefix, Z_STRVAL(num), Z_STRLEN(num), 1);
-                       zval_dtor(&num);
+                       zend_string *str = zend_long_to_str(num_key);
+                       php_prefix_varname(&final_name, prefix, str->val, str->len, 1);
+                       zend_string_release(str);
                } else {
                        continue;
                }
@@ -3364,14 +3361,9 @@ PHP_FUNCTION(array_unique)
 }
 /* }}} */
 
-static int zval_compare(zval *a, zval *b) /* {{{ */
+static int zval_compare(zval *first, zval *second) /* {{{ */
 {
        zval result;
-       zval *first;
-       zval *second;
-
-       first = a;
-       second = b;
 
        if (Z_TYPE_P(first) == IS_INDIRECT) {
                first = Z_INDIRECT_P(first);
index 3babd23d57edc35f8190d3be35c79e61c7a0cdef..de3a3b2026b0284db61fa20f81f8d7e32eda17d5 100644 (file)
@@ -1232,92 +1232,35 @@ static php_conv_err_t php_conv_get_string_prop_ex(const HashTable *ht, char **pr
        return PHP_CONV_ERR_SUCCESS;
 }
 
-#if IT_WAS_USED
-static php_conv_err_t php_conv_get_long_prop_ex(const HashTable *ht, zend_long *pretval, char *field_name, size_t field_name_len)
-{
-       zval **tmpval;
-
-       *pretval = 0;
-
-       if (zend_hash_find((HashTable *)ht, field_name, field_name_len, (void **)&tmpval) == SUCCESS) {
-               zval tmp, *ztval = *tmpval;
-
-               if (Z_TYPE_PP(tmpval) != IS_LONG) {
-                       tmp = *ztval;
-                       zval_copy_ctor(&tmp);
-                       convert_to_long(&tmp);
-                       ztval = &tmp;
-               }
-               *pretval = Z_LVAL_P(ztval);
-       } else {
-               return PHP_CONV_ERR_NOT_FOUND;
-       }
-       return PHP_CONV_ERR_SUCCESS;
-}
-#endif
-
 static php_conv_err_t php_conv_get_ulong_prop_ex(const HashTable *ht, zend_ulong *pretval, char *field_name, size_t field_name_len)
 {
-       zval *tmpval;
-
-       *pretval = 0;
+       zval *tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1);
+       if (tmpval != NULL) {
+               zend_long lval = zval_get_long(tmpval);
 
-       if ((tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1)) != NULL) {
-               zval tmp;
-
-               if (Z_TYPE_P(tmpval) != IS_LONG) {
-                       ZVAL_DUP(&tmp, tmpval);
-                       convert_to_long(&tmp);
-                       tmpval = &tmp;
-               }
-               if (Z_LVAL_P(tmpval) < 0) {
+               if (lval < 0) {
                        *pretval = 0;
                } else {
-                       *pretval = Z_LVAL_P(tmpval);
+                       *pretval = lval;
                }
+               return PHP_CONV_ERR_SUCCESS;
        } else {
+               *pretval = 0;
                return PHP_CONV_ERR_NOT_FOUND;
        }
-       return PHP_CONV_ERR_SUCCESS;
 }
 
 static php_conv_err_t php_conv_get_bool_prop_ex(const HashTable *ht, int *pretval, char *field_name, size_t field_name_len)
 {
-       zval *tmpval;
-
-       *pretval = 0;
-
-       if ((tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1)) != NULL) {
-               zval tmp;
-
-               if (Z_TYPE_P(tmpval) != IS_FALSE || Z_TYPE_P(tmpval) != IS_TRUE) {
-                       ZVAL_DUP(&tmp, tmpval);
-                       zval_copy_ctor(&tmp);
-                       convert_to_boolean(&tmp);
-                       tmpval = &tmp;
-               }
-               *pretval = (Z_TYPE_P(tmpval) == IS_TRUE);
+       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;
                return PHP_CONV_ERR_NOT_FOUND;
        }
-       return PHP_CONV_ERR_SUCCESS;
-}
-
-
-#if IT_WAS_USED
-static int php_conv_get_int_prop_ex(const HashTable *ht, int *pretval, char *field_name, size_t field_name_len)
-{
-       zend_long l;
-       php_conv_err_t err;
-
-       *pretval = 0;
-
-       if ((err = php_conv_get_long_prop_ex(ht, &l, field_name, field_name_len)) == PHP_CONV_ERR_SUCCESS) {
-               *pretval = l;
-       }
-       return err;
 }
-#endif
 
 /* 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)
index 656dc50cbbd74e05b5938d4af9e721c6c3b95dfe..209e2533f8f8c310eeb46ed915d9262280fb41f0 100644 (file)
@@ -231,16 +231,8 @@ PHP_FUNCTION(password_needs_rehash)
                        {
                                zend_long new_cost = PHP_PASSWORD_BCRYPT_COST, cost = 0;
 
-                               if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
-                                       if (Z_TYPE_P(option_buffer) != IS_LONG) {
-                                               zval cast_option_buffer;
-                                               ZVAL_DUP(&cast_option_buffer, option_buffer);
-                                               convert_to_long(&cast_option_buffer);
-                                               new_cost = Z_LVAL(cast_option_buffer);
-                                               zval_dtor(&cast_option_buffer);
-                                       } else {
-                                               new_cost = Z_LVAL_P(option_buffer);
-                                       }
+                               if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
+                                       new_cost = zval_get_long(option_buffer);
                                }
 
                                sscanf(hash, "$2y$" ZEND_LONG_FMT "$", &cost);
@@ -314,16 +306,8 @@ PHP_FUNCTION(password_hash)
                {
                        zend_long cost = PHP_PASSWORD_BCRYPT_COST;
 
-                       if (options && (option_buffer = zend_symtable_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
-                               if (Z_TYPE_P(option_buffer) != IS_LONG) {
-                                       zval cast_option_buffer;
-                                       ZVAL_DUP(&cast_option_buffer, option_buffer);
-                                       convert_to_long(&cast_option_buffer);
-                                       cost = Z_LVAL(cast_option_buffer);
-                                       zval_dtor(&cast_option_buffer);
-                               } else {
-                                       cost = Z_LVAL_P(option_buffer);
-                               }
+                       if (options && (option_buffer = zend_hash_str_find(options, "cost", sizeof("cost")-1)) != NULL) {
+                               cost = zval_get_long(option_buffer);
                        }
 
                        if (cost < 4 || cost > 31) {
@@ -343,7 +327,7 @@ PHP_FUNCTION(password_hash)
                        RETURN_NULL();
        }
 
-       if (options && (option_buffer = zend_symtable_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
+       if (options && (option_buffer = zend_hash_str_find(options, "salt", sizeof("salt")-1)) != NULL) {
                char *buffer;
                size_t buffer_len = 0;
 
index c0dc964e573b0feb01dc76d58ee4d4b0b016175d..19e4369aef6af3771e27ab8f547503c0eeafbf05 100644 (file)
@@ -407,12 +407,9 @@ static void php_wddx_serialize_string(wddx_packet *packet, zval *var)
 static void php_wddx_serialize_number(wddx_packet *packet, zval *var)
 {
        char tmp_buf[WDDX_BUF_LEN];
-       zval tmp;
-
-       ZVAL_DUP(&tmp, var);
-       convert_to_string(&tmp);
-       snprintf(tmp_buf, sizeof(tmp_buf), WDDX_NUMBER, Z_STRVAL(tmp));
-       zval_ptr_dtor(&tmp);
+       zend_string *str = zval_get_string(var);
+       snprintf(tmp_buf, sizeof(tmp_buf), WDDX_NUMBER, str->val);
+       zend_string_release(str);
 
        php_wddx_add_chunk(packet, tmp_buf);
 }
index 34b3733d7e440161874bd54270994ee0f265ab4c..991ab9022027f93c5a5272383d76d69f78742373 100644 (file)
@@ -315,16 +315,7 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path, char
 {
        zval *option;
        if ((option = zend_hash_str_find(HASH_OF(options), "remove_all_path", sizeof("remove_all_path") - 1)) != NULL) {
-               zend_long opt;
-               if (Z_TYPE_P(option) != IS_LONG) {
-                       zval tmp;
-                       ZVAL_DUP(&tmp, option);
-                       convert_to_long(&tmp);
-                       opt = Z_LVAL(tmp);
-               } else {
-                       opt = Z_LVAL_P(option);
-               }
-               *remove_all_path = opt;
+               *remove_all_path = zval_get_long(option);
        }
 
        /* If I add more options, it would make sense to create a nice static struct and loop over it. */
index 7fcb6a1e517d36e74b010ee11fddddd5d4b3c7b9..715fdaff1c401fa2d8177a266403c04facece715 100644 (file)
@@ -899,7 +899,7 @@ PHP_FUNCTION(deflate_init)
                RETURN_FALSE;
        }
 
-       if (options && (option_buffer = zend_symtable_str_find(options, "memory", sizeof("memory")-1)) != NULL) {
+       if (options && (option_buffer = zend_hash_str_find(options, "memory", sizeof("memory")-1)) != NULL) {
                memory = zval_get_long(option_buffer);
        }
        if (memory < 1 || memory > 9) {
index 1ac90a4d47462a56300a411cc0d4c48a711b5e8a..c0705f8b314929bd87bfab81b39efa32d62af8f1 100644 (file)
@@ -326,15 +326,12 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f
 
                        if ((Z_TYPE_P(filterparams) == IS_ARRAY || Z_TYPE_P(filterparams) == IS_OBJECT) &&
                                (tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
-                               zval tmp;
-
                                /* log-2 base of history window (9 - 15) */
-                               ZVAL_DUP(&tmp, tmpzval);
-                               convert_to_long(&tmp);
-                               if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 32) {
-                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for window size. (%pd)", Z_LVAL(tmp));
+                               zend_long tmp = zval_get_long(tmpzval);
+                               if (tmp < -MAX_WBITS || tmp > MAX_WBITS + 32) {
+                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for window size. (%pd)", tmp);
                                } else {
-                                       windowBits = Z_LVAL(tmp);
+                                       windowBits = tmp;
                                }
                        }
                }
@@ -351,7 +348,8 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f
 
 
                if (filterparams) {
-                       zval *tmpzval, tmp;
+                       zval *tmpzval;
+                       zend_long tmp;
 
                        /* filterparams can either be a scalar value to indicate compression level (shortcut method)
                Or can be a hash containing one or more of 'window', 'memory', and/or 'level' members. */
@@ -360,31 +358,27 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f
                                case IS_ARRAY:
                                case IS_OBJECT:
                                        if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "memory", sizeof("memory") -1))) {
-                                               ZVAL_DUP(&tmp, tmpzval);
-                                               convert_to_long(&tmp);
-
                                                /* Memory Level (1 - 9) */
-                                               if (Z_LVAL(tmp) < 1 || Z_LVAL(tmp) > MAX_MEM_LEVEL) {
-                                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for memory level. (%pd)", Z_LVAL(tmp));
+                                               tmp = zval_get_long(tmpzval);
+                                               if (tmp < 1 || tmp > MAX_MEM_LEVEL) {
+                                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for memory level. (%pd)", tmp);
                                                } else {
-                                                       memLevel = Z_LVAL(tmp);
+                                                       memLevel = tmp;
                                                }
                                        }
 
                                        if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "window", sizeof("window") - 1))) {
-                                               ZVAL_DUP(&tmp, tmpzval);
-                                               convert_to_long(&tmp);
-
                                                /* log-2 base of history window (9 - 15) */
-                                               if (Z_LVAL(tmp) < -MAX_WBITS || Z_LVAL(tmp) > MAX_WBITS + 16) {
-                                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for window size. (%pd)", Z_LVAL(tmp));
+                                               tmp = zval_get_long(tmpzval);
+                                               if (tmp < -MAX_WBITS || tmp > MAX_WBITS + 16) {
+                                                       php_error_docref(NULL, E_WARNING, "Invalid parameter give for window size. (%pd)", tmp);
                                                } else {
-                                                       windowBits = Z_LVAL(tmp);
+                                                       windowBits = tmp;
                                                }
                                        }
 
                                        if ((tmpzval = zend_hash_str_find(HASH_OF(filterparams), "level", sizeof("level") - 1))) {
-                                               ZVAL_COPY_VALUE(&tmp, tmpzval);
+                                               tmp = zval_get_long(tmpzval);
 
                                                /* Pseudo pass through to catch level validating code */
                                                goto factory_setlevel;
@@ -393,16 +387,13 @@ static php_stream_filter *php_zlib_filter_create(const char *filtername, zval *f
                                case IS_STRING:
                                case IS_DOUBLE:
                                case IS_LONG:
-                                       ZVAL_COPY_VALUE(&tmp, filterparams);
+                                       tmp = zval_get_long(filterparams);
 factory_setlevel:
-                                       zval_copy_ctor(&tmp);
-                                       convert_to_long(&tmp);
-
                                        /* Set compression level within reason (-1 == default, 0 == none, 1-9 == least to most compression */
-                                       if (Z_LVAL(tmp) < -1 || Z_LVAL(tmp) > 9) {
-                                               php_error_docref(NULL, E_WARNING, "Invalid compression level specified. (%pd)", Z_LVAL(tmp));
+                                       if (tmp < -1 || tmp > 9) {
+                                               php_error_docref(NULL, E_WARNING, "Invalid compression level specified. (%pd)", tmp);
                                        } else {
-                                               level = Z_LVAL(tmp);
+                                               level = tmp;
                                        }
                                        break;
                                default:
index 7914244147a988468bd848998931e840c1889aa4..80d34848f841175333ad7b68f0a222a670863aa4 100644 (file)
@@ -886,15 +886,13 @@ PHPAPI zval *cfg_get_entry(const char *name, size_t name_length)
  */
 PHPAPI int cfg_get_long(const char *varname, zend_long *result)
 {
-       zval *tmp, var;
+       zval *tmp;
 
        if ((tmp = zend_hash_str_find(&configuration_hash, varname, strlen(varname))) == NULL) {
                *result = 0;
                return FAILURE;
        }
-       ZVAL_DUP(&var, tmp);
-       convert_to_long(&var);
-       *result = Z_LVAL(var);
+       *result = zval_get_long(tmp);
        return SUCCESS;
 }
 /* }}} */
@@ -903,15 +901,13 @@ PHPAPI int cfg_get_long(const char *varname, zend_long *result)
  */
 PHPAPI int cfg_get_double(const char *varname, double *result)
 {
-       zval *tmp, var;
+       zval *tmp;
 
        if ((tmp = zend_hash_str_find(&configuration_hash, varname, strlen(varname))) == NULL) {
                *result = (double) 0;
                return FAILURE;
        }
-       ZVAL_DUP(&var, tmp);
-       convert_to_double(&var);
-       *result = Z_DVAL(var);
+       *result = zval_get_double(tmp);
        return SUCCESS;
 }
 /* }}} */