Fredrik Lundh avatar Fredrik Lundh committed 3943b4c

Detect I/O errors.

Comments (0)

Files changed (1)

libImaging/File.c

 
     /* PPM magic */
     if (fgetc(fp) != 'P')
-	goto syntax;
+	goto error;
     switch (fgetc(fp)) {
     case '4': /* FIXME: 1-bit images are not yet supported */
-	goto syntax;
+	goto error;
     case '5':
 	mode = "L";
 	break;
 	mode = "RGB";
 	break;
     default:
-	goto syntax;
+	goto error;
     }
 
     i = 0;
 		do {
 		    c = fgetc(fp);
 		    if (c == EOF)
-			goto syntax;
+			goto error;
 		} while (c != '\n');
 		c = fgetc(fp);
 	    }
 	}
 
 	if (c == EOF)
-	    goto syntax;
+	    goto error;
 
 	switch (i++) {
 	case 0:
 
 	/* PPM "L" */
 	for (y = 0; y < im->ysize; y++)
-	    fread(im->image[y], 1, im->xsize, fp);
+	    if (fread(im->image[y], im->xsize, 1, fp) != 1)
+		goto error;
 
     } else {
 
 	/* PPM "RGB" or PyPPM mode */
 	for (y = 0; y < im->ysize; y++)
 	    for (x = i = 0; x < im->xsize; x++, i += im->pixelsize)
-		fread(im->image[y]+i, 1, im->bands, fp);
-
+		if (fread(im->image[y]+i, im->bands, 1, fp) != 1)
+		    goto error;
     }
 
     fclose(fp);
 
     return im;
 
-syntax:
+error:
     fclose(fp);
     return ImagingError_IOError();
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.