changeset 1472:b9d3577d8290

Improve error handling and checking.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 10 May 2018 23:08:09 +0300
parents a988baa5b21f
children c543f5ae92d5
files tools/lib64gfx.c
diffstat 1 files changed, 14 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/tools/lib64gfx.c	Thu May 10 23:07:50 2018 +0300
+++ b/tools/lib64gfx.c	Thu May 10 23:08:09 2018 +0300
@@ -1365,10 +1365,19 @@
     if (img == NULL || fmt == NULL)
         return DMERR_NULLPTR;
 
+    // Sanity check img vs format
+    if (img->type != fmt->type ||
+        img->width != fmt->width || img->height != fmt->height)
+    {
+        res = dmError(DMERR_NOT_SUPPORTED,
+            "Can't directly encode a differing C64 bitmap image format.\n");
+        goto err;
+    }
+
     // Allocate the output buffer
     if ((res = dmGrowBufAlloc(buf, fmt->size, BUF_SIZE_GROW)) != DMERR_OK)
     {
-        return dmError(res,
+        dmError(res,
             "Could not allocate %d bytes of memory for C64 image encoding buffer.\n",
             fmt->size);
         goto err;
@@ -1725,10 +1734,13 @@
 {
     int res;
 
-    if (img == NULL)
+    if (img == NULL || fmt == NULL)
         return DMERR_NULLPTR;
 
     // Attempt to encode the image to a buffer
+    if ((res = dmGrowBufInit(buf)) != DMERR_OK)
+        return res;
+
     if (fmt->encode != NULL)
         res = fmt->encode(buf, img, fmt);
     else