From 3afddb5a00abf3a0656a90d83f0ce8ccffb7811d Mon Sep 17 00:00:00 2001
From: Petter Reinholdtsen <pere@debian.org>
Date: Sat, 8 Mar 2025 11:03:12 +0100
Subject: [PATCH] Avoid undefined shifting of negative number in rate.c.

It is undefined what left-shifting a negative number should do, and the
compiler do not like trying to shift the -64 value 57 bits to the left.
Testing show that the value of both OC_Q57(64) and OC_Q57(-64) are
0x8000000000000000, which suprised me a bit.  Given that the former do
not produce a compiler warning about undefined behaviour, and assuming
that the value generated by GCC is the desired one, I provide this
change to get rid of the indefined behavior.

This get rid of this clang message:

>  ../../lib/rate.c:746:15: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
>      log_scale=OC_Q57(-64);
>                ^~~~~~~~~~~
>  ../../lib/mathops.h:134:38: note: expanded from macro 'OC_Q57'
>  #define OC_Q57(_v) ((ogg_int64_t)(_v)<<57)
>                      ~~~~~~~~~~~~~~~~~^

Fixes #2321
---
 lib/rate.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/rate.c b/lib/rate.c
index bf2b1396..efc0235a 100644
--- a/lib/rate.c
+++ b/lib/rate.c
@@ -743,7 +743,7 @@ int oc_enc_update_rc_state(oc_enc_ctx *_enc,
   buf_delta=_enc->rc.bits_per_frame*(1+_enc->dup_count);
   if(_bits<=0){
     /*We didn't code any blocks in this frame.*/
-    log_scale=OC_Q57(-64);
+    log_scale=OC_Q57(64);
     _bits=0;
   }
   else{
-- 
GitLab