]> git.draconx.ca Git - liblbx.git/blobdiff - src/image.c
liblbx: Fix framebuffer allocation for 0x0 images.
[liblbx.git] / src / image.c
index 4657208a05ae28439496346473c8de7cb1566023..f68f2da9f9ac4b1573e9d16ed53279ac9a236043 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
+#include <inttypes.h>
 #include <errno.h>
 
 #include "pack.h"
@@ -280,6 +281,15 @@ static unsigned char **allocframebuffer(size_t width, size_t height)
        unsigned char **new, *tmp;
        size_t i;
 
+       if (height > SIZE_MAX / sizeof *new) {
+               lbx_error_raise(LBX_ENOMEM);
+               return NULL;
+       }
+
+       /* Ensure that there is at least one row in the framebuffer. */
+       if (height == 0 || width == 0)
+               width = height = 1;
+
        tmp = calloc(height, width);
        if (!tmp) {
                lbx_error_raise(LBX_ENOMEM);
@@ -413,9 +423,9 @@ lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
                }
 
                palette[i] = (struct lbx_colour) {
-                       .red    = entry[1] << 2,
-                       .green  = entry[2] << 2,
-                       .blue   = entry[3] << 2,
+                       .red    = entry[1] & 0x3f,
+                       .green  = entry[2] & 0x3f,
+                       .blue   = entry[3] & 0x3f,
                        .active = 1,
                };
        }
@@ -451,9 +461,9 @@ lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
                }
 
                palette[img->palstart + i] = (struct lbx_colour){
-                       .red    = entry[1] << 2,
-                       .green  = entry[2] << 2,
-                       .blue   = entry[3] << 2,
+                       .red    = entry[1],
+                       .green  = entry[2],
+                       .blue   = entry[3],
                        .active = 1,
                };
        }