From 80fca667e285e29070abf19630d7c2cf1b75d749 Mon Sep 17 00:00:00 2001 From: heckflosse Date: Wed, 29 Nov 2017 23:06:53 +0100 Subject: [PATCH] Fix crash bug in raw ca correction when width is odd, also fix two overruns detected by adress sanitizer, fixes #4202 --- rtengine/CA_correct_RT.cc | 14 +++++++------- rtengine/amaze_demosaic_RT.cc | 10 ++++++++-- rtengine/demosaic_algos.cc | 3 ++- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/rtengine/CA_correct_RT.cc b/rtengine/CA_correct_RT.cc index 59fd804f1..bfd56cc0a 100644 --- a/rtengine/CA_correct_RT.cc +++ b/rtengine/CA_correct_RT.cc @@ -218,18 +218,18 @@ void RawImageSource::CA_correct_RT(const bool autoCA, const double cared, const // Main algorithm: Tile loop calculating correction parameters per tile #pragma omp for collapse(2) schedule(dynamic) nowait for (int top = -border ; top < height; top += ts - border2) - for (int left = -border; left < width; left += ts - border2) { + for (int left = -border; left < width - (W & 1); left += ts - border2) { memset(buffer, 0, buffersize); const int vblock = ((top + border) / (ts - border2)) + 1; const int hblock = ((left + border) / (ts - border2)) + 1; const int bottom = min(top + ts, height + border); - const int right = min(left + ts, width + border); + const int right = min(left + ts, width - (W & 1) + border); const int rr1 = bottom - top; const int cc1 = right - left; const int rrmin = top < 0 ? border : 0; const int rrmax = bottom > height ? height - top : rr1; const int ccmin = left < 0 ? border : 0; - const int ccmax = right > width ? width - left : cc1; + const int ccmax = (right > width - (W & 1)) ? width - (W & 1) - left : cc1; // rgb from input CFA data // rgb values should be floating point numbers between 0 and 1 @@ -755,20 +755,20 @@ void RawImageSource::CA_correct_RT(const bool autoCA, const double cared, const #pragma omp for schedule(dynamic) collapse(2) nowait for (int top = -border; top < height; top += ts - border2) - for (int left = -border; left < width; left += ts - border2) { + for (int left = -border; left < width - (W & 1); left += ts - border2) { memset(buffer, 0, buffersize); float lblockshifts[2][2]; const int vblock = ((top + border) / (ts - border2)) + 1; const int hblock = ((left + border) / (ts - border2)) + 1; const int bottom = min(top + ts, height + border); - const int right = min(left + ts, width + border); + const int right = min(left + ts, width - (W & 1) + border); const int rr1 = bottom - top; const int cc1 = right - left; const int rrmin = top < 0 ? border : 0; const int rrmax = bottom > height ? height - top : rr1; const int ccmin = left < 0 ? border : 0; - const int ccmax = right > width ? width - left : cc1; + const int ccmax = (right > width - (W & 1)) ? width - (W & 1) - left : cc1; // rgb from input CFA data // rgb values should be floating point number between 0 and 1 @@ -1145,7 +1145,7 @@ void RawImageSource::CA_correct_RT(const bool autoCA, const double cared, const STC2VFU(rawData[row][col], LVFU(RawDataTmp[indx])); } #endif - for(; col < width; col += 2, indx++) { + for(; col < width - (W & 1); col += 2, indx++) { rawData[row][col] = RawDataTmp[indx]; } } diff --git a/rtengine/amaze_demosaic_RT.cc b/rtengine/amaze_demosaic_RT.cc index 3cb0ee8de..fdd51d92a 100644 --- a/rtengine/amaze_demosaic_RT.cc +++ b/rtengine/amaze_demosaic_RT.cc @@ -223,13 +223,19 @@ SSEFUNCTION void RawImageSource::amaze_demosaic_RT(int winx, int winy, int winw, // fill inner part for (int rr = rrmin; rr < rrmax; rr++) { int row = rr + top; - - for (int cc = ccmin; cc < ccmax; cc += 4) { + int cc = ccmin; + for (; cc < ccmax - 3; cc += 4) { int indx1 = rr * ts + cc; vfloat tempv = LVFU(rawData[row][cc + left]) / c65535v; STVF(cfa[indx1], tempv ); STVF(rgbgreen[indx1], tempv ); } + for (; cc < ccmax; ++cc) { + int indx1 = rr * ts + cc; + float temp = rawData[row][cc + left] / 65535.f; + cfa[indx1] = temp; + rgbgreen[indx1] = temp; + } } //fill lower border diff --git a/rtengine/demosaic_algos.cc b/rtengine/demosaic_algos.cc index 88d369969..1677b785f 100644 --- a/rtengine/demosaic_algos.cc +++ b/rtengine/demosaic_algos.cc @@ -2029,7 +2029,8 @@ SSEFUNCTION void RawImageSource::igv_interpolate(int winw, int winh) for (; col < width; col++, indx += 2) { dest1[indx >> 1] = CLIP(rawData[row][col]); //rawData = RT datas col++; - dest2[indx >> 1] = CLIP(rawData[row][col]); //rawData = RT datas + if(col < width) + dest2[indx >> 1] = CLIP(rawData[row][col]); //rawData = RT datas } }