Skip to content

Commit 06a2804

Browse files
committed
Use RETURN_THROWS() when an exception is thrown
Closes phpGH-5036
1 parent e34a1f9 commit 06a2804

File tree

16 files changed

+81
-80
lines changed

16 files changed

+81
-80
lines changed

Zend/zend_API.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,7 @@ END_EXTERN_C()
674674
#define RETURN_ZVAL(zv, copy, dtor) do { RETVAL_ZVAL(zv, copy, dtor); return; } while (0)
675675
#define RETURN_FALSE do { RETVAL_FALSE; return; } while (0)
676676
#define RETURN_TRUE do { RETVAL_TRUE; return; } while (0)
677+
#define RETURN_THROWS() do { ZEND_ASSERT(EG(exception)); (void) return_value; return; } while (0)
677678

678679
#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)))
679680
#define ZVAL_IS_NULL(z) (Z_TYPE_P(z) == IS_NULL)

ext/standard/array.c

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,7 +1264,7 @@ PHP_FUNCTION(min)
12641264
ZVAL_COPY_DEREF(return_value, result);
12651265
} else {
12661266
zend_value_error("Array must contain at least one element");
1267-
return;
1267+
RETURN_THROWS();
12681268
}
12691269
}
12701270
} else {
@@ -1311,7 +1311,7 @@ PHP_FUNCTION(max)
13111311
ZVAL_COPY_DEREF(return_value, result);
13121312
} else {
13131313
zend_value_error("Array must contain at least one element");
1314-
return;
1314+
RETURN_THROWS();
13151315
}
13161316
}
13171317
} else {
@@ -2452,18 +2452,18 @@ PHP_FUNCTION(extract)
24522452

24532453
if (extract_type < EXTR_OVERWRITE || extract_type > EXTR_IF_EXISTS) {
24542454
zend_value_error("Invalid extract type");
2455-
return;
2455+
RETURN_THROWS();
24562456
}
24572457

24582458
if (extract_type > EXTR_SKIP && extract_type <= EXTR_PREFIX_IF_EXISTS && ZEND_NUM_ARGS() < 3) {
24592459
zend_value_error("Specified extract type requires the prefix parameter");
2460-
return;
2460+
RETURN_THROWS();
24612461
}
24622462

24632463
if (prefix) {
24642464
if (ZSTR_LEN(prefix) && !php_valid_var_name(ZSTR_VAL(prefix), ZSTR_LEN(prefix))) {
24652465
zend_value_error("Prefix is not a valid identifier");
2466-
return;
2466+
RETURN_THROWS();
24672467
}
24682468
}
24692469

@@ -2620,10 +2620,10 @@ PHP_FUNCTION(array_fill)
26202620
if (EXPECTED(num > 0)) {
26212621
if (sizeof(num) > 4 && UNEXPECTED(EXPECTED(num > 0x7fffffff))) {
26222622
zend_value_error("Too many elements");
2623-
return;
2623+
RETURN_THROWS();
26242624
} else if (UNEXPECTED(start_key > ZEND_LONG_MAX - num + 1)) {
26252625
zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
2626-
return;
2626+
RETURN_THROWS();
26272627
} else if (EXPECTED(start_key >= 0) && EXPECTED(start_key < num)) {
26282628
/* create packed array */
26292629
Bucket *p;
@@ -2669,7 +2669,7 @@ PHP_FUNCTION(array_fill)
26692669
RETURN_EMPTY_ARRAY();
26702670
} else {
26712671
zend_value_error("Number of elements can't be negative");
2672-
return;
2672+
RETURN_THROWS();
26732673
}
26742674
}
26752675
/* }}} */
@@ -2708,7 +2708,7 @@ PHP_FUNCTION(array_fill_keys)
27082708
if (__calc_size >= (double)HT_MAX_SIZE) { \
27092709
zend_value_error(\
27102710
"The supplied range exceeds the maximum array size: start=%0.0f end=%0.0f", end, start); \
2711-
return; \
2711+
RETURN_THROWS(); \
27122712
} \
27132713
size = (uint32_t)_php_math_round(__calc_size, 0, PHP_ROUND_HALF_UP); \
27142714
array_init_size(return_value, size); \
@@ -2720,7 +2720,7 @@ PHP_FUNCTION(array_fill_keys)
27202720
if (__calc_size >= HT_MAX_SIZE - 1) { \
27212721
zend_value_error(\
27222722
"The supplied range exceeds the maximum array size: start=" ZEND_LONG_FMT " end=" ZEND_LONG_FMT, end, start); \
2723-
return; \
2723+
RETURN_THROWS(); \
27242724
} \
27252725
size = (uint32_t)(__calc_size + 1); \
27262726
array_init_size(return_value, size); \
@@ -2817,7 +2817,7 @@ PHP_FUNCTION(range)
28172817

28182818
if (zend_isinf(high) || zend_isinf(low)) {
28192819
zend_value_error("Invalid range supplied: start=%0.0f end=%0.0f", low, high);
2820-
return;
2820+
RETURN_THROWS();
28212821
}
28222822

28232823
if (low > high) { /* Negative steps */
@@ -2910,7 +2910,7 @@ PHP_FUNCTION(range)
29102910
err:
29112911
if (err) {
29122912
zend_value_error("Step exceeds the specified range");
2913-
return;
2913+
RETURN_THROWS();
29142914
}
29152915
}
29162916
/* }}} */
@@ -4390,7 +4390,7 @@ PHP_FUNCTION(array_pad)
43904390
pad_size_abs = ZEND_ABS(pad_size);
43914391
if (pad_size_abs < 0 || pad_size_abs - input_size > Z_L(1048576)) {
43924392
zend_value_error("You may only pad up to 1048576 elements at a time");
4393-
return;
4393+
RETURN_THROWS();
43944394
}
43954395

43964396
if (input_size >= pad_size_abs) {
@@ -4715,7 +4715,7 @@ static void php_array_intersect_key(INTERNAL_FUNCTION_PARAMETERS, int data_compa
47154715
}
47164716

47174717
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
4718-
return;
4718+
RETURN_THROWS();
47194719
}
47204720

47214721
for (i = 0; i < argc; i++) {
@@ -4809,7 +4809,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
48094809
}
48104810

48114811
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache) == FAILURE) {
4812-
return;
4812+
RETURN_THROWS();
48134813
}
48144814
fci_data = &fci1;
48154815
fci_data_cache = &fci1_cache;
@@ -4861,7 +4861,7 @@ static void php_array_intersect(INTERNAL_FUNCTION_PARAMETERS, int behavior, int
48614861
}
48624862

48634863
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache, &fci2, &fci2_cache) == FAILURE) {
4864-
return;
4864+
RETURN_THROWS();
48654865
}
48664866

48674867
} else {
@@ -5107,7 +5107,7 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
51075107
return;
51085108
}
51095109
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+f", &args, &argc, &BG(user_compare_fci), &BG(user_compare_fci_cache)) == FAILURE) {
5110-
return;
5110+
RETURN_THROWS();
51115111
}
51125112
diff_data_compare_func = zval_user_compare;
51135113
} else {
@@ -5116,7 +5116,7 @@ static void php_array_diff_key(INTERNAL_FUNCTION_PARAMETERS, int data_compare_ty
51165116
return;
51175117
}
51185118
if (zend_parse_parameters(ZEND_NUM_ARGS(), "+", &args, &argc) == FAILURE) {
5119-
return;
5119+
RETURN_THROWS();
51205120
}
51215121
if (data_compare_type == DIFF_COMP_DATA_INTERNAL) {
51225122
diff_data_compare_func = zval_compare;
@@ -5214,7 +5214,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
52145214
}
52155215

52165216
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache) == FAILURE) {
5217-
return;
5217+
RETURN_THROWS();
52185218
}
52195219
fci_data = &fci1;
52205220
fci_data_cache = &fci1_cache;
@@ -5266,7 +5266,7 @@ static void php_array_diff(INTERNAL_FUNCTION_PARAMETERS, int behavior, int data_
52665266
}
52675267

52685268
if (zend_parse_parameters(ZEND_NUM_ARGS(), param_spec, &args, &arr_argc, &fci1, &fci1_cache, &fci2, &fci2_cache) == FAILURE) {
5269-
return;
5269+
RETURN_THROWS();
52705270
}
52715271

52725272
} else {
@@ -5854,7 +5854,7 @@ PHP_FUNCTION(array_rand)
58545854

58555855
if (num_avail == 0) {
58565856
zend_value_error("Array is empty");
5857-
return;
5857+
RETURN_THROWS();
58585858
}
58595859

58605860
if (num_req == 1) {
@@ -5895,7 +5895,7 @@ PHP_FUNCTION(array_rand)
58955895

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

59015901
/* Make the return value an array only if we need to pass back more than one result. */

ext/standard/basic_functions.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ PHP_FUNCTION(putenv)
15611561

15621562
if (setting_len == 0 || setting[0] == '=') {
15631563
zend_value_error("Invalid parameter syntax");
1564-
return;
1564+
RETURN_THROWS();
15651565
}
15661566

15671567
pe.putenv_string = estrndup(setting, setting_len);
@@ -1942,7 +1942,7 @@ PHP_FUNCTION(sleep)
19421942

19431943
if (num < 0) {
19441944
zend_value_error("Number of seconds must be greater than or equal to 0");
1945-
return;
1945+
RETURN_THROWS();
19461946
}
19471947

19481948
RETURN_LONG(php_sleep((unsigned int)num));
@@ -1962,7 +1962,7 @@ PHP_FUNCTION(usleep)
19621962

19631963
if (num < 0) {
19641964
zend_value_error("Number of microseconds must be greater than or equal to 0");
1965-
return;
1965+
RETURN_THROWS();
19661966
}
19671967
if (usleep((unsigned int)num) < 0) {
19681968
#if ZEND_DEBUG
@@ -1989,11 +1989,11 @@ PHP_FUNCTION(time_nanosleep)
19891989

19901990
if (tv_sec < 0) {
19911991
zend_value_error("The seconds value must be greater than 0");
1992-
return;
1992+
RETURN_THROWS();
19931993
}
19941994
if (tv_nsec < 0) {
19951995
zend_value_error("The nanoseconds value must be greater than 0");
1996-
return;
1996+
RETURN_THROWS();
19971997
}
19981998

19991999
php_req.tv_sec = (time_t) tv_sec;
@@ -2007,7 +2007,7 @@ PHP_FUNCTION(time_nanosleep)
20072007
return;
20082008
} else if (errno == EINVAL) {
20092009
zend_value_error("Nanoseconds was not in the range 0 to 999 999 999 or seconds was negative");
2010-
return;
2010+
RETURN_THROWS();
20112011
}
20122012

20132013
RETURN_FALSE;

ext/standard/dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,7 @@ PHP_FUNCTION(scandir)
558558

559559
if (dirn_len < 1) {
560560
zend_value_error("Directory name cannot be empty");
561-
return;
561+
RETURN_THROWS();
562562
}
563563

564564
if (zcontext) {

ext/standard/dns.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ PHP_FUNCTION(dns_check_record)
382382

383383
if (hostname_len == 0) {
384384
zend_value_error("Host cannot be empty");
385-
return;
385+
RETURN_THROWS();
386386
}
387387

388388
if (rectype) {

ext/standard/dns_win32.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ PHP_FUNCTION(dns_get_mx) /* {{{ */
4949
PDNS_RECORD pResult, pRec; /* Pointer to DNS_RECORD structure */
5050

5151
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sz|z", &hostname, &hostname_len, &mx_list, &weight_list) == FAILURE) {
52-
return;
52+
RETURN_THROWS();
5353
}
5454

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

105105
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s", &hostname, &hostname_len, &rectype, &rectype_len) == FAILURE) {
106-
return;
106+
RETURN_THROWS();
107107
}
108108

109109
if (hostname_len == 0) {
110110
zend_value_error("Host cannot be empty");
111-
return;
111+
RETURN_THROWS();
112112
}
113113

114114
if (rectype) {
@@ -357,7 +357,7 @@ PHP_FUNCTION(dns_get_record)
357357

358358
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lz!z!b",
359359
&hostname, &hostname_len, &type_param, &authns, &addtl, &raw) == FAILURE) {
360-
return;
360+
RETURN_THROWS();
361361
}
362362

363363
if (authns) {

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ PHP_FUNCTION(file_get_contents)
544544

545545
if (ZEND_NUM_ARGS() == 5 && maxlen < 0) {
546546
zend_value_error("Length must be greater than or equal to zero");
547-
return;
547+
RETURN_THROWS();
548548
}
549549

550550
context = php_stream_context_from_zval(zcontext, 0);

ext/standard/levenshtein.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@ PHP_FUNCTION(levenshtein)
9999
switch (argc) {
100100
case 2: /* just two strings: use maximum performance version */
101101
if (zend_parse_parameters(2, "ss", &str1, &str1_len, &str2, &str2_len) == FAILURE) {
102-
return;
102+
RETURN_THROWS();
103103
}
104104
distance = reference_levdist(str1, str1_len, str2, str2_len, 1, 1, 1);
105105
break;
106106

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

114114
case 3: /* most general version: calc cost by user-supplied function */
115115
if (zend_parse_parameters(3, "sss", &str1, &str1_len, &str2, &str2_len, &callback_name, &callback_len) == FAILURE) {
116-
return;
116+
RETURN_THROWS();
117117
}
118118
distance = custom_levdist(str1, str2, callback_name);
119119
break;

ext/standard/math.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ PHP_FUNCTION(log)
624624

625625
if (base <= 0.0) {
626626
zend_value_error("Base must be greater than 0");
627-
return;
627+
RETURN_THROWS();
628628
}
629629

630630
RETURN_DOUBLE(log(num) / log(base));
@@ -1006,11 +1006,11 @@ PHP_FUNCTION(base_convert)
10061006

10071007
if (frombase < 2 || frombase > 36) {
10081008
zend_value_error("Invalid `from base' (" ZEND_LONG_FMT ")", frombase);
1009-
return;
1009+
RETURN_THROWS();
10101010
}
10111011
if (tobase < 2 || tobase > 36) {
10121012
zend_value_error("Invalid `to base' (" ZEND_LONG_FMT ")", tobase);
1013-
return;
1013+
RETURN_THROWS();
10141014
}
10151015

10161016
_php_math_basetozval(Z_STR_P(number), (int)frombase, &temp);

ext/standard/mt_rand.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ PHP_FUNCTION(mt_rand)
326326

327327
if (UNEXPECTED(max < min)) {
328328
zend_value_error("max (" ZEND_LONG_FMT ") is smaller than min (" ZEND_LONG_FMT ")", max, min);
329-
return;
329+
RETURN_THROWS();
330330
}
331331

332332
RETURN_LONG(php_mt_rand_common(min, max));

ext/standard/password.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,15 @@ PHP_FUNCTION(password_hash)
671671
zend_string *algostr = zval_get_string(zalgo);
672672
zend_value_error("Unknown password hashing algorithm: %s", ZSTR_VAL(algostr));
673673
zend_string_release(algostr);
674-
return;
674+
RETURN_THROWS();
675675
}
676676

677677
digest = algo->hash(password, options);
678678
if (!digest) {
679679
if (!EG(exception)) {
680680
zend_throw_error(NULL, "Password hashing failed for unknown reason");
681681
}
682-
return;
682+
RETURN_THROWS();
683683
}
684684

685685
RETURN_NEW_STR(digest);

ext/standard/proc_open.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ PHP_FUNCTION(proc_open)
529529
uint32_t num_elems = zend_hash_num_elements(Z_ARRVAL_P(command_zv));
530530
if (num_elems == 0) {
531531
zend_value_error("Command array must have at least one element");
532-
return;
532+
RETURN_THROWS();
533533
}
534534

535535
#ifdef PHP_WIN32

0 commit comments

Comments
 (0)