Skip to content

Use RETURN_THROWS() macro when an exception is thrown #5036

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

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions Zend/zend_API.h
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,7 @@ END_EXTERN_C()
#define RETURN_ZVAL(zv, copy, dtor) do { RETVAL_ZVAL(zv, copy, dtor); return; } while (0)
#define RETURN_FALSE do { RETVAL_FALSE; return; } while (0)
#define RETURN_TRUE do { RETVAL_TRUE; return; } while (0)
#define RETURN_THROWS() do { ZEND_ASSERT(EG(exception)); (void) return_value; return; } while (0)

#define HASH_OF(p) (Z_TYPE_P(p)==IS_ARRAY ? Z_ARRVAL_P(p) : ((Z_TYPE_P(p)==IS_OBJECT ? Z_OBJ_HT_P(p)->get_properties(Z_OBJ_P(p)) : NULL)))
#define ZVAL_IS_NULL(z) (Z_TYPE_P(z) == IS_NULL)
Expand Down
44 changes: 22 additions & 22 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ PHP_FUNCTION(min)
ZVAL_COPY_DEREF(return_value, result);
} else {
zend_value_error("Array must contain at least one element");
return;
RETURN_THROWS();
}
}
} else {
Expand Down Expand Up @@ -1311,7 +1311,7 @@ PHP_FUNCTION(max)
ZVAL_COPY_DEREF(return_value, result);
} else {
zend_value_error("Array must contain at least one element");
return;
RETURN_THROWS();
}
}
} else {
Expand Down Expand Up @@ -2452,18 +2452,18 @@ PHP_FUNCTION(extract)

if (extract_type < EXTR_OVERWRITE || extract_type > EXTR_IF_EXISTS) {
zend_value_error("Invalid extract type");
return;
RETURN_THROWS();
}

if (extract_type > EXTR_SKIP && extract_type <= EXTR_PREFIX_IF_EXISTS && ZEND_NUM_ARGS() < 3) {
zend_value_error("Specified extract type requires the prefix parameter");
return;
RETURN_THROWS();
}

if (prefix) {
if (ZSTR_LEN(prefix) && !php_valid_var_name(ZSTR_VAL(prefix), ZSTR_LEN(prefix))) {
zend_value_error("Prefix is not a valid identifier");
return;
RETURN_THROWS();
}
}

Expand Down Expand Up @@ -2620,10 +2620,10 @@ PHP_FUNCTION(array_fill)
if (EXPECTED(num > 0)) {
if (sizeof(num) > 4 && UNEXPECTED(EXPECTED(num > 0x7fffffff))) {
zend_value_error("Too many elements");
return;
RETURN_THROWS();
} else if (UNEXPECTED(start_key > ZEND_LONG_MAX - num + 1)) {
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
return;
RETURN_THROWS();
} else if (EXPECTED(start_key >= 0) && EXPECTED(start_key < num)) {
/* create packed array */
Bucket *p;
Expand Down Expand Up @@ -2669,7 +2669,7 @@ PHP_FUNCTION(array_fill)
RETURN_EMPTY_ARRAY();
} else {
zend_value_error("Number of elements can't be negative");
return;
RETURN_THROWS();
}
}
/* }}} */
Expand Down Expand Up @@ -2708,7 +2708,7 @@ PHP_FUNCTION(array_fill_keys)
if (__calc_size >= (double)HT_MAX_SIZE) { \
zend_value_error(\
"The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \
return; \
RETURN_THROWS(); \
} \
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
array_init_size(return_value, size); \
Expand All @@ -2720,7 +2720,7 @@ PHP_FUNCTION(array_fill_keys)
if (__calc_size >= HT_MAX_SIZE - 1) { \
zend_value_error(\
"The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \
return; \
RETURN_THROWS(); \
} \
size = (uint32_t)(__calc_size + 1); \
array_init_size(return_value, size); \
Expand Down Expand Up @@ -2817,7 +2817,7 @@ PHP_FUNCTION(range)

if (zend_isinf(high) || zend_isinf(low)) {
zend_value_error("Invalid range supplied: start=%0.0f end=%0.0f", low, high);
return;
RETURN_THROWS();
}

if (low > high) { /* Negative steps */
Expand Down Expand Up @@ -2910,7 +2910,7 @@ PHP_FUNCTION(range)
err:
if (err) {
zend_value_error("Step exceeds the specified range");
return;
RETURN_THROWS();
}
}
/* }}} */
Expand Down Expand Up @@ -4390,7 +4390,7 @@ PHP_FUNCTION(array_pad)
pad_size_abs = ZEND_ABS(pad_size);
if (pad_size_abs < 0 || pad_size_abs - input_size > Z_L(1048576)) {
zend_value_error("You may only pad up to 1048576 elements at a time");
return;
RETURN_THROWS();
}

if (input_size >= pad_size_abs) {
Expand Down Expand Up @@ -4715,7 +4715,7 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
return;
RETURN_THROWS();
}

for (i = 0; i < argc; i++) {
Expand Down Expand Up @@ -4809,7 +4809,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache) == FAILURE) {
return;
RETURN_THROWS();
}
fci_data = &fci1;
fci_data_cache = &fci1_cache;
Expand Down Expand Up @@ -4861,7 +4861,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache, &fci2, &fci2_cache) == FAILURE) {
return;
RETURN_THROWS();
}

} else {
Expand Down Expand Up @@ -5107,7 +5107,7 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+f", &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
return;
RETURN_THROWS();
}
diff_data_compare_func = zval_user_compare;
} else {
Expand All @@ -5116,7 +5116,7 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
return;
}
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
return;
RETURN_THROWS();
}
if (data_compare_type == DIFF_COMP_DATA_INTERNAL) {
diff_data_compare_func = zval_compare;
Expand Down Expand Up @@ -5214,7 +5214,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache) == FAILURE) {
return;
RETURN_THROWS();
}
fci_data = &fci1;
fci_data_cache = &fci1_cache;
Expand Down Expand Up @@ -5266,7 +5266,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
}

if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache, &fci2, &fci2_cache) == FAILURE) {
return;
RETURN_THROWS();
}

} else {
Expand Down Expand Up @@ -5854,7 +5854,7 @@ PHP_FUNCTION(array_rand)

if (num_avail == 0) {
zend_value_error("Array is empty");
return;
RETURN_THROWS();
}

if (num_req == 1) {
Expand Down Expand Up @@ -5895,7 +5895,7 @@ PHP_FUNCTION(array_rand)

if (num_req <= 0 || num_req > num_avail) {
zend_value_error("Second argument has to be between 1 and the number of elements in the array");
return;
RETURN_THROWS();
}

/* Make the return value an array only if we need to pass back more than one result. */
Expand Down
12 changes: 6 additions & 6 deletions ext/standard/basic_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1561,7 +1561,7 @@ PHP_FUNCTION(putenv)

if (setting_len == 0 || setting[0] == '=') {
zend_value_error("Invalid parameter syntax");
return;
RETURN_THROWS();
}

pe.putenv_string = estrndup(setting, setting_len);
Expand Down Expand Up @@ -1944,7 +1944,7 @@ PHP_FUNCTION(sleep)

if (num < 0) {
zend_value_error("Number of seconds must be greater than or equal to 0");
return;
RETURN_THROWS();
}

RETURN_LONG(php_sleep((unsigned int)num));
Expand All @@ -1964,7 +1964,7 @@ PHP_FUNCTION(usleep)

if (num < 0) {
zend_value_error("Number of microseconds must be greater than or equal to 0");
return;
RETURN_THROWS();
}
if (usleep((unsigned int)num) < 0) {
#if ZEND_DEBUG
Expand All @@ -1991,11 +1991,11 @@ PHP_FUNCTION(time_nanosleep)

if (tv_sec < 0) {
zend_value_error("The seconds value must be greater than 0");
return;
RETURN_THROWS();
}
if (tv_nsec < 0) {
zend_value_error("The nanoseconds value must be greater than 0");
return;
RETURN_THROWS();
}

php_req.tv_sec = (time_t) tv_sec;
Expand All @@ -2009,7 +2009,7 @@ PHP_FUNCTION(time_nanosleep)
return;
} else if (errno == EINVAL) {
zend_value_error("Nanoseconds was not in the range 0 to 999 999 999 or seconds was negative");
return;
RETURN_THROWS();
}

RETURN_FALSE;
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ PHP_FUNCTION(scandir)

if (dirn_len < 1) {
zend_value_error("Directory name cannot be empty");
return;
RETURN_THROWS();
}

if (zcontext) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ PHP_FUNCTION(dns_check_record)

if (hostname_len == 0) {
zend_value_error("Host cannot be empty");
return;
RETURN_THROWS();
}

if (rectype) {
Expand Down
8 changes: 4 additions & 4 deletions ext/standard/dns_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ PHP_FUNCTION(dns_get_mx) /* {{{ */
PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */

if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
return;
RETURN_THROWS();
}

status = DnsQuery_A(hostname, DNS_TYPE_MX, DNS_QUERY_STANDARD, NULL, &pResult, NULL);
Expand Down Expand Up @@ -103,12 +103,12 @@ PHP_FUNCTION(dns_check_record)
PDNS_RECORD pResult; /* Pointer to DNS_RECORD structure */

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
return;
RETURN_THROWS();
}

if (hostname_len == 0) {
zend_value_error("Host cannot be empty");
return;
RETURN_THROWS();
}

if (rectype) {
Expand Down Expand Up @@ -357,7 +357,7 @@ PHP_FUNCTION(dns_get_record)

if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lz!z!b",
&hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
return;
RETURN_THROWS();
}

if (authns) {
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ PHP_FUNCTION(file_get_contents)

if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
zend_value_error("Length must be greater than or equal to zero");
return;
RETURN_THROWS();
}

context = php_stream_context_from_zval(zcontext, 0);
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/levenshtein.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,21 @@ PHP_FUNCTION(levenshtein)
switch (argc) {
case 2: /* just two strings: use maximum performance version */
if (zend_parse_parameters(2, "ss", &str1, &str1_len, &str2, &str2_len) == FAILURE) {
return;
RETURN_THROWS();
}
distance = reference_levdist(str1, str1_len, str2, str2_len, 1, 1, 1);
break;

case 5: /* more general version: calc cost by ins/rep/del weights */
if (zend_parse_parameters(5, "sslll", &str1, &str1_len, &str2, &str2_len, &cost_ins, &cost_rep, &cost_del) == FAILURE) {
return;
RETURN_THROWS();
}
distance = reference_levdist(str1, str1_len, str2, str2_len, cost_ins, cost_rep, cost_del);
break;

case 3: /* most general version: calc cost by user-supplied function */
if (zend_parse_parameters(3, "sss", &str1, &str1_len, &str2, &str2_len, &callback_name, &callback_len) == FAILURE) {
return;
RETURN_THROWS();
}
distance = custom_levdist(str1, str2, callback_name);
break;
Expand Down
6 changes: 3 additions & 3 deletions ext/standard/math.c
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ PHP_FUNCTION(log)

if (base <= 0.0) {
zend_value_error("Base must be greater than 0");
return;
RETURN_THROWS();
}

RETURN_DOUBLE(log(num) / log(base));
Expand Down Expand Up @@ -1008,11 +1008,11 @@ PHP_FUNCTION(base_convert)

if (frombase < 2 || frombase > 36) {
zend_value_error("Invalid `from base' (" ZEND_LONG_FMT ")", frombase);
return;
RETURN_THROWS();
}
if (tobase < 2 || tobase > 36) {
zend_value_error("Invalid `to base' (" ZEND_LONG_FMT ")", tobase);
return;
RETURN_THROWS();
}

_php_math_basetozval(Z_STR_P(number), (int)frombase, &temp);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/mt_rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ PHP_FUNCTION(mt_rand)

if (UNEXPECTED(max < min)) {
zend_value_error("max (" ZEND_LONG_FMT ") is smaller than min (" ZEND_LONG_FMT ")", max, min);
return;
RETURN_THROWS();
}

RETURN_LONG(php_mt_rand_common(min, max));
Expand Down
4 changes: 2 additions & 2 deletions ext/standard/password.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,15 @@ PHP_FUNCTION(password_hash)
zend_string *algostr = zval_get_string(zalgo);
zend_value_error("Unknown password hashing algorithm: %s", ZSTR_VAL(algostr));
zend_string_release(algostr);
return;
RETURN_THROWS();
}

digest = algo->hash(password, options);
if (!digest) {
if (!EG(exception)) {
zend_throw_error(NULL, "Password hashing failed for unknown reason");
}
return;
RETURN_THROWS();
}

RETURN_NEW_STR(digest);
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/proc_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ PHP_FUNCTION(proc_open)
uint32_t num_elems = zend_hash_num_elements(Z_ARRVAL_P(command_zv));
if (num_elems == 0) {
zend_value_error("Command array must have at least one element");
return;
RETURN_THROWS();
}

#ifdef PHP_WIN32
Expand Down
Loading