sdl2-compat: SDL2Compat_strlen: hack to prevent gcc optimizing the code into strlen

From 2347d5c89752c43107f9a0a8108a6b6ac46bbfa0 Mon Sep 17 00:00:00 2001
From: Ozkan Sezer <[EMAIL REDACTED]>
Date: Mon, 10 Feb 2025 23:37:14 +0300
Subject: [PATCH] SDL2Compat_strlen: hack to prevent gcc optimizing the code
 into strlen

making retval volatile prevents gcc optimizing the code into strlen and
defeating the purpose of the procedure.

Fixes https://github.com/libsdl-org/sdl2-compat/issues/340
---
 src/sdl2_compat.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index fa570c4a..c00ec035 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -251,7 +251,7 @@ SDL2COMPAT_itoa(char *dst, int val)
 /* you can use SDL3_strlen once we're past startup. */
 static int SDL2Compat_strlen(const char *str)
 {
-    int retval = 0;
+    volatile int retval = 0;  /* volatile prevents gcc from optimizing this into strlen() */
     while (str[retval]) {
         retval++;
     }