diff --git a/src/lodepng.cpp b/src/lodepng.cpp
index b237d5a..839f05a 100644
--- a/src/lodepng.cpp
+++ b/src/lodepng.cpp
@@ -4132,12 +4132,13 @@ unsigned LodePNG_loadFile(unsigned char** out, size_t* outsize, const char* file
   fseek(file , 0 , SEEK_END);
   size = ftell(file);
   rewind(file);
-  
-  /*read contents of the file into the vector*/
-  *outsize = 0;
-  *out = (unsigned char*)malloc((size_t)size);
-  if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file);
-
+  if(size>0)
+  {
+	/*read contents of the file into the vector*/
+	*outsize = 0;
+	*out = (unsigned char*)malloc((size_t)size);
+	if(size && (*out)) (*outsize) = fread(*out, 1, (size_t)size, file);
+  }
   fclose(file);
   if(!(*out) && size) return 80; /*the above malloc failed*/
   return 0;
