tools/sfnt2fnt.c: Check return value of fwrite. Report and exit in case of error. (bug # 16413)

titon barua titanix88 at gmail.com
Sun Jan 4 14:11:29 CST 2009


---
 tools/sfnt2fnt.c |   64 ++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 45 insertions(+), 19 deletions(-)

diff --git a/tools/sfnt2fnt.c b/tools/sfnt2fnt.c
index 6880ee8..c132321 100644
--- a/tools/sfnt2fnt.c
+++ b/tools/sfnt2fnt.c
@@ -520,12 +520,23 @@ static struct fontinfo *fill_fontinfo( const char *face_name, int ppem, int enc,
     return info;
 }
 
+static void report_error_and_exit (const char * s)
+{
+    perror (s);
+    exit (1);
+}
+
 static void write_fontinfo( const struct fontinfo *info, FILE *fp )
 {
-    fwrite( &info->hdr, sizeof(info->hdr), 1, fp );
-    fwrite( info->dfCharTable + info->hdr.fi.dfFirstChar, sizeof(*info->dfCharTable),
-            ((unsigned char)info->hdr.fi.dfLastChar - (unsigned char)info->hdr.fi.dfFirstChar) + 3, fp );
-    fwrite( info->data, info->hdr.dfSize - info->hdr.fi.dfBitsOffset, 1, fp );
+    if (fwrite( &info->hdr, sizeof(info->hdr), 1, fp ) != 1)
+        report_error_and_exit ( "Error writing fontinfo" );
+    if (fwrite( info->dfCharTable + info->hdr.fi.dfFirstChar, sizeof(*info->dfCharTable),
+            ((unsigned char)info->hdr.fi.dfLastChar - (unsigned char)info->hdr.fi.dfFirstChar) + 3, fp ) != 
+ (((unsigned char)info->hdr.fi.dfLastChar - (unsigned char)info->hdr.fi.dfFirstChar) + 3))
+        report_error_and_exit ( "Error writing fontinfo" );
+
+    if (fwrite( info->data, info->hdr.dfSize - info->hdr.fi.dfBitsOffset, 1, fp ) != 3)
+        report_error_and_exit ( "Error writing fontinfo" );
 }
 
 /* parse options from the argv array and remove all the recognized ones */
@@ -696,16 +707,20 @@ int main(int argc, char **argv)
         goto done;
     }
 
-    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 ( option_output );
+    if (fwrite(&NE_hdr, sizeof(NE_hdr), 1, ofp) != 1)
+       report_error_and_exit ( option_output );
 
     align = 4;
-    fwrite(&align, sizeof(align), 1, ofp);
+    if (fwrite(&align, sizeof(align), 1, ofp) != 1)
+       report_error_and_exit ( option_output );
 
     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 ( option_output );
 
     rc_name.offset = fontdir_off >> 4;
     rc_name.length = (fontdir_len + 15) >> 4;
@@ -713,12 +728,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 ( option_output );
 
     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 ( option_output );
 
     for(res = first_res | 0x8000, i = 0; i < num_files; i++, res++) {
         int len = (info[i]->hdr.dfSize + 15) & ~0xf;
@@ -729,26 +746,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 ( option_output );
 
         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 ( option_output );
 
     fputc(strlen("FONTDIR"), ofp);
-    fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp);
+    if (fwrite("FONTDIR", strlen("FONTDIR"), 1, ofp) != 1)
+        report_error_and_exit ( option_output );
     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 ( option_output );
 
     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 ( option_output );
     fputc(0x00, ofp); /* terminator */
 
     /* empty ne_modtab and ne_imptab */
@@ -762,14 +784,18 @@ 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 ( option_output );
 
     for(res = first_res, i = 0; i < num_files; i++, res++) {
         const char *name = get_face_name( info[i] );
-        fwrite(&res, sizeof(res), 1, ofp);
-        fwrite(&info[i]->hdr, FIELD_OFFSET(FNT_HEADER,fi.dfBitsOffset), 1, ofp);
+        if (fwrite(&res, sizeof(res), 1, ofp) != 1)
+            report_error_and_exit ( option_output );
+        if (fwrite(&info[i]->hdr, FIELD_OFFSET(FNT_HEADER,fi.dfBitsOffset), 1, ofp) != 1)
+            report_error_and_exit ( option_output );
         fputc(0x00, ofp);
-        fwrite(name, strlen(name) + 1, 1, ofp);
+        if (fwrite(name, strlen(name) + 1, 1, ofp) != 1)
+            report_error_and_exit ( option_output );
     }
 
     pad = ftell(ofp) & 0xf;
-- 
1.5.6.3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winehq.org/pipermail/wine-patches/attachments/20090105/4d627f9a/attachment-0001.htm 


More information about the wine-patches mailing list