Skip to content

Commit

Permalink
Merge pull request #532 from d11mshepcar/fix-pam-reader
Browse files Browse the repository at this point in the history
Fixes for reading malformed/truncated PAM files
  • Loading branch information
jonsneyers authored Sep 3, 2021
2 parents f716e98 + 2485f1b commit 4a712df
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/image/image-pam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ bool image_load_pam_fp(FILE *fp, Image& image) {
#ifndef SUPPORT_HDR
if (maxval > 0xff) {
e_printf("PAM file has more than 8 bit per channel, this FLIF cannot handle that.\n");
fclose(fp);
return false;
}
#endif
Expand All @@ -78,8 +79,16 @@ bool image_load_pam_fp(FILE *fp, Image& image) {
for (unsigned int y=0; y<height; y++) {
for (unsigned int x=0; x<width; x++) {
for (unsigned int c=0; c<nbplanes; c++) {
ColorVal pixel= (fgetc(fp) << 8);
pixel += fgetc(fp);
int msb = fgetc(fp);
int lsb = fgetc(fp);
if (msb == EOF || lsb == EOF) {
e_printf("PAM file has insufficient data.\n");
fclose(fp);
return false;
}
ColorVal pixel = (msb << 8) + lsb;
if (pixel > maxval)
pixel = maxval;
image.set(c,y,x, pixel);
}
}
Expand All @@ -88,7 +97,15 @@ bool image_load_pam_fp(FILE *fp, Image& image) {
for (unsigned int y=0; y<height; y++) {
for (unsigned int x=0; x<width; x++) {
for (unsigned int c=0; c<nbplanes; c++) {
image.set(c,y,x, fgetc(fp));
int pixel = fgetc(fp);
if (pixel == EOF) {
e_printf("PAM file has insufficient data.\n");
fclose(fp);
return false;
}
if (pixel > maxval)
pixel = maxval;
image.set(c,y,x, pixel);
}
}
}
Expand Down

0 comments on commit 4a712df

Please sign in to comment.