From 15acdb8e79fbd8e0456ffe913c207deb39443a88 Mon Sep 17 00:00:00 2001 From: Petter Reinholdtsen <pere@debian.org> Date: Sun, 9 Mar 2025 14:16:12 +0100 Subject: [PATCH] Avoided undefined bit shifting of signed value. Use multiplication instead, allowing the compiler to optimize to bitshifts if it believe it to be safe. Fixes a few issues discovered by --enable-gcc-sanitize-address in !31. --- lib/mathops.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/mathops.c b/lib/mathops.c index 23c8f6e1..aadaa22a 100644 --- a/lib/mathops.c +++ b/lib/mathops.c @@ -160,7 +160,7 @@ ogg_int64_t oc_bexp64(ogg_int64_t _z){ /*z is the fractional part of the log in Q62 format. We need 1 bit of headroom since the magnitude can get larger than 1 during the iteration, and a sign bit.*/ - z<<=5; + z*=32; /*w is the exponential in Q61 format (since it also needs headroom and can get as large as 2.0); we could get another bit if we dropped the sign, but we'll recover that bit later anyway. @@ -175,7 +175,7 @@ ogg_int64_t oc_bexp64(ogg_int64_t _z){ z-=OC_ATANH_LOG2[i]+mask^mask; /*Repeat iteration 4.*/ if(i>=3)break; - z<<=1; + z*=2; } for(;;i++){ mask=-(z<0); @@ -183,12 +183,12 @@ ogg_int64_t oc_bexp64(ogg_int64_t _z){ z-=OC_ATANH_LOG2[i]+mask^mask; /*Repeat iteration 13.*/ if(i>=12)break; - z<<=1; + z*=2; } for(;i<32;i++){ mask=-(z<0); w+=(w>>i+1)+mask^mask; - z=z-(OC_ATANH_LOG2[i]+mask^mask)<<1; + z=(z-(OC_ATANH_LOG2[i]+mask^mask))*2; } wlo=0; /*Skip the remaining iterations unless we really require that much @@ -207,12 +207,12 @@ ogg_int64_t oc_bexp64(ogg_int64_t _z){ z-=OC_ATANH_LOG2[31]+mask^mask; /*Repeat iteration 40.*/ if(i>=39)break; - z<<=1; + z*=2; } for(;i<61;i++){ mask=-(z<0); wlo+=(w>>i)+mask^mask; - z=z-(OC_ATANH_LOG2[31]+mask^mask)<<1; + z=(z-(OC_ATANH_LOG2[31]+mask^mask))*2; } } w=(w<<1)+wlo; -- GitLab