Skip to content

Commit 6b7f5f5

Browse files
committed
Convert UNKNOWN default values to null in ext/bcmath
1 parent d63eca2 commit 6b7f5f5

File tree

3 files changed

+105
-88
lines changed

3 files changed

+105
-88
lines changed

ext/bcmath/bcmath.c

Lines changed: 88 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -156,22 +156,24 @@ static void php_str2num(bc_num *num, char *str)
156156
PHP_FUNCTION(bcadd)
157157
{
158158
zend_string *left, *right;
159-
zend_long scale_param = 0;
159+
zend_long scale_param;
160+
zend_bool scale_param_is_null = 1;
160161
bc_num first, second, result;
161-
int scale = BCG(bc_precision);
162+
int scale;
162163

163164
ZEND_PARSE_PARAMETERS_START(2, 3)
164165
Z_PARAM_STR(left)
165166
Z_PARAM_STR(right)
166167
Z_PARAM_OPTIONAL
167-
Z_PARAM_LONG(scale_param)
168+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
168169
ZEND_PARSE_PARAMETERS_END();
169170

170-
if (ZEND_NUM_ARGS() == 3) {
171-
if (scale_param < 0 || scale_param > INT_MAX) {
172-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
173-
RETURN_THROWS();
174-
}
171+
if (scale_param_is_null) {
172+
scale = BCG(bc_precision);
173+
} else if (scale_param < 0 || scale_param > INT_MAX) {
174+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
175+
RETURN_THROWS();
176+
} else {
175177
scale = (int) scale_param;
176178
}
177179

@@ -195,22 +197,24 @@ PHP_FUNCTION(bcadd)
195197
PHP_FUNCTION(bcsub)
196198
{
197199
zend_string *left, *right;
198-
zend_long scale_param = 0;
200+
zend_long scale_param;
201+
zend_bool scale_param_is_null = 1;
199202
bc_num first, second, result;
200-
int scale = BCG(bc_precision);
203+
int scale;
201204

202205
ZEND_PARSE_PARAMETERS_START(2, 3)
203206
Z_PARAM_STR(left)
204207
Z_PARAM_STR(right)
205208
Z_PARAM_OPTIONAL
206-
Z_PARAM_LONG(scale_param)
209+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
207210
ZEND_PARSE_PARAMETERS_END();
208211

209-
if (ZEND_NUM_ARGS() == 3) {
210-
if (scale_param < 0 || scale_param > INT_MAX) {
211-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
212-
RETURN_THROWS();
213-
}
212+
if (scale_param_is_null) {
213+
scale = BCG(bc_precision);
214+
} else if (scale_param < 0 || scale_param > INT_MAX) {
215+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
216+
RETURN_THROWS();
217+
} else {
214218
scale = (int) scale_param;
215219
}
216220

@@ -234,22 +238,24 @@ PHP_FUNCTION(bcsub)
234238
PHP_FUNCTION(bcmul)
235239
{
236240
zend_string *left, *right;
237-
zend_long scale_param = 0;
241+
zend_long scale_param;
242+
zend_bool scale_param_is_null = 1;
238243
bc_num first, second, result;
239-
int scale = BCG(bc_precision);
244+
int scale;
240245

241246
ZEND_PARSE_PARAMETERS_START(2, 3)
242247
Z_PARAM_STR(left)
243248
Z_PARAM_STR(right)
244249
Z_PARAM_OPTIONAL
245-
Z_PARAM_LONG(scale_param)
250+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
246251
ZEND_PARSE_PARAMETERS_END();
247252

248-
if (ZEND_NUM_ARGS() == 3) {
249-
if (scale_param < 0 || scale_param > INT_MAX) {
250-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
251-
RETURN_THROWS();
252-
}
253+
if (scale_param_is_null) {
254+
scale = BCG(bc_precision);
255+
} else if (scale_param < 0 || scale_param > INT_MAX) {
256+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
257+
RETURN_THROWS();
258+
} else {
253259
scale = (int) scale_param;
254260
}
255261

@@ -273,22 +279,24 @@ PHP_FUNCTION(bcmul)
273279
PHP_FUNCTION(bcdiv)
274280
{
275281
zend_string *left, *right;
276-
zend_long scale_param = 0;
282+
zend_long scale_param;
283+
zend_bool scale_param_is_null = 1;
277284
bc_num first, second, result;
278285
int scale = BCG(bc_precision);
279286

280287
ZEND_PARSE_PARAMETERS_START(2, 3)
281288
Z_PARAM_STR(left)
282289
Z_PARAM_STR(right)
283290
Z_PARAM_OPTIONAL
284-
Z_PARAM_LONG(scale_param)
291+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
285292
ZEND_PARSE_PARAMETERS_END();
286293

287-
if (ZEND_NUM_ARGS() == 3) {
288-
if (scale_param < 0 || scale_param > INT_MAX) {
289-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
290-
RETURN_THROWS();
291-
}
294+
if (scale_param_is_null) {
295+
scale = BCG(bc_precision);
296+
} else if (scale_param < 0 || scale_param > INT_MAX) {
297+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
298+
RETURN_THROWS();
299+
} else {
292300
scale = (int) scale_param;
293301
}
294302

@@ -319,22 +327,24 @@ PHP_FUNCTION(bcdiv)
319327
PHP_FUNCTION(bcmod)
320328
{
321329
zend_string *left, *right;
322-
zend_long scale_param = 0;
330+
zend_long scale_param;
331+
zend_bool scale_param_is_null = 1;
323332
bc_num first, second, result;
324333
int scale = BCG(bc_precision);
325334

326335
ZEND_PARSE_PARAMETERS_START(2, 3)
327336
Z_PARAM_STR(left)
328337
Z_PARAM_STR(right)
329338
Z_PARAM_OPTIONAL
330-
Z_PARAM_LONG(scale_param)
339+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
331340
ZEND_PARSE_PARAMETERS_END();
332341

333-
if (ZEND_NUM_ARGS() == 3) {
334-
if (scale_param < 0 || scale_param > INT_MAX) {
335-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
336-
RETURN_THROWS();
337-
}
342+
if (scale_param_is_null) {
343+
scale = BCG(bc_precision);
344+
} else if (scale_param < 0 || scale_param > INT_MAX) {
345+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
346+
RETURN_THROWS();
347+
} else {
338348
scale = (int) scale_param;
339349
}
340350

@@ -356,7 +366,6 @@ PHP_FUNCTION(bcmod)
356366
bc_free_num(&first);
357367
bc_free_num(&second);
358368
bc_free_num(&result);
359-
return;
360369
}
361370
/* }}} */
362371

@@ -365,7 +374,8 @@ PHP_FUNCTION(bcmod)
365374
PHP_FUNCTION(bcpowmod)
366375
{
367376
zend_string *left, *right, *modulus;
368-
zend_long scale_param = 0;
377+
zend_long scale_param;
378+
zend_bool scale_param_is_null = 1;
369379
bc_num first, second, mod, result;
370380
int scale = BCG(bc_precision);
371381

@@ -374,14 +384,15 @@ PHP_FUNCTION(bcpowmod)
374384
Z_PARAM_STR(right)
375385
Z_PARAM_STR(modulus)
376386
Z_PARAM_OPTIONAL
377-
Z_PARAM_LONG(scale_param)
387+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
378388
ZEND_PARSE_PARAMETERS_END();
379389

380-
if (ZEND_NUM_ARGS() == 4) {
381-
if (scale_param < 0 || scale_param > INT_MAX) {
382-
zend_argument_value_error(4, "must be between 0 and %d", INT_MAX);
383-
RETURN_THROWS();
384-
}
390+
if (scale_param_is_null) {
391+
scale = BCG(bc_precision);
392+
} else if (scale_param < 0 || scale_param > INT_MAX) {
393+
zend_argument_value_error(4, "must be between 0 and %d", INT_MAX);
394+
RETURN_THROWS();
395+
} else {
385396
scale = (int) scale_param;
386397
}
387398

@@ -403,7 +414,6 @@ PHP_FUNCTION(bcpowmod)
403414
bc_free_num(&second);
404415
bc_free_num(&mod);
405416
bc_free_num(&result);
406-
return;
407417
}
408418
/* }}} */
409419

@@ -412,22 +422,24 @@ PHP_FUNCTION(bcpowmod)
412422
PHP_FUNCTION(bcpow)
413423
{
414424
zend_string *left, *right;
415-
zend_long scale_param = 0;
425+
zend_long scale_param;
426+
zend_bool scale_param_is_null = 1;
416427
bc_num first, second, result;
417428
int scale = BCG(bc_precision);
418429

419430
ZEND_PARSE_PARAMETERS_START(2, 3)
420431
Z_PARAM_STR(left)
421432
Z_PARAM_STR(right)
422433
Z_PARAM_OPTIONAL
423-
Z_PARAM_LONG(scale_param)
434+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
424435
ZEND_PARSE_PARAMETERS_END();
425436

426-
if (ZEND_NUM_ARGS() == 3) {
427-
if (scale_param < 0 || scale_param > INT_MAX) {
428-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
429-
RETURN_THROWS();
430-
}
437+
if (scale_param_is_null) {
438+
scale = BCG(bc_precision);
439+
} else if (scale_param < 0 || scale_param > INT_MAX) {
440+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
441+
RETURN_THROWS();
442+
} else {
431443
scale = (int) scale_param;
432444
}
433445

@@ -442,7 +454,6 @@ PHP_FUNCTION(bcpow)
442454
bc_free_num(&first);
443455
bc_free_num(&second);
444456
bc_free_num(&result);
445-
return;
446457
}
447458
/* }}} */
448459

@@ -451,21 +462,23 @@ PHP_FUNCTION(bcpow)
451462
PHP_FUNCTION(bcsqrt)
452463
{
453464
zend_string *left;
454-
zend_long scale_param = 0;
465+
zend_long scale_param;
466+
zend_bool scale_param_is_null = 1;
455467
bc_num result;
456468
int scale = BCG(bc_precision);
457469

458470
ZEND_PARSE_PARAMETERS_START(1, 2)
459471
Z_PARAM_STR(left)
460472
Z_PARAM_OPTIONAL
461-
Z_PARAM_LONG(scale_param)
473+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
462474
ZEND_PARSE_PARAMETERS_END();
463475

464-
if (ZEND_NUM_ARGS() == 2) {
465-
if (scale_param < 0 || scale_param > INT_MAX) {
466-
zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
467-
RETURN_THROWS();
468-
}
476+
if (scale_param_is_null) {
477+
scale = BCG(bc_precision);
478+
} else if (scale_param < 0 || scale_param > INT_MAX) {
479+
zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
480+
RETURN_THROWS();
481+
} else {
469482
scale = (int) scale_param;
470483
}
471484

@@ -488,22 +501,24 @@ PHP_FUNCTION(bcsqrt)
488501
PHP_FUNCTION(bccomp)
489502
{
490503
zend_string *left, *right;
491-
zend_long scale_param = 0;
504+
zend_long scale_param;
505+
zend_bool scale_param_is_null = 1;
492506
bc_num first, second;
493507
int scale = BCG(bc_precision);
494508

495509
ZEND_PARSE_PARAMETERS_START(2, 3)
496510
Z_PARAM_STR(left)
497511
Z_PARAM_STR(right)
498512
Z_PARAM_OPTIONAL
499-
Z_PARAM_LONG(scale_param)
513+
Z_PARAM_LONG_OR_NULL(scale_param, scale_param_is_null)
500514
ZEND_PARSE_PARAMETERS_END();
501515

502-
if (ZEND_NUM_ARGS() == 3) {
503-
if (scale_param < 0 || scale_param > INT_MAX) {
504-
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
505-
RETURN_THROWS();
506-
}
516+
if (scale_param_is_null) {
517+
scale = BCG(bc_precision);
518+
} else if (scale_param < 0 || scale_param > INT_MAX) {
519+
zend_argument_value_error(3, "must be between 0 and %d", INT_MAX);
520+
RETURN_THROWS();
521+
} else {
507522
scale = (int) scale_param;
508523
}
509524

@@ -529,19 +544,21 @@ PHP_FUNCTION(bccomp)
529544
PHP_FUNCTION(bcscale)
530545
{
531546
zend_long old_scale, new_scale;
547+
zend_bool new_scale_is_null = 1;
532548

533549
ZEND_PARSE_PARAMETERS_START(0, 1)
534550
Z_PARAM_OPTIONAL
535-
Z_PARAM_LONG(new_scale)
551+
Z_PARAM_LONG_OR_NULL(new_scale, new_scale_is_null)
536552
ZEND_PARSE_PARAMETERS_END();
537553

538554
old_scale = BCG(bc_precision);
539555

540-
if (ZEND_NUM_ARGS() == 1) {
556+
if (!new_scale_is_null) {
541557
if (new_scale < 0 || new_scale > INT_MAX) {
542558
zend_argument_value_error(1, "must be between 0 and %d", INT_MAX);
543559
RETURN_THROWS();
544560
}
561+
545562
BCG(bc_precision) = (int) new_scale;
546563
}
547564

ext/bcmath/bcmath.stub.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22

33
/** @generate-function-entries */
44

5-
function bcadd(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
5+
function bcadd(string $left_operand, string $right_operand, ?int $scale = null): string {}
66

7-
function bcsub(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
7+
function bcsub(string $left_operand, string $right_operand, ?int $scale = null): string {}
88

9-
function bcmul(string $left_operand, string $right_operand, int $scale = UNKNOWN): string {}
9+
function bcmul(string $left_operand, string $right_operand, ?int $scale = null): string {}
1010

11-
function bcdiv(string $dividend, string $divisor, int $scale = UNKNOWN): string {}
11+
function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {}
1212

13-
function bcmod(string $dividend, string $divisor, int $scale = UNKNOWN): string {}
13+
function bcmod(string $dividend, string $divisor, ?int $scale = null): string {}
1414

15-
function bcpowmod(string $base, string $exponent, string $modulus, int $scale = UNKNOWN): string|false {}
15+
function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {}
1616

17-
function bcpow(string $base, string $exponent, int $scale = UNKNOWN): string {}
17+
function bcpow(string $base, string $exponent, ?int $scale = null): string {}
1818

19-
function bcsqrt(string $operand, int $scale = UNKNOWN): string {}
19+
function bcsqrt(string $operand, ?int $scale = null): string {}
2020

21-
function bccomp(string $left_operand, string $right_operand, int $scale = UNKNOWN): int {}
21+
function bccomp(string $left_operand, string $right_operand, ?int $scale = null): int {}
2222

23-
function bcscale(int $scale = UNKNOWN): int {}
23+
function bcscale(?int $scale = null): int {}

0 commit comments

Comments
 (0)