fix some 'unsigned integer overflow' warnings in ubsan

I couldn't find a safe way of fixing VP8GetSigned() so i just
used the big-hammer.

Change-Id: I1039bc00307d1c90c85909a458a4bc70670e48b7
This commit is contained in:
skal 2016-08-16 15:02:43 -07:00 committed by James Zern
parent 8a4ebc6ab0
commit 6ab496ed22
11 changed files with 56 additions and 42 deletions

View File

@ -245,9 +245,9 @@ void VP8LAddGreenToBlueAndRed_C(uint32_t* data, int num_pixels) {
} }
} }
static WEBP_INLINE uint32_t ColorTransformDelta(int8_t color_pred, static WEBP_INLINE int ColorTransformDelta(int8_t color_pred,
int8_t color) { int8_t color) {
return (uint32_t)((int)(color_pred) * color) >> 5; return ((int)color_pred * color) >> 5;
} }
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code, static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
@ -264,8 +264,8 @@ void VP8LTransformColorInverse_C(const VP8LMultipliers* const m, uint32_t* data,
const uint32_t argb = data[i]; const uint32_t argb = data[i];
const uint32_t green = argb >> 8; const uint32_t green = argb >> 8;
const uint32_t red = argb >> 16; const uint32_t red = argb >> 16;
uint32_t new_red = red; int new_red = red;
uint32_t new_blue = argb; int new_blue = argb;
new_red += ColorTransformDelta(m->green_to_red_, green); new_red += ColorTransformDelta(m->green_to_red_, green);
new_red &= 0xff; new_red &= 0xff;
new_blue += ColorTransformDelta(m->green_to_blue_, green); new_blue += ColorTransformDelta(m->green_to_blue_, green);

View File

@ -342,14 +342,16 @@ static WEBP_INLINE void VP8LPrefixEncode(int distance, int* const code,
} }
// Sum of each component, mod 256. // Sum of each component, mod 256.
static WEBP_INLINE uint32_t VP8LAddPixels(uint32_t a, uint32_t b) { static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
uint32_t VP8LAddPixels(uint32_t a, uint32_t b) {
const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u); const uint32_t alpha_and_green = (a & 0xff00ff00u) + (b & 0xff00ff00u);
const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu); const uint32_t red_and_blue = (a & 0x00ff00ffu) + (b & 0x00ff00ffu);
return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu); return (alpha_and_green & 0xff00ff00u) | (red_and_blue & 0x00ff00ffu);
} }
// Difference of each component, mod 256. // Difference of each component, mod 256.
static WEBP_INLINE uint32_t VP8LSubPixels(uint32_t a, uint32_t b) { static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
uint32_t VP8LSubPixels(uint32_t a, uint32_t b) {
const uint32_t alpha_and_green = const uint32_t alpha_and_green =
0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u); 0x00ff00ffu + (a & 0xff00ff00u) - (b & 0xff00ff00u);
const uint32_t red_and_blue = const uint32_t red_and_blue =

View File

@ -922,11 +922,11 @@ void VP8LResidualImage(int width, int height, int bits, int low_effort,
void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels) { void VP8LSubtractGreenFromBlueAndRed_C(uint32_t* argb_data, int num_pixels) {
int i; int i;
for (i = 0; i < num_pixels; ++i) { for (i = 0; i < num_pixels; ++i) {
const uint32_t argb = argb_data[i]; const int argb = argb_data[i];
const uint32_t green = (argb >> 8) & 0xff; const int green = (argb >> 8) & 0xff;
const uint32_t new_r = (((argb >> 16) & 0xff) - green) & 0xff; const uint32_t new_r = (((argb >> 16) & 0xff) - green) & 0xff;
const uint32_t new_b = ((argb & 0xff) - green) & 0xff; const uint32_t new_b = (((argb >> 0) & 0xff) - green) & 0xff;
argb_data[i] = (argb & 0xff00ff00) | (new_r << 16) | new_b; argb_data[i] = (argb & 0xff00ff00u) | (new_r << 16) | new_b;
} }
} }
@ -936,9 +936,8 @@ static WEBP_INLINE void MultipliersClear(VP8LMultipliers* const m) {
m->red_to_blue_ = 0; m->red_to_blue_ = 0;
} }
static WEBP_INLINE uint32_t ColorTransformDelta(int8_t color_pred, static WEBP_INLINE int ColorTransformDelta(int8_t color_pred, int8_t color) {
int8_t color) { return ((int)color_pred * color) >> 5;
return (uint32_t)((int)(color_pred) * color) >> 5;
} }
static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code, static WEBP_INLINE void ColorCodeToMultipliers(uint32_t color_code,
@ -963,8 +962,8 @@ void VP8LTransformColor_C(const VP8LMultipliers* const m, uint32_t* data,
const uint32_t argb = data[i]; const uint32_t argb = data[i];
const uint32_t green = argb >> 8; const uint32_t green = argb >> 8;
const uint32_t red = argb >> 16; const uint32_t red = argb >> 16;
uint32_t new_red = red; int new_red = red;
uint32_t new_blue = argb; int new_blue = argb;
new_red -= ColorTransformDelta(m->green_to_red_, green); new_red -= ColorTransformDelta(m->green_to_red_, green);
new_red &= 0xff; new_red &= 0xff;
new_blue -= ColorTransformDelta(m->green_to_blue_, green); new_blue -= ColorTransformDelta(m->green_to_blue_, green);
@ -977,7 +976,7 @@ void VP8LTransformColor_C(const VP8LMultipliers* const m, uint32_t* data,
static WEBP_INLINE uint8_t TransformColorRed(uint8_t green_to_red, static WEBP_INLINE uint8_t TransformColorRed(uint8_t green_to_red,
uint32_t argb) { uint32_t argb) {
const uint32_t green = argb >> 8; const uint32_t green = argb >> 8;
uint32_t new_red = argb >> 16; int new_red = argb >> 16;
new_red -= ColorTransformDelta(green_to_red, green); new_red -= ColorTransformDelta(green_to_red, green);
return (new_red & 0xff); return (new_red & 0xff);
} }

View File

@ -211,13 +211,13 @@ void VP8LHashChainClear(VP8LHashChain* const p) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#define HASH_MULTIPLIER_HI (0xc6a4a793U) #define HASH_MULTIPLIER_HI (0xc6a4a793ULL)
#define HASH_MULTIPLIER_LO (0x5bd1e996U) #define HASH_MULTIPLIER_LO (0x5bd1e996ULL)
static WEBP_INLINE uint32_t GetPixPairHash64(const uint32_t* const argb) { static WEBP_INLINE uint32_t GetPixPairHash64(const uint32_t* const argb) {
uint32_t key; uint32_t key;
key = argb[1] * HASH_MULTIPLIER_HI; key = (argb[1] * HASH_MULTIPLIER_HI) & 0xffffffffu;
key += argb[0] * HASH_MULTIPLIER_LO; key += (argb[0] * HASH_MULTIPLIER_LO) & 0xffffffffu;
key = key >> (32 - HASH_BITS); key = key >> (32 - HASH_BITS);
return key; return key;
} }

View File

@ -545,8 +545,8 @@ static VP8LHistogram* HistogramCombineEntropyBin(
return cur_combo; return cur_combo;
} }
static uint32_t MyRand(uint32_t *seed) { static uint32_t MyRand(uint32_t* const seed) {
*seed *= 16807U; *seed = (*seed * 16807ull) & 0xffffffffu;
if (*seed == 0) { if (*seed == 0) {
*seed = 1; *seed = 1;
} }

View File

@ -163,18 +163,25 @@ typedef enum {
kHistoTotal // Must be last. kHistoTotal // Must be last.
} HistoIx; } HistoIx;
static void AddSingleSubGreen(uint32_t p, uint32_t* r, uint32_t* b) { static void AddSingleSubGreen(int p, uint32_t* const r, uint32_t* const b) {
const uint32_t green = p >> 8; // The upper bits are masked away later. const int green = p >> 8; // The upper bits are masked away later.
++r[((p >> 16) - green) & 0xff]; ++r[((p >> 16) - green) & 0xff];
++b[(p - green) & 0xff]; ++b[((p >> 0) - green) & 0xff];
} }
static void AddSingle(uint32_t p, static void AddSingle(uint32_t p,
uint32_t* a, uint32_t* r, uint32_t* g, uint32_t* b) { uint32_t* const a, uint32_t* const r,
++a[p >> 24]; uint32_t* const g, uint32_t* const b) {
++a[(p >> 24) & 0xff];
++r[(p >> 16) & 0xff]; ++r[(p >> 16) & 0xff];
++g[(p >> 8) & 0xff]; ++g[(p >> 8) & 0xff];
++b[(p & 0xff)]; ++b[(p >> 0) & 0xff];
}
static WEBP_INLINE uint32_t HashPix(uint32_t pix) {
// Note that masking with 0xffffffffu is for preventing an
// 'unsigned int overflow' warning. Doesn't impact the compiled code.
return ((((uint64_t)pix + (pix >> 19)) * 0x39c5fba7ull) & 0xffffffffu) >> 24;
} }
static int AnalyzeEntropy(const uint32_t* argb, static int AnalyzeEntropy(const uint32_t* argb,
@ -214,8 +221,8 @@ static int AnalyzeEntropy(const uint32_t* argb,
&histo[kHistoBluePredSubGreen * 256]); &histo[kHistoBluePredSubGreen * 256]);
{ {
// Approximate the palette by the entropy of the multiplicative hash. // Approximate the palette by the entropy of the multiplicative hash.
const int hash = ((pix + (pix >> 19)) * 0x39c5fba7) >> 24; const uint32_t hash = HashPix(pix);
++histo[kHistoPalette * 256 + (hash & 0xff)]; ++histo[kHistoPalette * 256 + hash];
} }
} }
prev_row = curr_row; prev_row = curr_row;

View File

@ -149,7 +149,8 @@ static WEBP_INLINE int VP8GetBit(VP8BitReader* const br, int prob) {
} }
// simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here) // simplified version of VP8GetBit() for prob=0x80 (note shift is always 1 here)
static WEBP_INLINE int VP8GetSigned(VP8BitReader* const br, int v) { static WEBP_UBSAN_IGNORE_UNSIGNED_OVERFLOW WEBP_INLINE
int VP8GetSigned(VP8BitReader* const br, int v) {
if (br->bits_ < 0) { if (br->bits_ < 0) {
VP8LoadNewBytes(br); VP8LoadNewBytes(br);
} }

View File

@ -54,7 +54,8 @@ int VP8BitWriterAppend(VP8BitWriter* const bw,
// return approximate write position (in bits) // return approximate write position (in bits)
static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) { static WEBP_INLINE uint64_t VP8BitWriterPos(const VP8BitWriter* const bw) {
return (uint64_t)(bw->pos_ + bw->run_) * 8 + 8 + bw->nb_bits_; const uint64_t nb_bits = 8 + bw->nb_bits_; // bw->nb_bits_ is <= 0, note
return (bw->pos_ + bw->run_) * 8 + nb_bits;
} }
// Returns a pointer to the internal buffer. // Returns a pointer to the internal buffer.

View File

@ -28,7 +28,11 @@ typedef struct {
int hash_bits_; int hash_bits_;
} VP8LColorCache; } VP8LColorCache;
static const uint32_t kHashMul = 0x1e35a7bd; static const uint64_t kHashMul = 0x1e35a7bdull;
static WEBP_INLINE int HashPix(uint32_t argb, int shift) {
return (int)(((argb * kHashMul) & 0xffffffffu) >> shift);
}
static WEBP_INLINE uint32_t VP8LColorCacheLookup( static WEBP_INLINE uint32_t VP8LColorCacheLookup(
const VP8LColorCache* const cc, uint32_t key) { const VP8LColorCache* const cc, uint32_t key) {
@ -44,20 +48,20 @@ static WEBP_INLINE void VP8LColorCacheSet(const VP8LColorCache* const cc,
static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc, static WEBP_INLINE void VP8LColorCacheInsert(const VP8LColorCache* const cc,
uint32_t argb) { uint32_t argb) {
const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; const int key = HashPix(argb, cc->hash_shift_);
cc->colors_[key] = argb; cc->colors_[key] = argb;
} }
static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc, static WEBP_INLINE int VP8LColorCacheGetIndex(const VP8LColorCache* const cc,
uint32_t argb) { uint32_t argb) {
return (kHashMul * argb) >> cc->hash_shift_; return HashPix(argb, cc->hash_shift_);
} }
// Return the key if cc contains argb, and -1 otherwise. // Return the key if cc contains argb, and -1 otherwise.
static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
uint32_t argb) { uint32_t argb) {
const uint32_t key = (kHashMul * argb) >> cc->hash_shift_; const int key = HashPix(argb, cc->hash_shift_);
return (cc->colors_[key] == argb) ? (int)key : -1; return (cc->colors_[key] == argb) ? key : -1;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------

View File

@ -45,7 +45,7 @@ static WEBP_INLINE uint32_t GetNextKey(uint32_t key, int len) {
while (key & step) { while (key & step) {
step >>= 1; step >>= 1;
} }
return (key & (step - 1)) + step; return step ? (key & (step - 1)) + step : key;
} }
// Stores code in table[0], table[step], table[2*step], ..., table[end]. // Stores code in table[0], table[step], table[2*step], ..., table[end].

View File

@ -248,7 +248,7 @@ int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {
int num_colors = 0; int num_colors = 0;
uint8_t in_use[COLOR_HASH_SIZE] = { 0 }; uint8_t in_use[COLOR_HASH_SIZE] = { 0 };
uint32_t colors[COLOR_HASH_SIZE]; uint32_t colors[COLOR_HASH_SIZE];
static const uint32_t kHashMul = 0x1e35a7bdU; static const uint64_t kHashMul = 0x1e35a7bdull;
const uint32_t* argb = pic->argb; const uint32_t* argb = pic->argb;
const int width = pic->width; const int width = pic->width;
const int height = pic->height; const int height = pic->height;
@ -263,7 +263,7 @@ int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {
continue; continue;
} }
last_pix = argb[x]; last_pix = argb[x];
key = (kHashMul * last_pix) >> COLOR_HASH_RIGHT_SHIFT; key = ((last_pix * kHashMul) & 0xffffffffu) >> COLOR_HASH_RIGHT_SHIFT;
while (1) { while (1) {
if (!in_use[key]) { if (!in_use[key]) {
colors[key] = last_pix; colors[key] = last_pix;