From 9ab4b2213a5a8ac466a9ddc3d7cf864299dab812 Mon Sep 17 00:00:00 2001 From: Themaister Date: Tue, 7 Jan 2014 17:43:44 +0100 Subject: [PATCH] Avoid potential overflows with overlay analogs. --- retroarch.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/retroarch.c b/retroarch.c index 93b06e2af2..5c0d92b973 100644 --- a/retroarch.c +++ b/retroarch.c @@ -636,7 +636,15 @@ static int16_t input_state(unsigned port, unsigned device, unsigned index, unsig { unsigned base = (index == RETRO_DEVICE_INDEX_ANALOG_RIGHT) ? 2 : 0; base += (id == RETRO_DEVICE_ID_ANALOG_Y) ? 1 : 0; - res += driver.overlay_state.analog[base]; + + // Simply adding here could overflow the int16_t. + int sum = res + driver.overlay_state.analog[base]; + if (sum > 0x7fff) + res = 0x7fff; + else if (sum < -0x7fff) + res = -0x7fff; + else + res = sum; } #endif