Fix negative shift left reported by UBSan (#758)
authorMatthieu Darbois <mayeut@users.noreply.github.com>
Wed, 27 Apr 2016 19:56:09 +0000 (21:56 +0200)
committerMatthieu Darbois <mayeut@users.noreply.github.com>
Wed, 27 Apr 2016 19:56:09 +0000 (21:56 +0200)
Follow-up of #757

This shall have no performance impact on 2’s complement machine where
the compiler replaces the multiplication by power of two (constant) by
a left shift.
Verified at least on MacOS Xcode 7.3, same assembly generated after fix.

src/lib/openjp2/t1.c
src/lib/openjp2/tcd.c

index 8d020bb52911845ce5af898806e9b5c1b3d63fb0..a3ca93e8f6c67caf1c542d1c8e0ec975a8dc52f3 100644 (file)
@@ -1514,7 +1514,7 @@ OPJ_BOOL opj_t1_encode_cblks(   opj_t1_t *t1,
                                                if (tccp->qmfbid == 1) {
                                                        for (j = 0; j < cblk_h; ++j) {
                                                                for (i = 0; i < cblk_w; ++i) {
-                                                                       tiledp[tileIndex] *= 1 << T1_NMSEDEC_FRACBITS;
+                                                                       tiledp[tileIndex] *= (1 << T1_NMSEDEC_FRACBITS);
                                                                        tileIndex++;
                                                                }
                                                                tileIndex += tileLineAdvance;
index 6eeb211e2f505a51678472d535cfdbceef4f9173..af6b53f5c9e97a6e7c81aae0472036ca7ffdf9af 100644 (file)
@@ -1911,7 +1911,7 @@ static OPJ_BOOL opj_tcd_dc_level_shift_encode ( opj_tcd_t *p_tcd )
                 }
                 else {
                         for (i = 0; i < l_nb_elem; ++i) {
-                                *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) << 11 ;
+                                *l_current_ptr = (*l_current_ptr - l_tccp->m_dc_level_shift) * (1 << 11);
                                 ++l_current_ptr;
                         }
                 }