Compiler warnings are your future errors

Author: Wojciech Muła
Added on:2015-03-22

Months ago I was asked to upgrade GCC from version 4.7 to 4.9 and also cleanup configure scripts. Not very exciting, merely time consuming task. Oh, we were hit by a bug in libstdc++, but simple patch has fixed the problem. Few weeks later I was asked to change GCC switch from -std=c++11 to -std=c++14 — the easiest task in the world. I had to modify single script, run configure, type make, then run tests... everything was OK. Quite boring so far.

Then my coworker noticed that after the last change unit tests started to randomly crash or hang. However, nobody else complained and continuous integration system worked fine. Wait, just a simple change in a compiler flag would cause such problems? I simply rejected this reason, but to be sure for 100% I had done a clean build, run tests and... nothing wrong happened. The conclusion was obvious for me: there is some bug in a coworker branch, and when he merge with the clean master everything will be OK.

Recently I've written a very simple console application that use our core modules. The application always hung, so I had started to investigate problem and finally figured the reason. My colleague was right, problem was introduced by the new compiler's flag.

There was an issue with variable-length arrays (VLA), which are used extensively in a one of core subsystem. Consider this simple program:

// test.cpp
#include

int size(int n) {
    char array[n];
    return sizeof(array);
}

int main() {
    printf("%d\n", size(5));
}

When compiled with g++ -std=c++11 size.cpp the program prints 5 — this is perfectly OK. But when we change flag to -std=c++14 the program prints 1 and the compiler displays following warning (output from GCC 4.9.1 and 4.9.2):

size.cpp: In function ‘int size(int)’:
size.cpp:7:24: warning: taking sizeof array of runtime bound [-Wvla]
     return sizeof(array);

Weird? Yes and no. GCC implemented proposal N3639 for C++14 ("Runtime-sized arrays with automatic storage duration") where sizeof(VLA) is an error ("ill-formed"), but as we see authors have chosen a soft warning rather a compile error. BTW, N3639 was rejected and GCC also removed support for this proposal.

Lesson learned

Because we always have very large build logs I didn't notice the new warning.

In order to prevent such errors in the future I've written a script that extracts all warnings from the logs and prints them in a easy-to-read form. I also fight with warnings in so called spare time.