libFLAC/bitwriter.c: Fix undefined behaviour
authorErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 22 Aug 2015 22:08:27 +0000 (08:08 +1000)
committerErik de Castro Lopo <erikd@mega-nerd.com>
Sat, 22 Aug 2015 22:51:44 +0000 (08:51 +1000)
Improve folding of signed to unsgned to avoid UB.

src/libFLAC/bitwriter.c

index 76be1bc..dcdd93e 100644 (file)
@@ -520,7 +520,9 @@ FLAC__bool FLAC__bitwriter_write_rice_signed_block(FLAC__BitWriter *bw, const FL
 
        while(nvals) {
                /* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
-               uval = (*vals<<1) ^ (*vals>>31);
+               uval = *vals;
+               uval <<= 1;
+               uval ^= (*vals>>31);
 
                msbits = uval >> parameter;