From cd82440ec714979543a5b5351f4ec96a467ee0de Mon Sep 17 00:00:00 2001 From: James Zern Date: Wed, 16 Sep 2015 23:54:29 -0700 Subject: [PATCH] VP8LAllocateHistogramSet: align histogram[] entries fixes issue #262: a SIGBUS when accessing a misaligned double in VP8LHistogram Change-Id: Ic78cc5366d7e43d892c375b6a69dce2379db931b --- src/enc/histogram.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/enc/histogram.c b/src/enc/histogram.c index 98a6e80d..15aea271 100644 --- a/src/enc/histogram.c +++ b/src/enc/histogram.c @@ -20,6 +20,9 @@ #include "../dsp/lossless.h" #include "../utils/utils.h" +#define ALIGN_CST 15 +#define DO_ALIGN(PTR) ((uintptr_t)((PTR) + ALIGN_CST) & ~ALIGN_CST) + #define MAX_COST 1.e38 // Number of partitions for the three dominant (literal, red and blue) symbol @@ -110,9 +113,9 @@ VP8LHistogram* VP8LAllocateHistogram(int cache_bits) { VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { int i; VP8LHistogramSet* set; - const size_t total_size = sizeof(*set) - + sizeof(*set->histograms) * size - + (size_t)VP8LGetHistogramSize(cache_bits) * size; + const int histo_size = VP8LGetHistogramSize(cache_bits); + const size_t total_size = + sizeof(*set) + size * (sizeof(*set->histograms) + histo_size + ALIGN_CST); uint8_t* memory = (uint8_t*)WebPSafeMalloc(total_size, sizeof(*memory)); if (memory == NULL) return NULL; @@ -123,12 +126,12 @@ VP8LHistogramSet* VP8LAllocateHistogramSet(int size, int cache_bits) { set->max_size = size; set->size = size; for (i = 0; i < size; ++i) { + memory = (uint8_t*)DO_ALIGN(memory); set->histograms[i] = (VP8LHistogram*)memory; // literal_ won't necessary be aligned. set->histograms[i]->literal_ = (uint32_t*)(memory + sizeof(VP8LHistogram)); VP8LHistogramInit(set->histograms[i], cache_bits); - // There's no padding/alignment between successive histograms. - memory += VP8LGetHistogramSize(cache_bits); + memory += histo_size; } return set; }