@@ -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,25 @@ 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
+
65
+ tmp = zend_atol (ZSTR_VAL (new_value ), ZSTR_LEN (new_value ));
66
+ if (tmp < 0 || tmp > INT_MAX ) {
67
+ return FAILURE ;
68
+ }
69
+
70
+ p = (int * ) ZEND_INI_GET_ADDR ();
71
+ * p = (int ) tmp ;
72
+
73
+ return SUCCESS ;
74
+ }
75
+
60
76
/* {{{ PHP_INI */
61
77
PHP_INI_BEGIN ()
62
- STD_PHP_INI_ENTRY ("bcmath.scale" , "0" , PHP_INI_ALL , OnUpdateLongGEZero , bc_precision , zend_bcmath_globals , bcmath_globals )
78
+ STD_PHP_INI_ENTRY ("bcmath.scale" , "0" , PHP_INI_ALL , OnUpdateScale , bc_precision , zend_bcmath_globals , bcmath_globals )
63
79
PHP_INI_END ()
64
80
/* }}} */
65
81
@@ -142,7 +158,7 @@ PHP_FUNCTION(bcadd)
142
158
zend_string * left , * right ;
143
159
zend_long scale_param = 0 ;
144
160
bc_num first , second , result ;
145
- int scale = ( int ) BCG (bc_precision );
161
+ int scale = BCG (bc_precision );
146
162
147
163
ZEND_PARSE_PARAMETERS_START (2 , 3 )
148
164
Z_PARAM_STR (left )
@@ -152,7 +168,11 @@ PHP_FUNCTION(bcadd)
152
168
ZEND_PARSE_PARAMETERS_END ();
153
169
154
170
if (ZEND_NUM_ARGS () == 3 ) {
155
- scale = (int ) (scale_param < 0 ? 0 : scale_param );
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
+ }
175
+ scale = (int ) scale_param ;
156
176
}
157
177
158
178
bc_init_num (& first );
@@ -177,7 +197,7 @@ PHP_FUNCTION(bcsub)
177
197
zend_string * left , * right ;
178
198
zend_long scale_param = 0 ;
179
199
bc_num first , second , result ;
180
- int scale = ( int ) BCG (bc_precision );
200
+ int scale = BCG (bc_precision );
181
201
182
202
ZEND_PARSE_PARAMETERS_START (2 , 3 )
183
203
Z_PARAM_STR (left )
@@ -187,7 +207,11 @@ PHP_FUNCTION(bcsub)
187
207
ZEND_PARSE_PARAMETERS_END ();
188
208
189
209
if (ZEND_NUM_ARGS () == 3 ) {
190
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
214
+ scale = (int ) scale_param ;
191
215
}
192
216
193
217
bc_init_num (& first );
@@ -212,7 +236,7 @@ PHP_FUNCTION(bcmul)
212
236
zend_string * left , * right ;
213
237
zend_long scale_param = 0 ;
214
238
bc_num first , second , result ;
215
- int scale = ( int ) BCG (bc_precision );
239
+ int scale = BCG (bc_precision );
216
240
217
241
ZEND_PARSE_PARAMETERS_START (2 , 3 )
218
242
Z_PARAM_STR (left )
@@ -222,7 +246,11 @@ PHP_FUNCTION(bcmul)
222
246
ZEND_PARSE_PARAMETERS_END ();
223
247
224
248
if (ZEND_NUM_ARGS () == 3 ) {
225
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ scale = (int ) scale_param ;
226
254
}
227
255
228
256
bc_init_num (& first );
@@ -247,7 +275,7 @@ PHP_FUNCTION(bcdiv)
247
275
zend_string * left , * right ;
248
276
zend_long scale_param = 0 ;
249
277
bc_num first , second , result ;
250
- int scale = ( int ) BCG (bc_precision );
278
+ int scale = BCG (bc_precision );
251
279
252
280
ZEND_PARSE_PARAMETERS_START (2 , 3 )
253
281
Z_PARAM_STR (left )
@@ -257,7 +285,11 @@ PHP_FUNCTION(bcdiv)
257
285
ZEND_PARSE_PARAMETERS_END ();
258
286
259
287
if (ZEND_NUM_ARGS () == 3 ) {
260
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
292
+ scale = (int ) scale_param ;
261
293
}
262
294
263
295
bc_init_num (& first );
@@ -289,7 +321,7 @@ PHP_FUNCTION(bcmod)
289
321
zend_string * left , * right ;
290
322
zend_long scale_param = 0 ;
291
323
bc_num first , second , result ;
292
- int scale = ( int ) BCG (bc_precision );
324
+ int scale = BCG (bc_precision );
293
325
294
326
ZEND_PARSE_PARAMETERS_START (2 , 3 )
295
327
Z_PARAM_STR (left )
@@ -299,7 +331,11 @@ PHP_FUNCTION(bcmod)
299
331
ZEND_PARSE_PARAMETERS_END ();
300
332
301
333
if (ZEND_NUM_ARGS () == 3 ) {
302
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
338
+ scale = (int ) scale_param ;
303
339
}
304
340
305
341
bc_init_num (& first );
@@ -329,18 +365,26 @@ PHP_FUNCTION(bcmod)
329
365
PHP_FUNCTION (bcpowmod )
330
366
{
331
367
zend_string * left , * right , * modulus ;
368
+ zend_long scale_param = 0 ;
332
369
bc_num first , second , mod , result ;
333
- zend_long scale = BCG (bc_precision );
334
- int scale_int ;
370
+ int scale = BCG (bc_precision );
335
371
336
372
ZEND_PARSE_PARAMETERS_START (3 , 4 )
337
373
Z_PARAM_STR (left )
338
374
Z_PARAM_STR (right )
339
375
Z_PARAM_STR (modulus )
340
376
Z_PARAM_OPTIONAL
341
- Z_PARAM_LONG (scale )
377
+ Z_PARAM_LONG (scale_param )
342
378
ZEND_PARSE_PARAMETERS_END ();
343
379
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
+ }
385
+ scale = (int ) scale_param ;
386
+ }
387
+
344
388
bc_init_num (& first );
345
389
bc_init_num (& second );
346
390
bc_init_num (& mod );
@@ -349,10 +393,8 @@ PHP_FUNCTION(bcpowmod)
349
393
php_str2num (& second , ZSTR_VAL (right ));
350
394
php_str2num (& mod , ZSTR_VAL (modulus ));
351
395
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 ));
396
+ if (bc_raisemod (first , second , mod , & result , scale ) != -1 ) {
397
+ RETVAL_STR (bc_num2str_ex (result , scale ));
356
398
} else {
357
399
RETVAL_FALSE ;
358
400
}
@@ -372,7 +414,7 @@ PHP_FUNCTION(bcpow)
372
414
zend_string * left , * right ;
373
415
zend_long scale_param = 0 ;
374
416
bc_num first , second , result ;
375
- int scale = ( int ) BCG (bc_precision );
417
+ int scale = BCG (bc_precision );
376
418
377
419
ZEND_PARSE_PARAMETERS_START (2 , 3 )
378
420
Z_PARAM_STR (left )
@@ -382,7 +424,11 @@ PHP_FUNCTION(bcpow)
382
424
ZEND_PARSE_PARAMETERS_END ();
383
425
384
426
if (ZEND_NUM_ARGS () == 3 ) {
385
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
431
+ scale = (int ) scale_param ;
386
432
}
387
433
388
434
bc_init_num (& first );
@@ -407,7 +453,7 @@ PHP_FUNCTION(bcsqrt)
407
453
zend_string * left ;
408
454
zend_long scale_param = 0 ;
409
455
bc_num result ;
410
- int scale = ( int ) BCG (bc_precision );
456
+ int scale = BCG (bc_precision );
411
457
412
458
ZEND_PARSE_PARAMETERS_START (1 , 2 )
413
459
Z_PARAM_STR (left )
@@ -416,7 +462,11 @@ PHP_FUNCTION(bcsqrt)
416
462
ZEND_PARSE_PARAMETERS_END ();
417
463
418
464
if (ZEND_NUM_ARGS () == 2 ) {
419
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
469
+ scale = (int ) scale_param ;
420
470
}
421
471
422
472
bc_init_num (& result );
@@ -440,7 +490,7 @@ PHP_FUNCTION(bccomp)
440
490
zend_string * left , * right ;
441
491
zend_long scale_param = 0 ;
442
492
bc_num first , second ;
443
- int scale = ( int ) BCG (bc_precision );
493
+ int scale = BCG (bc_precision );
444
494
445
495
ZEND_PARSE_PARAMETERS_START (2 , 3 )
446
496
Z_PARAM_STR (left )
@@ -450,7 +500,11 @@ PHP_FUNCTION(bccomp)
450
500
ZEND_PARSE_PARAMETERS_END ();
451
501
452
502
if (ZEND_NUM_ARGS () == 3 ) {
453
- scale = (int ) ((int )scale_param < 0 ? 0 : scale_param );
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
+ }
507
+ scale = (int ) scale_param ;
454
508
}
455
509
456
510
bc_init_num (& first );
@@ -460,7 +514,7 @@ PHP_FUNCTION(bccomp)
460
514
php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
461
515
}
462
516
if (!bc_str2num (& second , ZSTR_VAL (right ), scale )) {
463
- php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
517
+ php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
464
518
}
465
519
RETVAL_LONG (bc_compare (first , second ));
466
520
@@ -484,7 +538,11 @@ PHP_FUNCTION(bcscale)
484
538
old_scale = BCG (bc_precision );
485
539
486
540
if (ZEND_NUM_ARGS () == 1 ) {
487
- BCG (bc_precision ) = ((int )new_scale < 0 ) ? 0 : new_scale ;
541
+ if (new_scale < 0 || new_scale > INT_MAX ) {
542
+ zend_argument_value_error (1 , "must be between 0 and %d" , INT_MAX );
543
+ RETURN_THROWS ();
544
+ }
545
+ BCG (bc_precision ) = (int ) new_scale ;
488
546
}
489
547
490
548
RETURN_LONG (old_scale );
0 commit comments