fixed potential crash bug (accessing invalid memory)
authorHolger Schemel <info@artsoft.org>
Mon, 7 Jan 2019 15:42:30 +0000 (16:42 +0100)
committerHolger Schemel <info@artsoft.org>
Mon, 7 Jan 2019 15:42:30 +0000 (16:42 +0100)
This change fixes a bug that occured when invoking the changed
function with a "graphic" parameter indicating undefined graphic,
which is defined as "-1" and caused an "array out of bounds" access.

To fix this bug, the function parameter is checked before used.

src/tools.c

index 459f85a769cf82263a4229a219130bcc973461af..33b677954947107346e76e69d65f778027d1e2f6 100644 (file)
@@ -1150,10 +1150,12 @@ void FadeSkipNextFadeOut(void)
 
 static Bitmap *getBitmapFromGraphicOrDefault(int graphic, int default_graphic)
 {
+  if (graphic == IMG_UNDEFINED)
+    return NULL;
+
   boolean redefined = getImageListEntryFromImageID(graphic)->redefined;
 
-  return (graphic == IMG_UNDEFINED ? NULL :
-         graphic_info[graphic].bitmap != NULL || redefined ?
+  return (graphic_info[graphic].bitmap != NULL || redefined ?
          graphic_info[graphic].bitmap :
          graphic_info[default_graphic].bitmap);
 }