From ef095f5eef97a3592f883dec9a03e268ee8f4944 Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Sat, 11 Apr 2009 14:28:48 -0500 Subject: [PATCH] Check return value of fread and fwrite in image.c --- vm/image.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/vm/image.c b/vm/image.c index 5ce7147200..a1987180d0 100755 --- a/vm/image.c +++ b/vm/image.c @@ -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) + 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(!ok) { - print_string("Save data heap failed: "); print_string(strerror(errno)); nl(); - return false; + print_string("save-image failed: "); print_string(strerror(errno)); nl(); } - 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(fclose(file)) - { - print_string("Failed to close image file: "); print_string(strerror(errno)); nl(); - return false; - } - - return true; + return ok; } void primitive_save_image(void)