Hi Manuel, Here's the blend_highlights() function from dcraw.c with just the includes, defines, and globals it needs to compile. gcc -Wall -O4 -march=athlon64 foo.c gives two "array bounds" warnings, gcc -Wall -O4 -march=x86-64 foo.c gcc -Wall -O4 foo.c gcc -Wall -O3 foo.c give one warning, while "-O2" or less gives no warnings. Unlike -Wunused-result and -Waggressive-loop-optimizations, -Warray-bound is not enabled by default, only because I use -Wall. Dave Coffin 3/2/2015 On Mon, Mar 02, 2015 at 12:10:37PM +0100, Manuel López-Ibáñez wrote: > On 1 March 2015 at 21:03, <dcoffin@xxxxxxxxxxxxxxxxxx> wrote: > > I fixed all the "undefined behavior" warnings with > > typecasts, but I'm still getting array-bounds warnings > > with manifestly correct code. E.g. when blend_highlights() > > uses the global variable "colors": > > > > static const float trans[2][4][4] = { ... } > > ... > > if ((unsigned) (colors-3) > 1) return; > > ... > > lab[i][c] += trans[colors-3][c][j] * cam[i][j]; > > > > How could "colors-3" be out of bounds? I just verified > > that it's either 0 or 1, and if I hard-code 0 or 1 the warning > > goes away. > > Could you post a small complete example? It doesn't need to have a > main() function and it is better if it doesn't use any header files. > > Cheers, > > Manuel.
#include <stdio.h> #include <limits.h> #include <math.h> #if !defined(ushort) #define ushort unsigned short #endif #define FORC(cnt) for (c=0; c < cnt; c++) #define FORCC FORC(colors) #define MIN(a,b) ((a) < (b) ? (a) : (b)) #define SQR(x) ((x)*(x)) #define _(String) (String) ushort (*image)[4], height, width; unsigned colors; float pre_mul[4]; int verbose=0; void blend_highlights() { int clip=INT_MAX, row, col, c, i, j; static const float trans[2][4][4] = { { { 1,1,1 }, { 1.7320508,-1.7320508,0 }, { -1,-1,2 } }, { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } }; static const float itrans[2][4][4] = { { { 1,0.8660254,-0.5 }, { 1,-0.8660254,-0.5 }, { 1,0,1 } }, { { 1,1,1,1 }, { 1,-1,1,-1 }, { 1,1,-1,-1 }, { 1,-1,-1,1 } } }; float cam[2][4], lab[2][4], sum[2], chratio; if ((unsigned) (colors-3) > 1) return; if (verbose) fprintf (stderr,_("Blending highlights...\n")); FORCC if (clip > (i = 65535*pre_mul[c])) clip = i; for (row=0; row < height; row++) for (col=0; col < width; col++) { FORCC if (image[row*width+col][c] > clip) break; if (c == colors) continue; FORCC { cam[0][c] = image[row*width+col][c]; cam[1][c] = MIN(cam[0][c],clip); } for (i=0; i < 2; i++) { FORCC for (lab[i][c]=j=0; j < colors; j++) lab[i][c] += trans[colors-3][c][j] * cam[i][j]; for (sum[i]=0,c=1; c < colors; c++) sum[i] += SQR(lab[i][c]); } chratio = sqrt(sum[1]/sum[0]); for (c=1; c < colors; c++) lab[0][c] *= chratio; FORCC for (cam[0][c]=j=0; j < colors; j++) cam[0][c] += itrans[colors-3][c][j] * lab[0][j]; FORCC image[row*width+col][c] = cam[0][c] / colors; } }