]> gitweb.factorcode.org Git - factor.git/commitdiff
Check return value of fread and fwrite in image.c
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 11 Apr 2009 19:28:48 +0000 (14:28 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Sat, 11 Apr 2009 19:28:48 +0000 (14:28 -0500)
vm/image.c

index 5ce7147200645c57e5d3e38e0de5ccb5a2394226..a1987180d0fa9280d3a002336a22081030a15aaf 100755 (executable)
@@ -86,7 +86,8 @@ void load_image(F_PARAMETERS *p)
        }
 
        F_HEADER h;
-       fread(&h,sizeof(F_HEADER),1,file);
+       if(fread(&h,sizeof(F_HEADER),1,file) != 1)
+               fatal_error("Cannot read image header",0);
 
        if(h.magic != IMAGE_MAGIC)
                fatal_error("Bad image: magic number check failed",h.magic);
@@ -145,27 +146,19 @@ bool save_image(const F_CHAR *filename)
                        h.userenv[i] = userenv[i];
        }
 
-       fwrite(&h,sizeof(F_HEADER),1,file);
+       bool ok = true;
 
-       if(fwrite((void*)tenured->start,h.data_size,1,file) != 1)
-       {
-               print_string("Save data heap failed: "); print_string(strerror(errno)); nl();
-               return false;
-       }
-
-       if(fwrite(first_block(&code_heap),h.code_size,1,file) != 1)
-       {
-               print_string("Save code heap failed: "); print_string(strerror(errno)); nl();
-               return false;
-       }
+       if(fwrite(&h,sizeof(F_HEADER),1,file) != 1) ok = false;
+       if(fwrite((void*)tenured->start,h.data_size,1,file) != 1) ok = false;
+       if(fwrite(first_block(&code_heap),h.code_size,1,file) != 1) ok = false;
+       if(fclose(file)) ok = false;
 
-       if(fclose(file))
+       if(!ok)
        {
-               print_string("Failed to close image file: "); print_string(strerror(errno)); nl();
-               return false;
+               print_string("save-image failed: "); print_string(strerror(errno)); nl();
        }
 
-       return true;
+       return ok;
 }
 
 void primitive_save_image(void)