29
29
#include "php_bcmath.h"
30
30
#include "libbcmath/src/bcmath.h"
31
31
32
+ /* Always pair SETUP with TEARDOWN, and do so in the outer scope!
33
+ * Should not be used when data can escape the function. */
34
+ #define BC_ARENA_SETUP \
35
+ char bc_arena[BC_ARENA_SIZE]; \
36
+ BCG(arena) = bc_arena;
37
+
38
+ #define BC_ARENA_TEARDOWN \
39
+ BCG(arena) = NULL; \
40
+ BCG(arena_offset) = 0;
41
+
32
42
ZEND_DECLARE_MODULE_GLOBALS (bcmath )
33
43
static PHP_GINIT_FUNCTION (bcmath );
34
44
static PHP_GSHUTDOWN_FUNCTION (bcmath );
@@ -89,6 +99,8 @@ static PHP_GINIT_FUNCTION(bcmath)
89
99
ZEND_TSRMLS_CACHE_UPDATE ();
90
100
#endif
91
101
bcmath_globals -> bc_precision = 0 ;
102
+ bcmath_globals -> arena = NULL ;
103
+ bcmath_globals -> arena_offset = 0 ;
92
104
bc_init_numbers ();
93
105
}
94
106
/* }}} */
@@ -99,6 +111,8 @@ static PHP_GSHUTDOWN_FUNCTION(bcmath)
99
111
_bc_free_num_ex (& bcmath_globals -> _zero_ , 1 );
100
112
_bc_free_num_ex (& bcmath_globals -> _one_ , 1 );
101
113
_bc_free_num_ex (& bcmath_globals -> _two_ , 1 );
114
+ bcmath_globals -> arena = NULL ;
115
+ bcmath_globals -> arena_offset = 0 ;
102
116
}
103
117
/* }}} */
104
118
@@ -167,6 +181,8 @@ PHP_FUNCTION(bcadd)
167
181
scale = (int ) scale_param ;
168
182
}
169
183
184
+ BC_ARENA_SETUP ;
185
+
170
186
if (php_str2num (& first , left ) == FAILURE ) {
171
187
zend_argument_value_error (1 , "is not well-formed" );
172
188
goto cleanup ;
@@ -185,6 +201,7 @@ PHP_FUNCTION(bcadd)
185
201
bc_free_num (& first );
186
202
bc_free_num (& second );
187
203
bc_free_num (& result );
204
+ BC_ARENA_TEARDOWN ;
188
205
};
189
206
}
190
207
/* }}} */
@@ -214,6 +231,8 @@ PHP_FUNCTION(bcsub)
214
231
scale = (int ) scale_param ;
215
232
}
216
233
234
+ BC_ARENA_SETUP ;
235
+
217
236
if (php_str2num (& first , left ) == FAILURE ) {
218
237
zend_argument_value_error (1 , "is not well-formed" );
219
238
goto cleanup ;
@@ -232,6 +251,7 @@ PHP_FUNCTION(bcsub)
232
251
bc_free_num (& first );
233
252
bc_free_num (& second );
234
253
bc_free_num (& result );
254
+ BC_ARENA_TEARDOWN ;
235
255
};
236
256
}
237
257
/* }}} */
@@ -261,6 +281,8 @@ PHP_FUNCTION(bcmul)
261
281
scale = (int ) scale_param ;
262
282
}
263
283
284
+ BC_ARENA_SETUP ;
285
+
264
286
if (php_str2num (& first , left ) == FAILURE ) {
265
287
zend_argument_value_error (1 , "is not well-formed" );
266
288
goto cleanup ;
@@ -279,6 +301,7 @@ PHP_FUNCTION(bcmul)
279
301
bc_free_num (& first );
280
302
bc_free_num (& second );
281
303
bc_free_num (& result );
304
+ BC_ARENA_TEARDOWN ;
282
305
};
283
306
}
284
307
/* }}} */
@@ -308,6 +331,8 @@ PHP_FUNCTION(bcdiv)
308
331
scale = (int ) scale_param ;
309
332
}
310
333
334
+ BC_ARENA_SETUP ;
335
+
311
336
bc_init_num (& result );
312
337
313
338
if (php_str2num (& first , left ) == FAILURE ) {
@@ -331,6 +356,7 @@ PHP_FUNCTION(bcdiv)
331
356
bc_free_num (& first );
332
357
bc_free_num (& second );
333
358
bc_free_num (& result );
359
+ BC_ARENA_TEARDOWN ;
334
360
};
335
361
}
336
362
/* }}} */
@@ -360,6 +386,8 @@ PHP_FUNCTION(bcmod)
360
386
scale = (int ) scale_param ;
361
387
}
362
388
389
+ BC_ARENA_SETUP ;
390
+
363
391
bc_init_num (& result );
364
392
365
393
if (php_str2num (& first , left ) == FAILURE ) {
@@ -383,6 +411,7 @@ PHP_FUNCTION(bcmod)
383
411
bc_free_num (& first );
384
412
bc_free_num (& second );
385
413
bc_free_num (& result );
414
+ BC_ARENA_TEARDOWN ;
386
415
};
387
416
}
388
417
/* }}} */
@@ -413,6 +442,8 @@ PHP_FUNCTION(bcpowmod)
413
442
scale = (int ) scale_param ;
414
443
}
415
444
445
+ BC_ARENA_SETUP ;
446
+
416
447
bc_init_num (& result );
417
448
418
449
if (php_str2num (& bc_base , base_str ) == FAILURE ) {
@@ -458,6 +489,7 @@ PHP_FUNCTION(bcpowmod)
458
489
bc_free_num (& bc_expo );
459
490
bc_free_num (& bc_modulus );
460
491
bc_free_num (& result );
492
+ BC_ARENA_TEARDOWN ;
461
493
};
462
494
}
463
495
/* }}} */
@@ -487,6 +519,8 @@ PHP_FUNCTION(bcpow)
487
519
scale = (int ) scale_param ;
488
520
}
489
521
522
+ BC_ARENA_SETUP ;
523
+
490
524
bc_init_num (& result );
491
525
492
526
if (php_str2num (& first , base_str ) == FAILURE ) {
@@ -518,6 +552,7 @@ PHP_FUNCTION(bcpow)
518
552
bc_free_num (& first );
519
553
bc_free_num (& bc_exponent );
520
554
bc_free_num (& result );
555
+ BC_ARENA_TEARDOWN ;
521
556
};
522
557
}
523
558
/* }}} */
@@ -546,6 +581,8 @@ PHP_FUNCTION(bcsqrt)
546
581
scale = (int ) scale_param ;
547
582
}
548
583
584
+ BC_ARENA_SETUP ;
585
+
549
586
if (php_str2num (& result , left ) == FAILURE ) {
550
587
zend_argument_value_error (1 , "is not well-formed" );
551
588
goto cleanup ;
@@ -559,6 +596,7 @@ PHP_FUNCTION(bcsqrt)
559
596
560
597
cleanup : {
561
598
bc_free_num (& result );
599
+ BC_ARENA_TEARDOWN ;
562
600
};
563
601
}
564
602
/* }}} */
@@ -588,6 +626,8 @@ PHP_FUNCTION(bccomp)
588
626
scale = (int ) scale_param ;
589
627
}
590
628
629
+ BC_ARENA_SETUP ;
630
+
591
631
if (!bc_str2num (& first , ZSTR_VAL (left ), ZSTR_VAL (left ) + ZSTR_LEN (left ), scale , false)) {
592
632
zend_argument_value_error (1 , "is not well-formed" );
593
633
goto cleanup ;
@@ -603,6 +643,7 @@ PHP_FUNCTION(bccomp)
603
643
cleanup : {
604
644
bc_free_num (& first );
605
645
bc_free_num (& second );
646
+ BC_ARENA_TEARDOWN ;
606
647
};
607
648
}
608
649
/* }}} */
@@ -617,6 +658,8 @@ static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
617
658
Z_PARAM_STR (numstr )
618
659
ZEND_PARSE_PARAMETERS_END ();
619
660
661
+ BC_ARENA_SETUP ;
662
+
620
663
if (php_str2num (& num , numstr ) == FAILURE ) {
621
664
zend_argument_value_error (1 , "is not well-formed" );
622
665
goto cleanup ;
@@ -628,6 +671,7 @@ static void bcfloor_or_bcceil(INTERNAL_FUNCTION_PARAMETERS, bool is_floor)
628
671
cleanup : {
629
672
bc_free_num (& num );
630
673
bc_free_num (& result );
674
+ BC_ARENA_TEARDOWN ;
631
675
};
632
676
}
633
677
/* }}} */
@@ -676,6 +720,8 @@ PHP_FUNCTION(bcround)
676
720
return ;
677
721
}
678
722
723
+ BC_ARENA_SETUP ;
724
+
679
725
bc_init_num (& result );
680
726
681
727
if (php_str2num (& num , numstr ) == FAILURE ) {
@@ -689,6 +735,7 @@ PHP_FUNCTION(bcround)
689
735
cleanup : {
690
736
bc_free_num (& num );
691
737
bc_free_num (& result );
738
+ BC_ARENA_TEARDOWN ;
692
739
};
693
740
}
694
741
/* }}} */
0 commit comments