Skip to content

Commit f8a4dfe

Browse files
committed
Use size_t as a more appropriate type
1 parent ab935b7 commit f8a4dfe

File tree

19 files changed

+94
-81
lines changed

19 files changed

+94
-81
lines changed

ext/bcmath/libbcmath/src/add.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,19 @@
3131

3232
#include "bcmath.h"
3333
#include "private.h"
34+
#include <stddef.h>
3435
#include <string.h>
3536

3637

3738
/* Here is the full add routine that takes care of negative numbers.
3839
N1 is added to N2 and the result placed into RESULT. SCALE_MIN
3940
is the minimum scale for the result. */
4041

41-
void bc_add (bc_num n1, bc_num n2, bc_num *result, int scale_min)
42+
void bc_add (bc_num n1, bc_num n2, bc_num *result, size_t scale_min)
4243
{
4344
bc_num sum = NULL;
4445
int cmp_res;
45-
int res_scale;
46+
size_t res_scale;
4647

4748
if (n1->n_sign == n2->n_sign) {
4849
sum = _bc_do_add (n1, n2, scale_min);

ext/bcmath/libbcmath/src/bcmath.h

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,21 @@
3232
#ifndef _BCMATH_H_
3333
#define _BCMATH_H_
3434

35+
#include <stddef.h>
36+
3537
typedef enum {PLUS, MINUS} sign;
3638

3739
typedef struct bc_struct *bc_num;
3840

3941
typedef struct bc_struct {
40-
sign n_sign;
41-
int n_len; /* The number of digits before the decimal point. */
42-
int n_scale; /* The number of digits after the decimal point. */
43-
int n_refs; /* The number of pointers to this number. */
44-
char *n_ptr; /* The pointer to the actual storage.
42+
sign n_sign;
43+
size_t n_len; /* The number of digits before the decimal point. */
44+
size_t n_scale; /* The number of digits after the decimal point. */
45+
int n_refs; /* The number of pointers to this number. */
46+
char *n_ptr; /* The pointer to the actual storage.
4547
If NULL, n_value points to the inside of another number
4648
(bc_multiply...) and should not be "freed." */
47-
char *n_value; /* The number. Not zero char terminated.
49+
char *n_value; /* The number. Not zero char terminated.
4850
May not point to the same place as n_ptr as
4951
in the case of leading zeros generated. */
5052
} bc_struct;
@@ -84,17 +86,17 @@ typedef struct bc_struct {
8486

8587
void bc_init_numbers(void);
8688

87-
bc_num _bc_new_num_ex(int length, int scale, bool persistent);
89+
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent);
8890

8991
void _bc_free_num_ex(bc_num *num, bool persistent);
9092

9193
bc_num bc_copy_num(bc_num num);
9294

9395
void bc_init_num(bc_num *num);
9496

95-
bool bc_str2num(bc_num *num, char *str, int scale);
97+
bool bc_str2num(bc_num *num, char *str, size_t scale);
9698

97-
zend_string *bc_num2str_ex(bc_num num, int scale);
99+
zend_string *bc_num2str_ex(bc_num num, size_t scale);
98100

99101
void bc_int2num(bc_num *num, int val);
100102

@@ -104,29 +106,29 @@ int bc_compare(bc_num n1, bc_num n2);
104106

105107
bool bc_is_zero(bc_num num);
106108

107-
bool bc_is_zero_for_scale(bc_num num, int scale);
109+
bool bc_is_zero_for_scale(bc_num num, size_t scale);
108110

109-
bool bc_is_near_zero(bc_num num, int scale);
111+
bool bc_is_near_zero(bc_num num, size_t scale);
110112

111113
bool bc_is_neg(bc_num num);
112114

113-
void bc_add(bc_num n1, bc_num n2, bc_num *result, int scale_min);
115+
void bc_add(bc_num n1, bc_num n2, bc_num *result, size_t scale_min);
114116

115-
void bc_sub(bc_num n1, bc_num n2, bc_num *result, int scale_min);
117+
void bc_sub(bc_num n1, bc_num n2, bc_num *result, size_t scale_min);
116118

117-
void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, int scale);
119+
void bc_multiply(bc_num n1, bc_num n2, bc_num *prod, size_t scale);
118120

119121
int bc_divide(bc_num n1, bc_num n2, bc_num *quot, int scale);
120122

121-
int bc_modulo(bc_num num1, bc_num num2, bc_num *resul, int scale);
123+
int bc_modulo(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
122124

123-
int bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, int scale);
125+
int bc_divmod(bc_num num1, bc_num num2, bc_num *quo, bc_num *rem, size_t scale);
124126

125-
int bc_raisemod(bc_num base, bc_num expo, bc_num mo, bc_num *result, int scale);
127+
int bc_raisemod(bc_num base, bc_num expo, bc_num mo, bc_num *result, size_t scale);
126128

127-
void bc_raise(bc_num num1, bc_num num2, bc_num *resul, int scale);
129+
void bc_raise(bc_num num1, bc_num num2, bc_num *resul, size_t scale);
128130

129-
bool bc_sqrt(bc_num *num, int scale);
131+
bool bc_sqrt(bc_num *num, size_t scale);
130132

131133
void bc_out_num(bc_num num, int o_base, void (* out_char)(char), bool leading_zero);
132134

ext/bcmath/libbcmath/src/compare.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*************************************************************************/
3131

3232
#include <stdbool.h>
33+
#include <stddef.h>
3334
#include "bcmath.h"
3435
#include "private.h"
3536

@@ -41,7 +42,6 @@
4142
int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
4243
{
4344
char *n1ptr, *n2ptr;
44-
int count;
4545

4646
/* First, compare signs. */
4747
if (use_sign && n1->n_sign != n2->n_sign) {
@@ -73,7 +73,7 @@ int _bc_do_compare(bc_num n1, bc_num n2, bool use_sign, bool ignore_last)
7373

7474
/* If we get here, they have the same number of integer digits.
7575
check the integer part and the equal length part of the fraction. */
76-
count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
76+
size_t count = n1->n_len + MIN (n1->n_scale, n2->n_scale);
7777
n1ptr = n1->n_value;
7878
n2ptr = n2->n_value;
7979

ext/bcmath/libbcmath/src/debug.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <stdio.h>
3333
#include "bcmath.h"
3434
#include "private.h"
35+
#include <stddef.h>
3536

3637
/* pn prints the number NUM in base 10. */
3738

@@ -49,11 +50,10 @@ void pn(bc_num num)
4950

5051

5152
/* pv prints a character array as if it was a string of bcd digits. */
52-
void pv (char *name, unsigned char *num, int len)
53+
void pv (char *name, unsigned char *num, size_t len)
5354
{
54-
int i;
5555
printf("%s=", name);
56-
for (i=0; i<len; i++){
56+
for (size_t i = 0; i < len; i++){
5757
printf ("%c",BCD_CHAR(num[i]));
5858
}
5959
printf("\n");

ext/bcmath/libbcmath/src/div.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "bcmath.h"
3333
#include "private.h"
3434
#include <stdbool.h>
35+
#include <stddef.h>
3536
#include "zend_alloc.h"
3637

3738

@@ -40,7 +41,7 @@
4041
placed into RESULT. It is written so that NUM and RESULT can be
4142
the same pointers. */
4243

43-
static void _one_mult(unsigned char *num, int size, int digit, unsigned char *result)
44+
static void _one_mult(unsigned char *num, size_t size, int digit, unsigned char *result)
4445
{
4546
int carry, value;
4647
unsigned char *nptr, *rptr;

ext/bcmath/libbcmath/src/divmod.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,19 @@
3030
*************************************************************************/
3131

3232
#include "bcmath.h"
33+
#include <stddef.h>
3334

3435

3536
/* Division *and* modulo for numbers. This computes both NUM1 / NUM2 and
3637
NUM1 % NUM2 and puts the results in QUOT and REM, except that if QUOT
3738
is NULL then that store will be omitted.
3839
*/
3940

40-
int bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, int scale)
41+
int bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, size_t scale)
4142
{
4243
bc_num quotient = NULL;
4344
bc_num temp;
44-
int rscale;
45+
size_t rscale;
4546

4647
/* Check for correct numbers. */
4748
if (bc_is_zero (num2)) {
@@ -73,7 +74,7 @@ int bc_divmod(bc_num num1, bc_num num2, bc_num *quot, bc_num *rem, int scale)
7374
/* Modulo for numbers. This computes NUM1 % NUM2 and puts the
7475
result in RESULT. */
7576

76-
int bc_modulo(bc_num num1, bc_num num2, bc_num *result, int scale)
77+
int bc_modulo(bc_num num1, bc_num num2, bc_num *result, size_t scale)
7778
{
7879
return bc_divmod(num1, num2, NULL, result, scale);
7980
}

ext/bcmath/libbcmath/src/doaddsub.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,20 @@
3131

3232
#include "bcmath.h"
3333
#include "private.h"
34+
#include <stddef.h>
3435

3536

3637
/* Perform addition: N1 is added to N2 and the value is
3738
returned. The signs of N1 and N2 are ignored.
3839
SCALE_MIN is to set the minimum scale of the result. */
3940

40-
bc_num _bc_do_add(bc_num n1, bc_num n2, int scale_min)
41+
bc_num _bc_do_add(bc_num n1, bc_num n2, size_t scale_min)
4142
{
4243
bc_num sum;
43-
int sum_scale, sum_digits;
44+
size_t sum_scale, sum_digits;
4445
char *n1ptr, *n2ptr, *sumptr;
45-
int carry, n1bytes, n2bytes;
46-
int count;
46+
size_t n1bytes, n2bytes;
47+
bool carry;
4748

4849
/* Prepare sum. */
4950
sum_scale = MAX (n1->n_scale, n2->n_scale);
@@ -53,7 +54,7 @@ bc_num _bc_do_add(bc_num n1, bc_num n2, int scale_min)
5354
/* Zero extra digits made by scale_min. */
5455
if (scale_min > sum_scale) {
5556
sumptr = (char *) (sum->n_value + sum_scale + sum_digits);
56-
for (count = scale_min - sum_scale; count > 0; count--) {
57+
for (int count = scale_min - sum_scale; count > 0; count--) {
5758
*sumptr++ = 0;
5859
}
5960
}
@@ -105,16 +106,16 @@ bc_num _bc_do_add(bc_num n1, bc_num n2, int scale_min)
105106
while (n1bytes-- > 0) {
106107
*sumptr = *n1ptr-- + carry;
107108
if (*sumptr > (BASE-1)) {
108-
carry = 1;
109+
carry = true;
109110
*sumptr -= BASE;
110111
} else {
111-
carry = 0;
112+
carry = false;
112113
}
113114
sumptr--;
114115
}
115116

116117
/* Set final carry. */
117-
if (carry == 1) {
118+
if (carry) {
118119
*sumptr += 1;
119120
}
120121

@@ -128,11 +129,11 @@ bc_num _bc_do_add(bc_num n1, bc_num n2, int scale_min)
128129
returned. The signs of N1 and N2 are ignored. Also, N1 is
129130
assumed to be larger than N2. SCALE_MIN is the minimum scale
130131
of the result. */
131-
bc_num _bc_do_sub(bc_num n1, bc_num n2, int scale_min)
132+
bc_num _bc_do_sub(bc_num n1, bc_num n2, size_t scale_min)
132133
{
133134
bc_num diff;
134135
int diff_scale, diff_len;
135-
int min_scale, min_len;
136+
size_t min_scale, min_len;
136137
char *n1ptr, *n2ptr, *diffptr;
137138
int borrow, count, val;
138139

ext/bcmath/libbcmath/src/init.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
#include "bcmath.h"
3333
#include <stdbool.h>
34+
#include <stddef.h>
3435
#include "zend_alloc.h"
3536

3637
/* new_num allocates a number and sets fields to known values. */
37-
bc_num _bc_new_num_ex(int length, int scale, bool persistent)
38+
bc_num _bc_new_num_ex(size_t length, size_t scale, bool persistent)
3839
{
3940
bc_num temp;
4041
/* PHP Change: malloc() -> pemalloc(), removed free_list code */

ext/bcmath/libbcmath/src/nearzero.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,23 @@
3030
*************************************************************************/
3131

3232
#include <stdbool.h>
33+
#include <stddef.h>
3334
#include "bcmath.h"
3435

3536
/* In some places we need to check if the number NUM is almost zero.
3637
Specifically, all but the last digit is 0 and the last digit is 1.
3738
Last digit is defined by scale. */
3839

39-
bool bc_is_near_zero(bc_num num, int scale)
40+
bool bc_is_near_zero(bc_num num, size_t scale)
4041
{
41-
int count;
42-
char *nptr;
43-
4442
/* Error checking */
4543
if (scale > num->n_scale) {
4644
scale = num->n_scale;
4745
}
4846

4947
/* Initialize */
50-
count = num->n_len + scale;
51-
nptr = num->n_value;
48+
size_t count = num->n_len + scale;
49+
const char *nptr = num->n_value;
5250

5351
/* The check */
5452
while ((count > 0) && (*nptr++ == 0)) {

ext/bcmath/libbcmath/src/num2long.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
*************************************************************************/
3131

3232
#include "bcmath.h"
33+
#include <stddef.h>
3334

3435
/* Convert a number NUM to a long. The function returns only the integer
3536
part of the number. For numbers that are too large to represent as
@@ -40,12 +41,11 @@ long bc_num2long(bc_num num)
4041
{
4142
long val;
4243
char *nptr;
43-
int index;
4444

4545
/* Extract the int value, ignore the fraction. */
4646
val = 0;
4747
nptr = num->n_value;
48-
for (index = num->n_len; index > 0; index--) {
48+
for (size_t index = num->n_len; index > 0; index--) {
4949
char n = *nptr++;
5050

5151
if (val > LONG_MAX/BASE) {

ext/bcmath/libbcmath/src/num2str.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,11 @@
3030
*************************************************************************/
3131

3232
#include "bcmath.h"
33+
#include <stddef.h>
3334
#include "zend_string.h"
3435

3536
/* Convert a numbers to a string. Base 10 only.*/
36-
zend_string *bc_num2str_ex(bc_num num, int scale)
37+
zend_string *bc_num2str_ex(bc_num num, size_t scale)
3738
{
3839
zend_string *str;
3940
char *sptr;

ext/bcmath/libbcmath/src/private.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@
3232
/* "Private" routines to bcmath. */
3333

3434
#include <stdbool.h>
35+
#include <stddef.h>
3536

3637
/* routines */
3738
int _bc_do_compare (bc_num n1, bc_num n2, bool use_sign, bool ignore_last);
38-
bc_num _bc_do_add (bc_num n1, bc_num n2, int scale_min);
39-
bc_num _bc_do_sub (bc_num n1, bc_num n2, int scale_min);
39+
bc_num _bc_do_add (bc_num n1, bc_num n2, size_t scale_min);
40+
bc_num _bc_do_sub (bc_num n1, bc_num n2, size_t scale_min);
4041
void _bc_rm_leading_zeros (bc_num num);

ext/bcmath/libbcmath/src/raise.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,21 @@
3131

3232
#include "bcmath.h"
3333
#include <stdbool.h>
34+
#include <stddef.h>
3435
#include "zend_exceptions.h" /* zend_argument_value_error() */
3536

3637

3738
/* Raise NUM1 to the NUM2 power. The result is placed in RESULT.
3839
Maximum exponent is LONG_MAX. If a NUM2 is not an integer,
3940
only the integer part is used. */
4041

41-
void bc_raise(bc_num num1, bc_num num2, bc_num *result, int scale)
42+
void bc_raise(bc_num num1, bc_num num2, bc_num *result, size_t scale)
4243
{
4344
bc_num temp, power;
4445
long exponent;
45-
int rscale;
46-
int pwrscale;
47-
int calcscale;
46+
size_t rscale;
47+
size_t pwrscale;
48+
size_t calcscale;
4849
bool is_neg;
4950

5051
/* Check the exponent for scale digits and convert to a long. */

0 commit comments

Comments
 (0)