Skip to content

Commit be93f29

Browse files
committed
ext/gd: Reduce scope of variables
1 parent 9491c68 commit be93f29

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

Diff for: ext/gd/gd.c

+18-23
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,6 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, cons
17531753
char *file = NULL;
17541754
zend_long quality = 128, type = 1;
17551755
gdImagePtr im;
1756-
FILE *fp;
17571756
size_t file_len = 0;
17581757

17591758
/* The quality parameter for gd2 stands for chunk size */
@@ -1783,7 +1782,7 @@ static void _php_image_output(INTERNAL_FUNCTION_PARAMETERS, int image_type, cons
17831782
if (file_len) {
17841783
PHP_GD_CHECK_OPEN_BASEDIR(file, "Invalid filename");
17851784

1786-
fp = VCWD_FOPEN(file, "wb");
1785+
FILE *fp = VCWD_FOPEN(file, "wb");
17871786
if (!fp) {
17881787
php_error_docref(NULL, E_WARNING, "Unable to open \"%s\" for writing", file);
17891788
RETURN_FALSE;
@@ -1851,9 +1850,7 @@ PHP_FUNCTION(imagexbm)
18511850
zend_long foreground_color;
18521851
bool foreground_color_is_null = true;
18531852
gdImagePtr im;
1854-
int i;
18551853
gdIOCtx *ctx = NULL;
1856-
php_stream *stream;
18571854

18581855
ZEND_PARSE_PARAMETERS_START(2, 3)
18591856
Z_PARAM_OBJECT_OF_CLASS(imgind, gd_image_ce)
@@ -1865,7 +1862,7 @@ PHP_FUNCTION(imagexbm)
18651862
im = php_gd_libgdimageptr_from_zval_p(imgind);
18661863

18671864
if (file != NULL) {
1868-
stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
1865+
php_stream *stream = php_stream_open_wrapper(file, "wb", REPORT_ERRORS|IGNORE_PATH, NULL);
18691866
if (stream == NULL) {
18701867
RETURN_FALSE;
18711868
}
@@ -1876,6 +1873,7 @@ PHP_FUNCTION(imagexbm)
18761873
}
18771874

18781875
if (foreground_color_is_null) {
1876+
int i;
18791877
for (i = 0; i < gdImageColorsTotal(im); i++) {
18801878
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
18811879
break;
@@ -2083,7 +2081,6 @@ PHP_FUNCTION(imagewbmp)
20832081
zend_long foreground_color;
20842082
bool foreground_color_is_null = true;
20852083
gdImagePtr im;
2086-
int i;
20872084
gdIOCtx *ctx;
20882085
zval *to_zval = NULL;
20892086

@@ -2102,6 +2099,7 @@ PHP_FUNCTION(imagewbmp)
21022099
}
21032100

21042101
if (foreground_color_is_null) {
2102+
int i;
21052103
for (i = 0; i < gdImageColorsTotal(im); i++) {
21062104
if (!gdImageRed(im, i) && !gdImageGreen(im, i) && !gdImageBlue(im, i)) {
21072105
break;
@@ -2462,7 +2460,6 @@ PHP_FUNCTION(imagegammacorrect)
24622460
{
24632461
zval *IM;
24642462
gdImagePtr im;
2465-
int i;
24662463
double input, output, gamma;
24672464

24682465
ZEND_PARSE_PARAMETERS_START(3, 3)
@@ -2486,11 +2483,9 @@ PHP_FUNCTION(imagegammacorrect)
24862483
im = php_gd_libgdimageptr_from_zval_p(IM);
24872484

24882485
if (gdImageTrueColor(im)) {
2489-
int x, y, c;
2490-
2491-
for (y = 0; y < gdImageSY(im); y++) {
2492-
for (x = 0; x < gdImageSX(im); x++) {
2493-
c = gdImageGetPixel(im, x, y);
2486+
for (int y = 0; y < gdImageSY(im); y++) {
2487+
for (int x = 0; x < gdImageSX(im); x++) {
2488+
int c = gdImageGetPixel(im, x, y);
24942489
gdImageSetPixel(im, x, y,
24952490
gdTrueColorAlpha(
24962491
(int) ((pow((gdTrueColorGetRed(c) / 255.0), gamma) * 255) + .5),
@@ -2504,7 +2499,7 @@ PHP_FUNCTION(imagegammacorrect)
25042499
RETURN_TRUE;
25052500
}
25062501

2507-
for (i = 0; i < gdImageColorsTotal(im); i++) {
2502+
for (int i = 0; i < gdImageColorsTotal(im); i++) {
25082503
im->red[i] = (int)((pow((im->red[i] / 255.0), gamma) * 255) + .5);
25092504
im->green[i] = (int)((pow((im->green[i] / 255.0), gamma) * 255) + .5);
25102505
im->blue[i] = (int)((pow((im->blue[i] / 255.0), gamma) * 255) + .5);
@@ -2810,7 +2805,7 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
28102805
zval *var = NULL;
28112806
gdImagePtr im;
28122807
gdPointPtr points;
2813-
int npoints, col, nelem, i;
2808+
int npoints, col, nelem;
28142809

28152810
ZEND_PARSE_PARAMETERS_START(3, 4)
28162811
Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
@@ -2850,7 +2845,7 @@ static void php_imagepolygon(INTERNAL_FUNCTION_PARAMETERS, int filled)
28502845

28512846
points = (gdPointPtr) safe_emalloc(npoints, sizeof(gdPoint), 0);
28522847

2853-
for (i = 0; i < npoints; i++) {
2848+
for (int i = 0; i < npoints; i++) {
28542849
if ((var = zend_hash_index_find(Z_ARRVAL_P(POINTS), (i * 2))) != NULL) {
28552850
points[i].x = zval_get_long(var);
28562851
}
@@ -3712,7 +3707,7 @@ PHP_FUNCTION(imageconvolution)
37123707
zval *var = NULL, *var2 = NULL;
37133708
gdImagePtr im_src = NULL;
37143709
double div, offset;
3715-
int i, j, res;
3710+
int res;
37163711
float matrix[3][3] = {{0,0,0}, {0,0,0}, {0,0,0}};
37173712

37183713
ZEND_PARSE_PARAMETERS_START(4, 4)
@@ -3729,14 +3724,14 @@ PHP_FUNCTION(imageconvolution)
37293724
RETURN_THROWS();
37303725
}
37313726

3732-
for (i=0; i<3; i++) {
3727+
for (uint8_t i = 0; i < 3; i++) {
37333728
if ((var = zend_hash_index_find_deref(Z_ARRVAL_P(hash_matrix), (i))) != NULL && Z_TYPE_P(var) == IS_ARRAY) {
37343729
if (zend_hash_num_elements(Z_ARRVAL_P(var)) != 3 ) {
37353730
zend_argument_value_error(2, "must be a 3x3 array, matrix[%d] only has %d elements", i, zend_hash_num_elements(Z_ARRVAL_P(var)));
37363731
RETURN_THROWS();
37373732
}
37383733

3739-
for (j=0; j<3; j++) {
3734+
for (uint8_t j = 0; j < 3; j++) {
37403735
if ((var2 = zend_hash_index_find(Z_ARRVAL_P(var), j)) != NULL) {
37413736
matrix[i][j] = (float) zval_get_double(var2);
37423737
} else {
@@ -4042,9 +4037,7 @@ PHP_FUNCTION(imageaffine)
40424037
gdRectPtr pRect = NULL;
40434038
zval *z_rect = NULL;
40444039
zval *z_affine;
4045-
zval *tmp;
40464040
double affine[6];
4047-
zval *zval_affine_elem = NULL;
40484041

40494042
ZEND_PARSE_PARAMETERS_START(2, 3)
40504043
Z_PARAM_OBJECT_OF_CLASS(IM, gd_image_ce)
@@ -4062,7 +4055,8 @@ PHP_FUNCTION(imageaffine)
40624055
}
40634056

40644057
for (uint32_t i = 0; i < nelems; i++) {
4065-
if ((zval_affine_elem = zend_hash_index_find_deref(Z_ARRVAL_P(z_affine), i)) != NULL) {
4058+
zval *zval_affine_elem = zend_hash_index_find_deref(Z_ARRVAL_P(z_affine), i);
4059+
if (zval_affine_elem != NULL) {
40664060
switch (Z_TYPE_P(zval_affine_elem)) {
40674061
case IS_LONG:
40684062
case IS_DOUBLE:
@@ -4081,6 +4075,7 @@ PHP_FUNCTION(imageaffine)
40814075
}
40824076

40834077
if (z_rect != NULL) {
4078+
zval *tmp;
40844079
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(z_rect), "x", sizeof("x") - 1)) != NULL) {
40854080
rect.x = zval_get_long(tmp);
40864081
} else {
@@ -4130,7 +4125,7 @@ PHP_FUNCTION(imageaffinematrixget)
41304125
zend_long type;
41314126
zval *options = NULL;
41324127
zval *tmp;
4133-
int res = GD_FALSE, i;
4128+
int res = GD_FALSE;
41344129

41354130
ZEND_PARSE_PARAMETERS_START(2, 2)
41364131
Z_PARAM_LONG(type)
@@ -4194,7 +4189,7 @@ PHP_FUNCTION(imageaffinematrixget)
41944189
RETURN_FALSE;
41954190
} else {
41964191
array_init(return_value);
4197-
for (i = 0; i < 6; i++) {
4192+
for (uint8_t i = 0; i < 6; i++) {
41984193
add_index_double(return_value, i, affine[i]);
41994194
}
42004195
}

0 commit comments

Comments
 (0)