@@ -45,7 +45,7 @@ zend_module_entry bcmath_module_entry = {
45
45
PHP_BCMATH_VERSION ,
46
46
PHP_MODULE_GLOBALS (bcmath ),
47
47
PHP_GINIT (bcmath ),
48
- PHP_GSHUTDOWN (bcmath ),
48
+ PHP_GSHUTDOWN (bcmath ),
49
49
NULL ,
50
50
STANDARD_MODULE_PROPERTIES_EX
51
51
};
@@ -57,9 +57,32 @@ ZEND_TSRMLS_CACHE_DEFINE()
57
57
ZEND_GET_MODULE (bcmath )
58
58
#endif
59
59
60
+ ZEND_INI_MH (OnUpdateScale )
61
+ {
62
+ int * p ;
63
+ zend_long tmp ;
64
+ #ifndef ZTS
65
+ char * base = (char * ) mh_arg2 ;
66
+ #else
67
+ char * base ;
68
+
69
+ base = (char * ) ts_resource (* ((int * ) mh_arg2 ));
70
+ #endif
71
+
72
+ tmp = zend_atol (ZSTR_VAL (new_value ), ZSTR_LEN (new_value ));
73
+ if (tmp < 0 || tmp > INT_MAX ) {
74
+ return FAILURE ;
75
+ }
76
+
77
+ p = (int * ) (base + (size_t ) mh_arg1 );
78
+ * p = (int ) tmp ;
79
+
80
+ return SUCCESS ;
81
+ }
82
+
60
83
/* {{{ PHP_INI */
61
84
PHP_INI_BEGIN ()
62
- STD_PHP_INI_ENTRY ("bcmath.scale" , "0" , PHP_INI_ALL , OnUpdateLongGEZero , bc_precision , zend_bcmath_globals , bcmath_globals )
85
+ STD_PHP_INI_ENTRY ("bcmath.scale" , "0" , PHP_INI_ALL , OnUpdateScale , bc_precision , zend_bcmath_globals , bcmath_globals )
63
86
PHP_INI_END ()
64
87
/* }}} */
65
88
@@ -152,7 +175,11 @@ PHP_FUNCTION(bcadd)
152
175
ZEND_PARSE_PARAMETERS_END ();
153
176
154
177
if (ZEND_NUM_ARGS () == 3 ) {
155
- scale = (int ) (scale_param < 0 ? 0 : scale_param );
178
+ if (scale_param < 0 || scale_param > INT_MAX ) {
179
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
180
+ RETURN_THROWS ();
181
+ }
182
+ scale = (int ) scale_param ;
156
183
}
157
184
158
185
bc_init_num (& first );
@@ -187,7 +214,11 @@ PHP_FUNCTION(bcsub)
187
214
ZEND_PARSE_PARAMETERS_END ();
188
215
189
216
if (ZEND_NUM_ARGS () == 3 ) {
190
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
217
+ if (scale_param < 0 || scale_param > INT_MAX ) {
218
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
219
+ RETURN_THROWS ();
220
+ }
221
+ scale = (int ) scale_param ;
191
222
}
192
223
193
224
bc_init_num (& first );
@@ -222,7 +253,11 @@ PHP_FUNCTION(bcmul)
222
253
ZEND_PARSE_PARAMETERS_END ();
223
254
224
255
if (ZEND_NUM_ARGS () == 3 ) {
225
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
256
+ if (scale_param < 0 || scale_param > INT_MAX ) {
257
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
258
+ RETURN_THROWS ();
259
+ }
260
+ scale = (int ) scale_param ;
226
261
}
227
262
228
263
bc_init_num (& first );
@@ -257,7 +292,11 @@ PHP_FUNCTION(bcdiv)
257
292
ZEND_PARSE_PARAMETERS_END ();
258
293
259
294
if (ZEND_NUM_ARGS () == 3 ) {
260
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
295
+ if (scale_param < 0 || scale_param > INT_MAX ) {
296
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
297
+ RETURN_THROWS ();
298
+ }
299
+ scale = (int ) scale_param ;
261
300
}
262
301
263
302
bc_init_num (& first );
@@ -299,7 +338,11 @@ PHP_FUNCTION(bcmod)
299
338
ZEND_PARSE_PARAMETERS_END ();
300
339
301
340
if (ZEND_NUM_ARGS () == 3 ) {
302
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
341
+ if (scale_param < 0 || scale_param > INT_MAX ) {
342
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
343
+ RETURN_THROWS ();
344
+ }
345
+ scale = (int ) scale_param ;
303
346
}
304
347
305
348
bc_init_num (& first );
@@ -329,18 +372,26 @@ PHP_FUNCTION(bcmod)
329
372
PHP_FUNCTION (bcpowmod )
330
373
{
331
374
zend_string * left , * right , * modulus ;
375
+ zend_long scale_param = 0 ;
332
376
bc_num first , second , mod , result ;
333
- zend_long scale = BCG (bc_precision );
334
- int scale_int ;
377
+ int scale = (int )BCG (bc_precision );
335
378
336
379
ZEND_PARSE_PARAMETERS_START (3 , 4 )
337
380
Z_PARAM_STR (left )
338
381
Z_PARAM_STR (right )
339
382
Z_PARAM_STR (modulus )
340
383
Z_PARAM_OPTIONAL
341
- Z_PARAM_LONG (scale )
384
+ Z_PARAM_LONG (scale_param )
342
385
ZEND_PARSE_PARAMETERS_END ();
343
386
387
+ if (ZEND_NUM_ARGS () == 4 ) {
388
+ if (scale_param < 0 || scale_param > INT_MAX ) {
389
+ zend_argument_value_error (4 , "must be between 0 and %d" , INT_MAX );
390
+ RETURN_THROWS ();
391
+ }
392
+ scale = (int ) scale_param ;
393
+ }
394
+
344
395
bc_init_num (& first );
345
396
bc_init_num (& second );
346
397
bc_init_num (& mod );
@@ -349,10 +400,8 @@ PHP_FUNCTION(bcpowmod)
349
400
php_str2num (& second , ZSTR_VAL (right ));
350
401
php_str2num (& mod , ZSTR_VAL (modulus ));
351
402
352
- scale_int = (int ) ((int )scale < 0 ? 0 : scale );
353
-
354
- if (bc_raisemod (first , second , mod , & result , scale_int ) != -1 ) {
355
- RETVAL_STR (bc_num2str_ex (result , scale_int ));
403
+ if (bc_raisemod (first , second , mod , & result , scale ) != -1 ) {
404
+ RETVAL_STR (bc_num2str_ex (result , scale ));
356
405
} else {
357
406
RETVAL_FALSE ;
358
407
}
@@ -382,7 +431,11 @@ PHP_FUNCTION(bcpow)
382
431
ZEND_PARSE_PARAMETERS_END ();
383
432
384
433
if (ZEND_NUM_ARGS () == 3 ) {
385
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
434
+ if (scale_param < 0 || scale_param > INT_MAX ) {
435
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
436
+ RETURN_THROWS ();
437
+ }
438
+ scale = (int ) scale_param ;
386
439
}
387
440
388
441
bc_init_num (& first );
@@ -416,7 +469,11 @@ PHP_FUNCTION(bcsqrt)
416
469
ZEND_PARSE_PARAMETERS_END ();
417
470
418
471
if (ZEND_NUM_ARGS () == 2 ) {
419
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
472
+ if (scale_param < 0 || scale_param > INT_MAX ) {
473
+ zend_argument_value_error (2 , "must be between 0 and %d" , INT_MAX );
474
+ RETURN_THROWS ();
475
+ }
476
+ scale = (int ) scale_param ;
420
477
}
421
478
422
479
bc_init_num (& result );
@@ -450,7 +507,11 @@ PHP_FUNCTION(bccomp)
450
507
ZEND_PARSE_PARAMETERS_END ();
451
508
452
509
if (ZEND_NUM_ARGS () == 3 ) {
453
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
510
+ if (scale_param < 0 || scale_param > INT_MAX ) {
511
+ zend_argument_value_error (3 , "must be between 0 and %d" , INT_MAX );
512
+ RETURN_THROWS ();
513
+ }
514
+ scale = (int ) scale_param ;
454
515
}
455
516
456
517
bc_init_num (& first );
@@ -460,7 +521,7 @@ PHP_FUNCTION(bccomp)
460
521
php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
461
522
}
462
523
if (!bc_str2num (& second , ZSTR_VAL (right ), scale )) {
463
- php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
524
+ php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
464
525
}
465
526
RETVAL_LONG (bc_compare (first , second ));
466
527
@@ -484,7 +545,11 @@ PHP_FUNCTION(bcscale)
484
545
old_scale = BCG (bc_precision );
485
546
486
547
if (ZEND_NUM_ARGS () == 1 ) {
487
- BCG (bc_precision ) = ((int )new_scale < 0 ) ? 0 : new_scale ;
548
+ if (new_scale < 0 || new_scale > INT_MAX ) {
549
+ zend_argument_value_error (1 , "must be between 0 and %d" , INT_MAX );
550
+ RETURN_THROWS ();
551
+ }
552
+ BCG (bc_precision ) = (int ) new_scale ;
488
553
}
489
554
490
555
RETURN_LONG (old_scale );
0 commit comments