Merge pull request #21 from kAworu/threshold-fix

Avoid division by zero.
This commit is contained in:
Daniel Beer 2016-10-05 07:48:40 +13:00 committed by GitHub
commit aeb9b88d1a

View file

@ -174,6 +174,7 @@ static void flood_fill_seed(struct quirc *q, int x, int y, int from, int to,
* Adaptive thresholding * Adaptive thresholding
*/ */
#define THRESHOLD_S_MIN 1
#define THRESHOLD_S_DEN 8 #define THRESHOLD_S_DEN 8
#define THRESHOLD_T 5 #define THRESHOLD_T 5
@ -185,6 +186,15 @@ static void threshold(struct quirc *q)
int threshold_s = q->w / THRESHOLD_S_DEN; int threshold_s = q->w / THRESHOLD_S_DEN;
quirc_pixel_t *row = q->pixels; quirc_pixel_t *row = q->pixels;
/*
* Ensure a sane, non-zero value for threshold_s.
*
* threshold_s can be zero if the image width is small. We need to avoid
* SIGFPE as it will be used as divisor.
*/
if (threshold_s < THRESHOLD_S_MIN)
threshold_s = THRESHOLD_S_MIN;
for (y = 0; y < q->h; y++) { for (y = 0; y < q->h; y++) {
int row_average[q->w]; int row_average[q->w];