From b7cbdfe8f923e6722ffd409fdd99f6e642cd9541 Mon Sep 17 00:00:00 2001 From: Misa Date: Wed, 22 Dec 2021 21:49:08 -0800 Subject: [PATCH] Fix char overflow in Analogue Mode In aa7b63fa5f3a46d0e41f784210332d7cd7306474, I didn't notice that the result was implicitly being converted to int by the min/max from before. I instead added it to the existing char, but that resulted in a char overflow (it's unsigned, so thankfully not undefined behavior). But of course the entire point of that commit is to make it explicitly clear when you are converting between types, intentionally or otherwise, in min/max comparisons. So despite causing a regression (which I have now fixed), at least it did its job. --- desktop_version/src/GraphicsUtil.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/desktop_version/src/GraphicsUtil.cpp b/desktop_version/src/GraphicsUtil.cpp index 2cb8cd72..9c86a4ec 100644 --- a/desktop_version/src/GraphicsUtil.cpp +++ b/desktop_version/src/GraphicsUtil.cpp @@ -336,6 +336,7 @@ SDL_Surface* ApplyFilter( SDL_Surface* _src ) Uint8 red = (pixelOffset & _src->format->Rmask) >> 16 ; double mult; + int tmp; /* needed to avoid char overflow */ if(isscrolling && sampley > 220 && ((rand() %10) < 4)) { mult = 0.6; @@ -345,12 +346,12 @@ SDL_Surface* ApplyFilter( SDL_Surface* _src ) mult = 0.2; } - red += fRandom() * mult * 254; - red = SDL_min(red, 255); - green += fRandom() * mult * 254; - green = SDL_min(green, 255); - blue += fRandom() * mult * 254; - blue = SDL_min(blue, 255); + tmp = red + fRandom() * mult * 254; + red = SDL_min(tmp, 255); + tmp = green + fRandom() * mult * 254; + green = SDL_min(tmp, 255); + tmp = blue + fRandom() * mult * 254; + blue = SDL_min(tmp, 255); if(y % 2 == 0) {