Merge "Don't dereference NULL, ensure HashChain fully initialized"

This commit is contained in:
James Zern 2014-02-03 22:58:37 -08:00 committed by Gerrit Code Review
commit 0c7cc4ca20
2 changed files with 16 additions and 13 deletions

View File

@ -113,11 +113,16 @@ static WEBP_INLINE uint64_t GetPixPairHash64(const uint32_t* const argb) {
return key; return key;
} }
static int HashChainInit(HashChain* const p, int size) { static HashChain* HashChainNew(int size) {
int i; int i;
HashChain* const p = (HashChain*)malloc(sizeof(*p));
if (p == NULL) {
return NULL;
}
p->chain_ = (int*)WebPSafeMalloc((uint64_t)size, sizeof(*p->chain_)); p->chain_ = (int*)WebPSafeMalloc((uint64_t)size, sizeof(*p->chain_));
if (p->chain_ == NULL) { if (p->chain_ == NULL) {
return 0; free(p);
return NULL;
} }
for (i = 0; i < size; ++i) { for (i = 0; i < size; ++i) {
p->chain_[i] = -1; p->chain_[i] = -1;
@ -125,7 +130,7 @@ static int HashChainInit(HashChain* const p, int size) {
for (i = 0; i < HASH_SIZE; ++i) { for (i = 0; i < HASH_SIZE; ++i) {
p->hash_to_first_index_[i] = -1; p->hash_to_first_index_[i] = -1;
} }
return 1; return p;
} }
static void HashChainDelete(HashChain* const p) { static void HashChainDelete(HashChain* const p) {
@ -278,7 +283,7 @@ static int BackwardReferencesHashChain(int xsize, int ysize,
int cc_init = 0; int cc_init = 0;
const int use_color_cache = (cache_bits > 0); const int use_color_cache = (cache_bits > 0);
const int pix_count = xsize * ysize; const int pix_count = xsize * ysize;
HashChain* const hash_chain = (HashChain*)malloc(sizeof(*hash_chain)); HashChain* const hash_chain = HashChainNew(pix_count);
VP8LColorCache hashers; VP8LColorCache hashers;
int window_size = WINDOW_SIZE; int window_size = WINDOW_SIZE;
int iter_pos = 1; int iter_pos = 1;
@ -290,7 +295,6 @@ static int BackwardReferencesHashChain(int xsize, int ysize,
if (!cc_init) goto Error; if (!cc_init) goto Error;
} }
if (!HashChainInit(hash_chain, pix_count)) goto Error;
refs->size = 0; refs->size = 0;
GetParamsForHashChainFindCopy(quality, xsize, cache_bits, GetParamsForHashChainFindCopy(quality, xsize, cache_bits,
@ -485,7 +489,7 @@ static int BackwardReferencesHashChainDistanceOnly(
float* const cost = float* const cost =
(float*)WebPSafeMalloc((uint64_t)pix_count, sizeof(*cost)); (float*)WebPSafeMalloc((uint64_t)pix_count, sizeof(*cost));
CostModel* cost_model = (CostModel*)malloc(sizeof(*cost_model)); CostModel* cost_model = (CostModel*)malloc(sizeof(*cost_model));
HashChain* hash_chain = (HashChain*)malloc(sizeof(*hash_chain)); HashChain* hash_chain = HashChainNew(pix_count);
VP8LColorCache hashers; VP8LColorCache hashers;
const double mul0 = (recursive_cost_model != 0) ? 1.0 : 0.68; const double mul0 = (recursive_cost_model != 0) ? 1.0 : 0.68;
const double mul1 = (recursive_cost_model != 0) ? 1.0 : 0.82; const double mul1 = (recursive_cost_model != 0) ? 1.0 : 0.82;
@ -496,8 +500,6 @@ static int BackwardReferencesHashChainDistanceOnly(
if (cost == NULL || cost_model == NULL || hash_chain == NULL) goto Error; if (cost == NULL || cost_model == NULL || hash_chain == NULL) goto Error;
if (!HashChainInit(hash_chain, pix_count)) goto Error;
if (use_color_cache) { if (use_color_cache) {
cc_init = VP8LColorCacheInit(&hashers, cache_bits); cc_init = VP8LColorCacheInit(&hashers, cache_bits);
if (!cc_init) goto Error; if (!cc_init) goto Error;
@ -633,12 +635,11 @@ static int BackwardReferencesHashChainFollowChosenPath(
int window_size = WINDOW_SIZE; int window_size = WINDOW_SIZE;
int iter_pos = 1; int iter_pos = 1;
int iter_limit = -1; int iter_limit = -1;
HashChain* hash_chain = (HashChain*)malloc(sizeof(*hash_chain)); HashChain* hash_chain = HashChainNew(pix_count);
VP8LColorCache hashers; VP8LColorCache hashers;
if (hash_chain == NULL || !HashChainInit(hash_chain, pix_count)) { if (hash_chain == NULL) goto Error;
goto Error;
}
if (use_color_cache) { if (use_color_cache) {
cc_init = VP8LColorCacheInit(&hashers, cache_bits); cc_init = VP8LColorCacheInit(&hashers, cache_bits);
if (!cc_init) goto Error; if (!cc_init) goto Error;

View File

@ -959,7 +959,9 @@ static VP8LEncoder* VP8LEncoderNew(const WebPConfig* const config,
} }
static void VP8LEncoderDelete(VP8LEncoder* enc) { static void VP8LEncoderDelete(VP8LEncoder* enc) {
free(enc->argb_); if (enc != NULL) {
free(enc->argb_);
}
free(enc); free(enc);
} }