changeset 2405:1f74cc842d71

Add some error checking to data2inc.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 13 Jan 2020 20:30:13 +0200
parents bcd33c77c605
children b153bc46241d
files tools/data2inc.c
diffstat 1 files changed, 94 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/tools/data2inc.c	Mon Jan 13 20:04:54 2020 +0200
+++ b/tools/data2inc.c	Mon Jan 13 20:30:13 2020 +0200
@@ -26,10 +26,10 @@
 
     char *defDataType;
 
-    void (*writeHeader) (FILE *fh, const char *name);
-    void (*writeDecl) (FILE *fh, const size_t len, const char *name);
-    void (*writeData) (FILE *fh, const Uint8 *buf, const size_t len);
-    void (*writeFooter) (FILE *fh, const size_t len, const char *name);
+    int  (*writeHeader) (FILE *fh, const char *name);
+    int  (*writeDecl) (FILE *fh, const size_t len, const char *name);
+    int  (*writeData) (FILE *fh, const Uint8 *buf, const size_t len);
+    int  (*writeFooter) (FILE *fh, const size_t len, const char *name);
 } DMOutputFormat;
 
 
@@ -200,121 +200,157 @@
 
 /* Assembler include data output functions
  */
-void writeHeader_ASM(FILE *fh, const char *name)
+int writeHeader_ASM(FILE *fh, const char *name)
 {
+    int res = 0;
+
     if (name)
-        fprintf(fh, "; '%s'", name);
+        res = fprintf(fh, "; '%s'", name);
     else
-        fprintf(fh, "; Generated");
+        res = fprintf(fh, "; Generated");
 
-    fprintf(fh, " by %s v%s\n",
+    if (res >= 0)
+        res = fprintf(fh, " by %s v%s\n",
         dmProgName, dmProgVersion);
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeDecl_ASM(FILE *fh, const size_t len, const char *name)
+int writeDecl_ASM(FILE *fh, const size_t len, const char *name)
 {
+    int res = 0;
+
     if (optExtraData)
-        fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
+        res = fprintf(fh, "%s_size = %" DM_PRIu_SIZE_T "\n", name, len);
 
-    fprintf(fh, "%s:\n", name);
+    if (res >= 0)
+        res = fprintf(fh, "%s:\n", name);
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeData_ASM(FILE *fh, const Uint8 * buf, const size_t len)
+int writeData_ASM(FILE *fh, const Uint8 * buf, const size_t len)
 {
-    fprintf(fh, "%s ", optDataType);
+    int res = fprintf(fh, "%s ", optDataType);
 
-    for (size_t i = 0; i < len; i++)
+    for (size_t i = 0; res >= 0 && i < len; i++)
     {
         if (optFormatting)
         {
             if (optHexMode)
-                fprintf(fh, "$%.2x", buf[i]);
+                res = fprintf(fh, "$%.2x", buf[i]);
             else
-                fprintf(fh, "%3d", buf[i]);
+                res = fprintf(fh, "%3d", buf[i]);
         }
         else
         {
             if (optHexMode)
-                fprintf(fh, "$%x", buf[i]);
+                res = fprintf(fh, "$%x", buf[i]);
             else
-                fprintf(fh, "%d", buf[i]);
+                res = fprintf(fh, "%d", buf[i]);
         }
 
-        if (i + 1 < len)
-            fprintf(fh, optFormatting ? ", " : ",");
+        if (res >= 0 && i + 1 < len)
+            res = fprintf(fh, optFormatting ? ", " : ",");
     }
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeFooter_ASM(FILE *fh, const size_t len, const char *name)
+int writeFooter_ASM(FILE *fh, const size_t len, const char *name)
 {
+    int res;
     (void) len;
+
     if (optExtraData)
-        fprintf(fh, "%s_end: \n", name);
+        res = fprintf(fh, "%s_end: \n", name);
     else
-        fprintf(fh, "\n");
+        res = fprintf(fh, "\n");
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
 /* ANSI-C include data output functions
  */
-void writeHeader_C(FILE *fh, const char *name)
+int writeHeader_C(FILE *fh, const char *name)
 {
+    int res;
+
     if (name)
-        fprintf(fh, "/* '%s' generated", name);
+        res = fprintf(fh, "/* '%s' generated", name);
     else
-        fprintf(fh, "/* Generated");
+        res = fprintf(fh, "/* Generated");
 
-    fprintf(fh, " by %s v%s\n" " */\n",
+    if (res >= 0)
+        res = fprintf(fh, " by %s v%s\n" " */\n",
         dmProgName, dmProgVersion);
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeDecl_C(FILE *fh, const size_t len, const char *name)
+int writeDecl_C(FILE *fh, const size_t len, const char *name)
 {
-    fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
+    int res;
+
+    res = fprintf(fh, "%s %s[%" DM_PRIu_SIZE_T "] = {\n",
         optDataType, name, len);
 
-    printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
+    if (res >= 0)
+        res = printf("extern %s %s[%" DM_PRIu_SIZE_T "];\n",
         optDataType, name, len);
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeData_C(FILE *fh, const Uint8 *buf, const size_t len)
+int writeData_C(FILE *fh, const Uint8 *buf, const size_t len)
 {
-    for (size_t i = 0; i < len; i++)
+    int res = 0;
+
+    for (size_t i = 0; res >= 0 && i < len; i++)
     {
         if (optFormatting)
         {
             if (optHexMode)
-                fprintf(fh, "0x%.2x", buf[i]);
+                res = fprintf(fh, "0x%.2x", buf[i]);
             else
-                fprintf(fh, "%3d", buf[i]);
+                res = fprintf(fh, "%3d", buf[i]);
         }
         else
         {
             if (optHexMode)
-                fprintf(fh, "0x%x", buf[i]);
+                res = fprintf(fh, "0x%x", buf[i]);
             else
-                fprintf(fh, "%d", buf[i]);
+                res = fprintf(fh, "%d", buf[i]);
         }
 
-        fprintf(fh, optFormatting ? ", " : ",");
+        if (res >= 0)
+            res = fprintf(fh, optFormatting ? ", " : ",");
     }
+
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
-void writeFooter_C(FILE *fh, const size_t len, const char *name)
+int writeFooter_C(FILE *fh, const size_t len, const char *name)
 {
+    int res;
     (void) len;
     (void) name;
 
-    fprintf(fh, "};\n");
+    res = fprintf(fh, "};\n");
+    return res >= 0 ? DMERR_OK : dmGetErrno();
 }
 
 
+/*
+ * List of formats
+ */
 static const DMOutputFormat dmFormatList[] =
 {
     {
@@ -455,8 +491,13 @@
     totalSize = dmGetFileSize(inFile);
 
     // Output header
-    if (!optQuiet)
-        setFormat->writeHeader(outFile, optOutFilename);
+    if (!optQuiet &&
+        (res = setFormat->writeHeader(outFile, optOutFilename)) != DMERR_OK)
+    {
+        dmErrorMsg("Error writing output header: %s\n",
+            dmErrorStr(res));
+        goto exit;
+    }
 
     if (optAddLine)
         fprintf(outFile, "%s\n", optAddLine);
@@ -482,14 +523,24 @@
                     fputs(" ", outFile);
             }
 
-            setFormat->writeData(outFile, dataBuf, res);
+            if ((res = setFormat->writeData(outFile, dataBuf, res)) != DMERR_OK)
+            {
+                dmErrorMsg("Error writing output data: %s\n",
+                    dmErrorStr(res));
+                goto exit;
+            }
 
             fprintf(outFile, "\n");
         }
     }
 
     // Output footer
-    setFormat->writeFooter(outFile, totalSize, optObjName);
+    if ((res = setFormat->writeFooter(outFile, totalSize, optObjName)) != DMERR_OK)
+    {
+        dmErrorMsg("Error writing output footer: %s\n",
+            dmErrorStr(res));
+        goto exit;
+    }
 
 exit:
     // Cleanup