From 1dbc6a09b78d170b6efeb8d9279c7057253db60f Mon Sep 17 00:00:00 2001
From: Petter Reinholdtsen <pere@debian.org>
Date: Sat, 8 Mar 2025 23:04:14 +0100
Subject: [PATCH] Adjusted OC_DCT_CW_PACK() for well defined C99 behaviour with
 negative _mag.

Instead of shifting the negative number, shift one bit and multiply
it with the negative number.

Fixes the following clang message:
../../lib/decode.c:195:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0,10, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:198:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0,  0,1),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:208:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 1, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:210:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 2, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:212:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 3, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:214:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 4, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:216:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 5, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:220:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 1, -2,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:221:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 1, -3,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:227:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 6, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:228:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 7, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:229:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 8, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:230:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 9, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:236:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 2, -2,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:237:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 3, -2,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:238:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 2, -3,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:239:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 3, -3,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:242:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0,  0,1),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:253:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -1,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:257:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -2,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:260:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -3,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:262:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -4,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:264:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -5,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:266:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -6,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:270:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -7,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:271:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -8,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:277:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0, -9,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:278:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0,-10,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:279:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0,-11,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^
../../lib/decode.c:280:3: warning: shifting a negative signed value is undefined [-Wshift-negative-value]
  OC_DCT_CW_PACK( 0, 0,-12,0),
  ^~~~~~~~~~~~~~~~~~~~~~~~~~~
../../lib/decode.c:150:16: note: expanded from macro 'OC_DCT_CW_PACK'
 (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
 ~~~~~~~~~~~~~~^

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

diff --git a/lib/decode.c b/lib/decode.c
index fad26e09..76d228ae 100644
--- a/lib/decode.c
+++ b/lib/decode.c
@@ -147,7 +147,7 @@ static const unsigned char OC_INTERNAL_DCT_TOKEN_EXTRA_BITS[15]={
  ((_eobs)<<OC_DCT_CW_EOB_SHIFT| \
  (_rlen)<<OC_DCT_CW_RLEN_SHIFT| \
  (_flip)<<OC_DCT_CW_FLIP_BIT| \
- (_mag)-(_flip)<<OC_DCT_CW_MAG_SHIFT)
+ ((_mag)-(_flip))*(1<<OC_DCT_CW_MAG_SHIFT))
 
 /*A special code word value that signals the end of the frame (a long EOB run
    of zero).*/
-- 
GitLab