Skip to content

Commit aa92c70

Browse files
committed
ext/exif: Reduce scope of variable
1 parent 64569d3 commit aa92c70

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

ext/exif/exif.c

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,10 +2158,8 @@ static int exif_file_sections_realloc(image_info_type *ImageInfo, int section_in
21582158
*/
21592159
static void exif_file_sections_free(image_info_type *ImageInfo)
21602160
{
2161-
int i;
2162-
21632161
if (ImageInfo->file.count) {
2164-
for (i=0; i<ImageInfo->file.count; i++) {
2162+
for (int i = 0; i<ImageInfo->file.count; i++) {
21652163
EFREE_IF(ImageInfo->file.list[i].data);
21662164
}
21672165
}
@@ -2403,11 +2401,10 @@ static void exif_iif_add_buffer(image_info_type *image_info, int section_index,
24032401
Free memory allocated for image_info
24042402
*/
24052403
static void exif_iif_free(image_info_type *image_info, int section_index) {
2406-
int i;
24072404
void *f; /* faster */
24082405

24092406
if (image_info->info_list[section_index].count) {
2410-
for (i=0; i < image_info->info_list[section_index].count; i++) {
2407+
for (int i = 0; i < image_info->info_list[section_index].count; i++) {
24112408
if ((f=image_info->info_list[section_index].list[i].name) != NULL) {
24122409
efree(f);
24132410
}
@@ -2448,7 +2445,6 @@ static void exif_iif_free(image_info_type *image_info, int section_index) {
24482445
* Add image_info to associative array value. */
24492446
static void add_assoc_image_info(zval *value, int sub_array, image_info_type *image_info, int section_index)
24502447
{
2451-
char buffer[64], uname[64];
24522448
int idx = 0, unknown = 0;
24532449

24542450
if (!image_info->info_list[section_index].count) {
@@ -2466,6 +2462,8 @@ static void add_assoc_image_info(zval *value, int sub_array, image_info_type *im
24662462
image_info_data *info_data = &image_info->info_list[section_index].list[i];
24672463
image_info_value *info_value = &info_data->value;
24682464
const char *name = info_data->name;
2465+
char uname[64];
2466+
24692467
if (!name) {
24702468
snprintf(uname, sizeof(uname), "%d", unknown++);
24712469
name = uname;
@@ -2516,6 +2514,7 @@ static void add_assoc_image_info(zval *value, int sub_array, image_info_type *im
25162514
array_init(&array);
25172515
}
25182516
for (int ap = 0; ap < l; ap++) {
2517+
char buffer[64];
25192518
if (l>1) {
25202519
info_value = &info_data->value.list[ap];
25212520
}
@@ -2785,8 +2784,7 @@ PHP_FUNCTION(exif_tagname)
27852784
* Create a value for an ifd from an info_data pointer */
27862785
static void* exif_ifd_make_value(image_info_data *info_data, int motorola_intel) {
27872786
size_t byte_count;
2788-
char *value_ptr, *data_ptr;
2789-
size_t i;
2787+
char *value_ptr;
27902788

27912789
image_info_value *info_value;
27922790

@@ -2808,8 +2806,8 @@ static void* exif_ifd_make_value(image_info_data *info_data, int motorola_intel)
28082806
*value_ptr = info_data->value.i;
28092807
return value_ptr;
28102808
} else {
2811-
data_ptr = value_ptr;
2812-
for(i=0; i<info_data->length; i++) {
2809+
char *data_ptr = value_ptr;
2810+
for(size_t i = 0; i < info_data->length; i++) {
28132811
if (info_data->length==1) {
28142812
info_value = &info_data->value;
28152813
} else {
@@ -3032,15 +3030,14 @@ static int exif_process_string(char **result, const char *value, size_t byte_cou
30323030
* Process UserComment in IFD. */
30333031
static int exif_process_user_comment(const image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount)
30343032
{
3035-
int a;
3036-
char *decode;
30373033
size_t len;
30383034

30393035
*pszEncoding = NULL;
30403036
/* Copy the comment */
30413037
if (ByteCount>=8) {
30423038
const zend_encoding *from, *to;
30433039
if (!memcmp(szValuePtr, "UNICODE\0", 8)) {
3040+
char *decode;
30443041
*pszEncoding = estrdup(szValuePtr);
30453042
szValuePtr = szValuePtr+8;
30463043
ByteCount -= 8;
@@ -3105,7 +3102,7 @@ static int exif_process_user_comment(const image_info_type *ImageInfo, char **ps
31053102

31063103
/* Olympus has this padded with trailing spaces. Remove these first. */
31073104
if (ByteCount>0) {
3108-
for (a=ByteCount-1;a && szValuePtr[a]==' ';a--) {
3105+
for (int a = ByteCount-1; a && szValuePtr[a]==' '; a--) {
31093106
(szValuePtr)[a] = '\0';
31103107
}
31113108
}
@@ -3141,14 +3138,13 @@ static int exif_process_unicode(const image_info_type *ImageInfo, xp_field_type
31413138
* Process nested IFDs directories in Maker Note. */
31423139
static bool exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * value_ptr, int value_len, const exif_offset_info *info, size_t displacement)
31433140
{
3144-
size_t i;
3145-
int de, section_index = SECTION_MAKERNOTE;
3141+
int section_index = SECTION_MAKERNOTE;
31463142
int NumDirEntries, old_motorola_intel;
31473143
const maker_note_type *maker_note;
31483144
char *dir_start;
31493145
exif_offset_info new_info;
31503146

3151-
for (i=0; i<=sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
3147+
for (size_t i = 0; i <= sizeof(maker_note_array)/sizeof(maker_note_type); i++) {
31523148
if (i==sizeof(maker_note_array)/sizeof(maker_note_type)) {
31533149
#ifdef EXIF_DEBUG
31543150
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "No maker note data found. Detected maker: %s (length = %d)", ImageInfo->make, ImageInfo->make ? strlen(ImageInfo->make) : 0);
@@ -3229,7 +3225,7 @@ static bool exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * val
32293225
break;
32303226
}
32313227

3232-
for (de=0;de<NumDirEntries;de++) {
3228+
for (int de = 0; de < NumDirEntries; de++) {
32333229
size_t offset = 2 + 12 * de;
32343230
if (!exif_process_IFD_TAG(ImageInfo, dir_start + offset,
32353231
info, displacement, section_index, 0, maker_note->tag_table)) {
@@ -3257,16 +3253,10 @@ static bool exif_process_IFD_in_MAKERNOTE(image_info_type *ImageInfo, char * val
32573253
* Process one of the nested IFDs directories. */
32583254
static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entry, const exif_offset_info *info, size_t displacement, int section_index, int ReadNextIFD, tag_table_type tag_table)
32593255
{
3260-
size_t length;
32613256
unsigned int tag, format, components;
32623257
char *value_ptr, tagname[64], cbuf[32], *outside=NULL;
3263-
size_t byte_count, offset_val, fpos, fgot;
3258+
size_t byte_count, offset_val;
32643259
int64_t byte_count_signed;
3265-
xp_field_type *tmp_xp;
3266-
#ifdef EXIF_DEBUG
3267-
char *dump_data;
3268-
int dump_free;
3269-
#endif /* EXIF_DEBUG */
32703260

32713261
tag = php_ifd_get16u(dir_entry, ImageInfo->motorola_intel);
32723262
format = php_ifd_get16u(dir_entry+2, ImageInfo->motorola_intel);
@@ -3314,9 +3304,9 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
33143304
value_ptr = cbuf;
33153305
}
33163306

3317-
fpos = php_stream_tell(ImageInfo->infile);
3307+
size_t fpos = php_stream_tell(ImageInfo->infile);
33183308
php_stream_seek(ImageInfo->infile, displacement+offset_val, SEEK_SET);
3319-
fgot = php_stream_tell(ImageInfo->infile);
3309+
size_t fgot = php_stream_tell(ImageInfo->infile);
33203310
if (fgot!=displacement+offset_val) {
33213311
EFREE_IF(outside);
33223312
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_WARNING, "Wrong file pointer: 0x%08X != 0x%08X", fgot, displacement+offset_val);
@@ -3339,7 +3329,8 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
33393329

33403330
ImageInfo->sections_found |= FOUND_ANY_TAG;
33413331
#ifdef EXIF_DEBUG
3342-
dump_data = exif_dump_data(&dump_free, format, components, ImageInfo->motorola_intel, value_ptr);
3332+
int dump_free;
3333+
char *dump_data = exif_dump_data(&dump_free, format, components, ImageInfo->motorola_intel, value_ptr);
33433334
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE,
33443335
"Process tag(x%04X=%s,@0x%04X + x%04X(=%d)): %s%s %s",
33453336
tag, exif_get_tagname_debug(tag, tag_table), offset_val+displacement, byte_count, byte_count, (components>1)&&format!=TAG_FMT_UNDEFINED&&format!=TAG_FMT_STRING?"ARRAY OF ":"", exif_get_tagformat(format), dump_data);
@@ -3392,7 +3383,8 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
33923383
} else {
33933384
if (section_index==SECTION_IFD0 || section_index==SECTION_EXIF)
33943385
switch(tag) {
3395-
case TAG_COPYRIGHT:
3386+
case TAG_COPYRIGHT: {
3387+
size_t length;
33963388
/* check for "<photographer> NUL <editor> NUL" */
33973389
if (byte_count>1 && (length=zend_strnlen(value_ptr, byte_count)) > 0) {
33983390
if (length<byte_count-1) {
@@ -3412,6 +3404,7 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
34123404
}
34133405
}
34143406
break;
3407+
}
34153408

34163409
case TAG_USERCOMMENT:
34173410
EFREE_IF(ImageInfo->UserComment);
@@ -3425,13 +3418,14 @@ static bool exif_process_IFD_TAG_impl(image_info_type *ImageInfo, char *dir_entr
34253418
case TAG_XP_COMMENTS:
34263419
case TAG_XP_AUTHOR:
34273420
case TAG_XP_KEYWORDS:
3428-
case TAG_XP_SUBJECT:
3429-
tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
3421+
case TAG_XP_SUBJECT: {
3422+
xp_field_type *tmp_xp = (xp_field_type*)safe_erealloc(ImageInfo->xp_fields.list, (ImageInfo->xp_fields.count+1), sizeof(xp_field_type), 0);
34303423
ImageInfo->sections_found |= FOUND_WINXP;
34313424
ImageInfo->xp_fields.list = tmp_xp;
34323425
ImageInfo->xp_fields.count++;
34333426
exif_process_unicode(ImageInfo, &(ImageInfo->xp_fields.list[ImageInfo->xp_fields.count-1]), tag, value_ptr, byte_count);
34343427
break;
3428+
}
34353429

34363430
case TAG_FNUMBER:
34373431
/* Simplest way of expressing aperture, so I trust it the most.
@@ -4032,7 +4026,7 @@ static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir
40324026
{
40334027
int i, sn, num_entries, sub_section_index = 0;
40344028
unsigned char *dir_entry;
4035-
size_t ifd_size, dir_size, entry_offset, next_offset, entry_length, entry_value=0, fgot;
4029+
size_t ifd_size, dir_size, next_offset, entry_length, entry_value=0;
40364030
int entry_tag , entry_type;
40374031
tag_table_type tag_table = exif_get_tag_table(section_index);
40384032

@@ -4119,7 +4113,7 @@ static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir
41194113
break;
41204114
}
41214115
} else {
4122-
entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4116+
size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
41234117
/* if entry needs expanding ifd cache and entry is at end of current ifd cache. */
41244118
/* otherwise there may be huge holes between two entries */
41254119
if (entry_offset + entry_length > dir_offset + ifd_size
@@ -4178,7 +4172,7 @@ static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir
41784172
sub_section_index = SECTION_THUMBNAIL;
41794173
break;
41804174
}
4181-
entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
4175+
size_t entry_offset = php_ifd_get32u(dir_entry+8, ImageInfo->motorola_intel);
41824176
#ifdef EXIF_DEBUG
41834177
exif_error_docref(NULL EXIFERR_CC, ImageInfo, E_NOTICE, "Next IFD: %s @0x%04X", exif_get_sectionname(sub_section_index), entry_offset);
41844178
#endif
@@ -4195,7 +4189,7 @@ static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir
41954189
if (!ImageInfo->Thumbnail.data) {
41964190
ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
41974191
php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4198-
fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4192+
size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
41994193
if (fgot != ImageInfo->Thumbnail.size) {
42004194
EXIF_ERRLOG_THUMBEOF(ImageInfo)
42014195
efree(ImageInfo->Thumbnail.data);
@@ -4235,7 +4229,7 @@ static bool exif_process_IFD_in_TIFF_impl(image_info_type *ImageInfo, size_t dir
42354229
if (!ImageInfo->Thumbnail.data && ImageInfo->Thumbnail.offset && ImageInfo->Thumbnail.size && ImageInfo->read_thumbnail) {
42364230
ImageInfo->Thumbnail.data = safe_emalloc(ImageInfo->Thumbnail.size, 1, 0);
42374231
php_stream_seek(ImageInfo->infile, ImageInfo->Thumbnail.offset, SEEK_SET);
4238-
fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
4232+
size_t fgot = exif_read_from_stream_file_looped(ImageInfo->infile, ImageInfo->Thumbnail.data, ImageInfo->Thumbnail.size);
42394233
if (fgot != ImageInfo->Thumbnail.size) {
42404234
EXIF_ERRLOG_THUMBEOF(ImageInfo)
42414235
efree(ImageInfo->Thumbnail.data);
@@ -4494,7 +4488,7 @@ PHP_FUNCTION(exif_read_data)
44944488
bool ret;
44954489
int i, sections_needed = 0;
44964490
image_info_type ImageInfo;
4497-
char tmp[64], *sections_str, *s;
4491+
char *sections_str;
44984492

44994493
/* Parse arguments */
45004494
ZEND_PARSE_PARAMETERS_START(1, 4)
@@ -4510,14 +4504,15 @@ PHP_FUNCTION(exif_read_data)
45104504
if (z_sections_needed) {
45114505
spprintf(&sections_str, 0, ",%s,", ZSTR_VAL(z_sections_needed));
45124506
/* sections_str DOES start with , and SPACES are NOT allowed in names */
4513-
s = sections_str;
4507+
char *s = sections_str;
45144508
while (*++s) {
45154509
if (*s == ' ') {
45164510
*s = ',';
45174511
}
45184512
}
45194513

45204514
for (i = 0; i < SECTION_COUNT; i++) {
4515+
char tmp[64];
45214516
snprintf(tmp, sizeof(tmp), ",%s,", exif_get_sectionname(i));
45224517
if (strstr(sections_str, tmp)) {
45234518
sections_needed |= 1<<i;

0 commit comments

Comments
 (0)