From dab4ce9dd07b13e57cf6008a66dbd6c02f5e9eb4 Mon Sep 17 00:00:00 2001 From: Fabian Arndt Date: Wed, 7 Jun 2023 03:50:20 +0200 Subject: [PATCH] Implemented macro for checking array bounds --- src/hud_elements.cpp | 4 ++-- src/mesa/util/macros.h | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/hud_elements.cpp b/src/hud_elements.cpp index c4f92e24..bd7e218d 100644 --- a/src/hud_elements.cpp +++ b/src/hud_elements.cpp @@ -849,7 +849,7 @@ void HudElements::gamescope_scaler(){ HUDElements.TextColored(HUDElements.colors.engine, "%s", "SCALER"); ImguiNextColumnOrNewRow(); - if (HUDElements.g_scaler >= 0 && HUDElements.g_scaler < (int)ARRAY_SIZE(gamescope_upscale_scaler)) + if (ARRAY_CHECK_BOUNDS(gamescope_upscale_scaler, HUDElements.g_scaler)) right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", gamescope_upscale_scaler[HUDElements.g_scaler]); else right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", "UNKNOWN"); @@ -865,7 +865,7 @@ void HudElements::gamescope_scaler_filter(){ HUDElements.TextColored(HUDElements.colors.engine, "%s", "FILTER"); ImguiNextColumnOrNewRow(); - if (HUDElements.g_scaler_filter >= 0 && HUDElements.g_scaler_filter < (int)ARRAY_SIZE(gamescope_upscale_filter)) + if (ARRAY_CHECK_BOUNDS(gamescope_upscale_filter, HUDElements.g_scaler_filter)) right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", gamescope_upscale_filter[HUDElements.g_scaler_filter]); else right_aligned_text(HUDElements.colors.text, HUDElements.ralign_width, "%s", "UNKNOWN"); diff --git a/src/mesa/util/macros.h b/src/mesa/util/macros.h index 16c88dbb..55ee7575 100644 --- a/src/mesa/util/macros.h +++ b/src/mesa/util/macros.h @@ -34,6 +34,11 @@ # define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) #endif +/* Check if index is in array's bounds */ +#ifndef ARRAY_CHECK_BOUNDS +# define ARRAY_CHECK_BOUNDS(array, index) (index >= 0 && index < (int)ARRAY_SIZE(array)) +#endif + /* For compatibility with Clang's __has_builtin() */ #ifndef __has_builtin # define __has_builtin(x) 0