Skip to content

Instantly share code, notes, and snippets.

@apoleon
Created January 11, 2019 19:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apoleon/24df7819b257faade31125303e91e4a2 to your computer and use it in GitHub Desktop.
Save apoleon/24df7819b257faade31125303e91e4a2 to your computer and use it in GitHub Desktop.
From: Markus Koschany <apo@debian.org>
Date: Fri, 11 Jan 2019 16:59:12 +0100
Subject: fix overflow on 32bit systems
---
src/common-image.c | 15 +++++++++++++--
1 file changed, 13 insertions(+), 2 deletions(-)
diff --git a/src/common-image.c b/src/common-image.c
index 8f5cd15..a17329c 100644
--- a/src/common-image.c
+++ b/src/common-image.c
@@ -166,8 +166,19 @@ struct image * load_image(char const * name)
return NULL;
}
- /* Allocate the pixel buffer */
- im->pixels = malloc(im->w * im->h * depth);
+ /* Allocate the pixel buffer and check it does not overflow on 32bit systems */
+ if (im->w > 0x7fffffff / (im->h * depth) || im->h > 0x7fffffff / (im->w * depth)
+ || depth > 0x7fffffff / (im->w * im->h))
+ {
+ caca_file_close(f);
+ free(im);
+ return NULL;
+ }
+ else
+ {
+ im->pixels = malloc(im->w * im->h * depth);
+ }
+
if(!im->pixels)
{
caca_file_close(f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment