libcupsfilters: Silenced warnings and added error handling when reading EXIF
The code to read an image resolution from the images EXIF data (PR
Use "unsigned char" consistemtly
Error handling for fread(), in case of error return NULL buffer
Free buffer after use
static int flush_tile(cf_image_t *img);
static cf_ib_t *get_tile(cf_image_t *img, int x, int y);
static void trim_spaces(char *buf);
-static char *find_bytes(FILE *fp, int *size);
+static unsigned char *find_bytes(FILE *fp, long int *size);
/*
* 'cfImageClose()' - Close an image file.
@@ -901,9 +901,9 @@ static void trim_spaces(char *buf)
helper function to extract bytes from image files
*/
-static char *find_bytes(FILE *fp, int *size)
+static unsigned char *find_bytes(FILE *fp, long int *size)
{
- char *buf;
+ unsigned char *buf;
long int originalOffset = ftell(fp);
fseek(fp, 0L, SEEK_END);
@@ -911,13 +911,19 @@ static char *find_bytes(FILE *fp, int *size)
// calculating the size of the file
long int res = ftell(fp);
- buf = (char *)malloc(res * sizeof(char) + 1);
+ buf = (unsigned char *)malloc(res * sizeof(unsigned char) + 1);
fseek(fp, 0, SEEK_SET);
- fread(buf, res, 1, fp);
+ if (fread(buf, 1, res, fp) < res)
+ {
+ free(buf);
+ buf = NULL;
+ *size = 0;
+ }
+ else
+ *size = res + 1;
fseek(fp, originalOffset, SEEK_SET);
- *size = res + 1;
return buf;
}
@@ -930,13 +936,14 @@ int _cupsImageReadEXIF(cf_image_t *img, FILE *fp)
return -1;
}
- int bufSize = 0;
+ long int bufSize = 0;
- char *buf = find_bytes(fp, &bufSize);
+ unsigned char *buf = find_bytes(fp, &bufSize);
- ExifData *ed = exif_data_new_from_data(buf, bufSize);
+ ExifData *ed = NULL;
- if (ed == NULL)
+ if (buf == NULL || bufSize <= 0 ||
+ (ed = exif_data_new_from_data(buf, bufSize)) == NULL)
{
DEBUG_printf(("DEBUG: No EXIF data found"));
return 2;
@@ -971,6 +978,7 @@ int _cupsImageReadEXIF(cf_image_t *img, FILE *fp)
img->xppi = xRes;
}
else{
+ free(buf);
return 2;
}
}
@@ -989,10 +997,12 @@ int _cupsImageReadEXIF(cf_image_t *img, FILE *fp)
img->yppi = yRes;
}
else{
+ free(buf);
return 2;
}
}
+ free(buf);
return 1;
}
#endif
