From 026013546cd889986b8395deae43b9793f4471f6 Mon Sep 17 00:00:00 2001 From: Fabian Giesen Date: Fri, 2 Jul 2021 20:59:22 -0700 Subject: [PATCH] stb_image: Avoid shift of signed values in extend_receive Use an equivalent formulation that has sgn=0 or 1, not 0 or -1. This avoids right-shifting signed values, at least in this place. Fixes issue #1061. --- stb_image.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stb_image.h b/stb_image.h index 8ac86b6..5dd9f32 100644 --- a/stb_image.h +++ b/stb_image.h @@ -2102,12 +2102,12 @@ stbi_inline static int stbi__extend_receive(stbi__jpeg *j, int n) int sgn; if (j->code_bits < n) stbi__grow_buffer_unsafe(j); - sgn = (stbi__int32)j->code_buffer >> 31; // sign bit is always in MSB + sgn = j->code_buffer >> 31; // sign bit always in MSB; 0 if MSB clear (positive), 1 if MSB set (negative) k = stbi_lrot(j->code_buffer, n); j->code_buffer = k & ~stbi__bmask[n]; k &= stbi__bmask[n]; j->code_bits -= n; - return k + (stbi__jbias[n] & ~sgn); + return k + (stbi__jbias[n] & (sgn - 1)); } // get some unsigned bits