diff --git a/CImg.h b/CImg.h
index 83db0d90532ee05d4c151a78bce1fb250d15c454..fbf582aebb2de34c5f34442e140e4e4f82d7f4a4 100644
--- a/CImg.h
+++ b/CImg.h
@@ -49315,6 +49315,12 @@ namespace cimg_library_suffixed {
                               "load_pfm(): WIDTH and HEIGHT fields are undefined in file '%s'.",
                               cimg_instance,
                               filename?filename:"(FILE*)");
+      } else if (W<=0 || H<=0) {
+        if (!file) cimg::fclose(nfile);
+        throw CImgIOException(_cimg_instance
+                              "load_pfm(): WIDTH (%d) and HEIGHT (%d) fields are invalid in file '%s'.",
+                              cimg_instance,W,H,
+                              filename?filename:"(FILE*)");
       }
       if (err==2) {
         while ((err=std::fscanf(nfile," %16383[^\n]",item.data()))!=EOF && (*item=='#' || !err)) std::fgetc(nfile);