From 2fa2a9480a72ac9e4e21a63d97fa83feda816546 Mon Sep 17 00:00:00 2001 From: Mingjie Shen Date: Tue, 13 Feb 2024 14:02:31 -0500 Subject: [PATCH] Fix undefined behavior of signed integer overflow Testing for overflow by adding a value to a variable to see if it "wraps around" works only for unsigned integer values. Signed integer overflow is undefined behavior in C and C++. --- tests/tearing.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/tearing.c b/tests/tearing.c index 5c7893c2..293c7af7 100644 --- a/tests/tearing.c +++ b/tests/tearing.c @@ -36,6 +36,7 @@ #include #include #include +#include #include "linmath.h" @@ -103,7 +104,7 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, { case GLFW_KEY_UP: { - if (swap_interval + 1 > swap_interval) + if (swap_interval <= INT_MAX - 1) set_swap_interval(window, swap_interval + 1); break; } @@ -112,12 +113,12 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, { if (swap_tear) { - if (swap_interval - 1 < swap_interval) + if (swap_interval >= INT_MIN + 1) set_swap_interval(window, swap_interval - 1); } else { - if (swap_interval - 1 >= 0) + if (swap_interval >= 1) set_swap_interval(window, swap_interval - 1); } break;