tools/fnt2fon.c: check return values of fread and fwrite to prevent gcc warnings. exit(1) in case of any error (bug # 16413)

titon barua titanix88 at gmail.com
Sun Jan 4 10:25:29 CST 2009


---
 tools/fnt2fon.c |   77 +++++++++++++++++++++++++++++++++++++++---------------
 1 files changed, 55 insertions(+), 22 deletions(-)

diff --git a/tools/fnt2fon.c b/tools/fnt2fon.c
index aa7d72b..6b21e6e 100644
--- a/tools/fnt2fon.c
+++ b/tools/fnt2fon.c
@@ -75,6 +75,12 @@ static void usage(char **argv)
     return;
 }
 
+static void report_error_and_exit (const char * s)
+{
+     perror(s);
+     exit(1);
+}
+
 #ifndef __GNUC__
 #define __attribute__(X)
 #endif
@@ -115,17 +121,26 @@ int main(int argc, char **argv)
             usage(argv);
             exit(1);
         }
-        fread(&ver, sizeof(short), 1, fp);
+        if (fread(&ver, sizeof(short), 1, fp) != 1)
+            report_error_and_exit (argv[i+1]);
         if(ver != 0x200 && ver != 0x300) {
             fprintf(stderr, "error: invalid fnt file %s ver %d\n", argv[i+1], ver);
             exit(1);
         }
-        fread(file_lens + i, sizeof(int), 1, fp);
+        if (fread(file_lens + i, sizeof(int), 1, fp) != 1)
+            report_error_and_exit (argv[i+1]);
         fseek(fp, 0x44, SEEK_SET);
-        fread(&pt, sizeof(short), 1, fp);
-        fread(dpi, sizeof(short), 2, fp);
+        if (fread(&pt, sizeof(short), 1, fp) != 1)
+            report_error_and_exit (argv[i+1]);
+
+        if (fread(dpi, sizeof(short), 2, fp) != 2)
+            report_error_and_exit (argv[i+1]);
+
         fseek(fp, 0x69, SEEK_SET);
-        fread(&off, sizeof(long), 1, fp);
+
+        if (fread(&off, sizeof(long), 1, fp) != 1)
+            report_error_and_exit (argv[i+1]);
+ 
         fseek(fp, off, SEEK_SET);
         cp = name;
         while((c = fgetc(fp)) != 0 && c != EOF)
@@ -189,16 +204,21 @@ int main(int argc, char **argv)
     output_file = argv[argc - 1];
     ofp = fopen(output_file, "wb");
 
-    fwrite(MZ_hdr, sizeof(MZ_hdr), 1, ofp);
-    fwrite(&NE_hdr, sizeof(NE_hdr), 1, ofp);
+    if (fwrite(MZ_hdr, sizeof(MZ_hdr), 1, ofp) != 1)
+        report_error_and_exit (output_file);
+
+    if (fwrite(&NE_hdr, sizeof(NE_hdr), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     align = 4;
-    fwrite(&align, sizeof(align), 1, ofp);
+    if (fwrite(&align, sizeof(align), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     rc_type.type_id = NE_RSCTYPE_FONTDIR;
     rc_type.count = 1;
     rc_type.resloader = 0;
-    fwrite(&rc_type, sizeof(rc_type), 1, ofp);
+    if (fwrite(&rc_type, sizeof(rc_type), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     rc_name.offset = fontdir_off >> 4;
     rc_name.length = (fontdir_len + 15) >> 4;
@@ -206,12 +226,14 @@ int main(int argc, char **argv)
     rc_name.id = resident_name_off - sizeof("FONTDIR") - NE_hdr.ne_rsrctab;
     rc_name.handle = 0;
     rc_name.usage = 0;
-    fwrite(&rc_name, sizeof(rc_name), 1, ofp);
+    if (fwrite(&rc_name, sizeof(rc_name), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     rc_type.type_id = NE_RSCTYPE_FONT;
     rc_type.count = num_files;
     rc_type.resloader = 0;
-    fwrite(&rc_type, sizeof(rc_type), 1, ofp);
+    if (fwrite(&rc_type, sizeof(rc_type), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     for(res = first_res | 0x8000, i = 0; i < num_files; i++, res++) {
         int len = (file_lens[i] + 15) & ~0xf;
@@ -222,26 +244,31 @@ int main(int argc, char **argv)
         rc_name.id = res;
         rc_name.handle = 0;
         rc_name.usage = 0;
-        fwrite(&rc_name, sizeof(rc_name), 1, ofp);
+        if (fwrite(&rc_name, sizeof(rc_name), 1, ofp) != 1)
+            report_error_and_exit (output_file);
 
         font_off += len;
     }
 
     /* empty type info */
     memset(&rc_type, 0, sizeof(rc_type));
-    fwrite(&rc_type, sizeof(rc_type), 1, ofp);
+    if (fwrite(&rc_type, sizeof(rc_type), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     fputc(strlen("FONTDIR"), ofp);
-    fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp);
+    if (fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp) != 1)
+        report_error_and_exit (output_file);
     fputc(strlen(resident_name), ofp);
-    fwrite(resident_name, strlen(resident_name), 1, ofp);
+    if (fwrite(resident_name, strlen(resident_name), 1, ofp) != 1)
+        report_error_and_exit (output_file);
 
     fputc(0x00, ofp);    fputc(0x00, ofp);
     fputc(0x00, ofp);
     fputc(0x00, ofp);    fputc(0x00, ofp);
 
     fputc(strlen(non_resident_name), ofp);
-    fwrite(non_resident_name, strlen(non_resident_name), 1, ofp);
+    if (fwrite(non_resident_name, strlen(non_resident_name), 1, ofp) != 1)
+        report_error_and_exit (output_file);
     fputc(0x00, ofp); /* terminator */
 
     /* empty ne_modtab and ne_imptab */
@@ -255,24 +282,29 @@ int main(int argc, char **argv)
         fputc(0x00, ofp);
 
     /* FONTDIR resource */
-    fwrite(&num_files, sizeof(num_files), 1, ofp);
+    if (fwrite(&num_files, sizeof(num_files), 1, ofp) != 1)
+        report_error_and_exit (output_file);
     
     for(res = first_res, i = 0; i < num_files; i++, res++) {
         fp = fopen(argv[i+1], "rb");
 
-        fwrite(&res, sizeof(res), 1, ofp);
-        fread(buf, 0x72, 1, fp);
+        if (fwrite(&res, sizeof(res), 1, ofp) != 1)
+            report_error_and_exit (output_file);
+        if (fread(buf, 0x72, 1, fp) != 1)
+            report_error_and_exit ( argv[i+1] );
 
         fnt_header = (struct _fnt_header *)buf;
         fseek(fp, fnt_header->fi.dfFace, SEEK_SET);
         fnt_header->fi.dfBitsOffset = 0;
-        fwrite(buf, 0x72, 1, ofp);
+        if (fwrite(buf, 0x72, 1, ofp) != 1)
+            report_error_and_exit (output_file);
 
         cp = name;
         while((c = fgetc(fp)) != 0 && c != EOF)
             *cp++ = c;
         *cp = '\0';
-        fwrite(name, strlen(name) + 1, 1, ofp);
+        if (fwrite(name, strlen(name) + 1, 1, ofp) != 1)
+            report_error_and_exit (output_file);
         fclose(fp);
     }
 
@@ -288,7 +320,8 @@ int main(int argc, char **argv)
         while(1) {
             nread = read(fileno(fp), buf, sizeof(buf));
             if(!nread) break;
-            fwrite(buf, nread, 1, ofp);
+            if (fwrite(buf, nread, 1, ofp) != 1)
+                report_error_and_exit (output_file);
         }
         fclose(fp);
         pad = file_lens[i] & 0xf;
-- 
1.5.6.3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winehq.org/pipermail/wine-patches/attachments/20090104/c4197333/attachment-0001.htm 


More information about the wine-patches mailing list