@@ -129,20 +129,23 @@ PHP_MINFO_FUNCTION(bcmath)
129
129
130
130
/* {{{ php_str2num
131
131
Convert to bc_num detecting scale */
132
- static void php_str2num (bc_num * num , char * str )
132
+ static zend_result php_str2num (bc_num * num , char * str )
133
133
{
134
134
char * p ;
135
135
136
136
if (!(p = strchr (str , '.' ))) {
137
137
if (!bc_str2num (num , str , 0 )) {
138
- php_error_docref ( NULL , E_WARNING , "bcmath function argument is not well-formed" ) ;
138
+ return FAILURE ;
139
139
}
140
- return ;
140
+
141
+ return SUCCESS ;
141
142
}
142
143
143
144
if (!bc_str2num (num , str , strlen (p + 1 ))) {
144
- php_error_docref ( NULL , E_WARNING , "bcmath function argument is not well-formed" ) ;
145
+ return FAILURE ;
145
146
}
147
+
148
+ return SUCCESS ;
146
149
}
147
150
/* }}} */
148
151
@@ -174,15 +177,26 @@ PHP_FUNCTION(bcadd)
174
177
bc_init_num (& first );
175
178
bc_init_num (& second );
176
179
bc_init_num (& result );
177
- php_str2num (& first , ZSTR_VAL (left ));
178
- php_str2num (& second , ZSTR_VAL (right ));
180
+
181
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
182
+ zend_argument_value_error (1 , "is not well-formed" );
183
+ goto cleanup ;
184
+ }
185
+
186
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
187
+ zend_argument_value_error (2 , "is not well-formed" );
188
+ goto cleanup ;
189
+ }
190
+
179
191
bc_add (first , second , & result , scale );
180
192
181
193
RETVAL_STR (bc_num2str_ex (result , scale ));
182
- bc_free_num (& first );
183
- bc_free_num (& second );
184
- bc_free_num (& result );
185
- return ;
194
+
195
+ cleanup : {
196
+ bc_free_num (& first );
197
+ bc_free_num (& second );
198
+ bc_free_num (& result );
199
+ };
186
200
}
187
201
/* }}} */
188
202
@@ -214,15 +228,26 @@ PHP_FUNCTION(bcsub)
214
228
bc_init_num (& first );
215
229
bc_init_num (& second );
216
230
bc_init_num (& result );
217
- php_str2num (& first , ZSTR_VAL (left ));
218
- php_str2num (& second , ZSTR_VAL (right ));
231
+
232
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
233
+ zend_argument_value_error (1 , "is not well-formed" );
234
+ goto cleanup ;
235
+ }
236
+
237
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
238
+ zend_argument_value_error (2 , "is not well-formed" );
239
+ goto cleanup ;
240
+ }
241
+
219
242
bc_sub (first , second , & result , scale );
220
243
221
244
RETVAL_STR (bc_num2str_ex (result , scale ));
222
- bc_free_num (& first );
223
- bc_free_num (& second );
224
- bc_free_num (& result );
225
- return ;
245
+
246
+ cleanup : {
247
+ bc_free_num (& first );
248
+ bc_free_num (& second );
249
+ bc_free_num (& result );
250
+ };
226
251
}
227
252
/* }}} */
228
253
@@ -254,15 +279,26 @@ PHP_FUNCTION(bcmul)
254
279
bc_init_num (& first );
255
280
bc_init_num (& second );
256
281
bc_init_num (& result );
257
- php_str2num (& first , ZSTR_VAL (left ));
258
- php_str2num (& second , ZSTR_VAL (right ));
282
+
283
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
284
+ zend_argument_value_error (1 , "is not well-formed" );
285
+ goto cleanup ;
286
+ }
287
+
288
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
289
+ zend_argument_value_error (2 , "is not well-formed" );
290
+ goto cleanup ;
291
+ }
292
+
259
293
bc_multiply (first , second , & result , scale );
260
294
261
295
RETVAL_STR (bc_num2str_ex (result , scale ));
262
- bc_free_num (& first );
263
- bc_free_num (& second );
264
- bc_free_num (& result );
265
- return ;
296
+
297
+ cleanup : {
298
+ bc_free_num (& first );
299
+ bc_free_num (& second );
300
+ bc_free_num (& result );
301
+ };
266
302
}
267
303
/* }}} */
268
304
@@ -294,8 +330,16 @@ PHP_FUNCTION(bcdiv)
294
330
bc_init_num (& first );
295
331
bc_init_num (& second );
296
332
bc_init_num (& result );
297
- php_str2num (& first , ZSTR_VAL (left ));
298
- php_str2num (& second , ZSTR_VAL (right ));
333
+
334
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
335
+ zend_argument_value_error (1 , "is not well-formed" );
336
+ goto cleanup ;
337
+ }
338
+
339
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
340
+ zend_argument_value_error (2 , "is not well-formed" );
341
+ goto cleanup ;
342
+ }
299
343
300
344
switch (bc_divide (first , second , & result , scale )) {
301
345
case 0 : /* OK */
@@ -306,10 +350,11 @@ PHP_FUNCTION(bcdiv)
306
350
break ;
307
351
}
308
352
309
- bc_free_num (& first );
310
- bc_free_num (& second );
311
- bc_free_num (& result );
312
- return ;
353
+ cleanup : {
354
+ bc_free_num (& first );
355
+ bc_free_num (& second );
356
+ bc_free_num (& result );
357
+ };
313
358
}
314
359
/* }}} */
315
360
@@ -341,8 +386,16 @@ PHP_FUNCTION(bcmod)
341
386
bc_init_num (& first );
342
387
bc_init_num (& second );
343
388
bc_init_num (& result );
344
- php_str2num (& first , ZSTR_VAL (left ));
345
- php_str2num (& second , ZSTR_VAL (right ));
389
+
390
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
391
+ zend_argument_value_error (1 , "is not well-formed" );
392
+ goto cleanup ;
393
+ }
394
+
395
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
396
+ zend_argument_value_error (2 , "is not well-formed" );
397
+ goto cleanup ;
398
+ }
346
399
347
400
switch (bc_modulo (first , second , & result , scale )) {
348
401
case 0 :
@@ -353,9 +406,11 @@ PHP_FUNCTION(bcmod)
353
406
break ;
354
407
}
355
408
356
- bc_free_num (& first );
357
- bc_free_num (& second );
358
- bc_free_num (& result );
409
+ cleanup : {
410
+ bc_free_num (& first );
411
+ bc_free_num (& second );
412
+ bc_free_num (& result );
413
+ };
359
414
}
360
415
/* }}} */
361
416
@@ -389,18 +444,32 @@ PHP_FUNCTION(bcpowmod)
389
444
bc_init_num (& second );
390
445
bc_init_num (& mod );
391
446
bc_init_num (& result );
392
- php_str2num (& first , ZSTR_VAL (left ));
393
- php_str2num (& second , ZSTR_VAL (right ));
394
- php_str2num (& mod , ZSTR_VAL (modulus ));
447
+
448
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
449
+ zend_argument_value_error (1 , "is not well-formed" );
450
+ goto cleanup ;
451
+ }
452
+
453
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
454
+ zend_argument_value_error (2 , "is not well-formed" );
455
+ goto cleanup ;
456
+ }
457
+
458
+ if (php_str2num (& mod , ZSTR_VAL (modulus )) == FAILURE ) {
459
+ zend_argument_value_error (3 , "is not well-formed" );
460
+ goto cleanup ;
461
+ }
395
462
396
463
if (bc_raisemod (first , second , mod , & result , scale ) == SUCCESS ) {
397
464
RETVAL_STR (bc_num2str_ex (result , scale ));
398
465
}
399
466
400
- bc_free_num (& first );
401
- bc_free_num (& second );
402
- bc_free_num (& mod );
403
- bc_free_num (& result );
467
+ cleanup : {
468
+ bc_free_num (& first );
469
+ bc_free_num (& second );
470
+ bc_free_num (& mod );
471
+ bc_free_num (& result );
472
+ };
404
473
}
405
474
/* }}} */
406
475
@@ -432,14 +501,26 @@ PHP_FUNCTION(bcpow)
432
501
bc_init_num (& first );
433
502
bc_init_num (& second );
434
503
bc_init_num (& result );
435
- php_str2num (& first , ZSTR_VAL (left ));
436
- php_str2num (& second , ZSTR_VAL (right ));
504
+
505
+ if (php_str2num (& first , ZSTR_VAL (left )) == FAILURE ) {
506
+ zend_argument_value_error (1 , "is not well-formed" );
507
+ goto cleanup ;
508
+ }
509
+
510
+ if (php_str2num (& second , ZSTR_VAL (right )) == FAILURE ) {
511
+ zend_argument_value_error (2 , "is not well-formed" );
512
+ goto cleanup ;
513
+ }
514
+
437
515
bc_raise (first , second , & result , scale );
438
516
439
517
RETVAL_STR (bc_num2str_ex (result , scale ));
440
- bc_free_num (& first );
441
- bc_free_num (& second );
442
- bc_free_num (& result );
518
+
519
+ cleanup : {
520
+ bc_free_num (& first );
521
+ bc_free_num (& second );
522
+ bc_free_num (& result );
523
+ };
443
524
}
444
525
/* }}} */
445
526
@@ -468,16 +549,21 @@ PHP_FUNCTION(bcsqrt)
468
549
}
469
550
470
551
bc_init_num (& result );
471
- php_str2num (& result , ZSTR_VAL (left ));
552
+
553
+ if (php_str2num (& result , ZSTR_VAL (left )) == FAILURE ) {
554
+ zend_argument_value_error (1 , "is not well-formed" );
555
+ goto cleanup ;
556
+ }
472
557
473
558
if (bc_sqrt (& result , scale ) != 0 ) {
474
559
RETVAL_STR (bc_num2str_ex (result , scale ));
475
560
} else {
476
561
zend_argument_value_error (1 , "must be greater than or equal to 0" );
477
562
}
478
563
479
- bc_free_num (& result );
480
- return ;
564
+ cleanup : {
565
+ bc_free_num (& result );
566
+ };
481
567
}
482
568
/* }}} */
483
569
@@ -510,16 +596,21 @@ PHP_FUNCTION(bccomp)
510
596
bc_init_num (& second );
511
597
512
598
if (!bc_str2num (& first , ZSTR_VAL (left ), scale )) {
513
- php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
599
+ zend_argument_value_error (1 , "is not well-formed" );
600
+ goto cleanup ;
514
601
}
602
+
515
603
if (!bc_str2num (& second , ZSTR_VAL (right ), scale )) {
516
- php_error_docref (NULL , E_WARNING , "bcmath function argument is not well-formed" );
604
+ zend_argument_value_error (2 , "is not well-formed" );
605
+ goto cleanup ;
517
606
}
607
+
518
608
RETVAL_LONG (bc_compare (first , second ));
519
609
520
- bc_free_num (& first );
521
- bc_free_num (& second );
522
- return ;
610
+ cleanup : {
611
+ bc_free_num (& first );
612
+ bc_free_num (& second );
613
+ };
523
614
}
524
615
/* }}} */
525
616
0 commit comments