Clean up some type conversions

While at it also fix some type checks in iconv and drop dead and
unported code in standard/filters.
This commit is contained in:
Nikita Popov 2015-04-25 20:43:11 +02:00
parent 1800bed104
commit 40e465e357
11 changed files with 65 additions and 192 deletions

View 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;
}
}
}

View 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) {

View 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) {

View 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);

View File

@ -1232,93 +1232,36 @@ 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;
zval *tmpval = zend_hash_str_find((HashTable *)ht, field_name, field_name_len-1);
if (tmpval != NULL) {
zend_long lval = zval_get_long(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_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);
}
} else {
return PHP_CONV_ERR_NOT_FOUND;
*pretval = lval;
}
return PHP_CONV_ERR_SUCCESS;
} else {
*pretval = 0;
return PHP_CONV_ERR_NOT_FOUND;
}
}
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)
{

View 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;

View 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);
}

View 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. */

View 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) {

View 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:

View 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;
}
/* }}} */