News for hackers. Stuff that matters.
Re:Results of the 2015 Underhanded C Contest by asgfoi on Thursday February 04, 2016 @05:37AM (#11033087)
I wonder how many of them would be caught using a very strict compiler flag regime.

For the winner, perhaps the gcc flag, -Wmissing-prototypes would catch it?

Re:Results of the 2015 Underhanded C Contest by nkurz on Thursday February 04, 2016 @06:14AM (#11033231)
No, that doesn't catch it because "float_t" is properly defined as a 'float' by including <math.h>, instead of a 'double' in the local header. But I thought you might be on to something with adding extra warnings, so I tried it out on http://gcc.godbolt.org.

Nope, no warnings at all for spectral_contrast.c with GCC, ICC, or Clang even with "-Wall -Wextra -pedantic". Then I thought to try it on MSVC at http://webcompiler.cloudapp.net/. To my surprise, it caught it pretty clearly with /W4:

  main.cpp(11): warning C4244: '/=': conversion from 'double' 
                to 'float_t', possible loss of data
With that hint, I searched for other GCC warnings and found -Wconversion. Indeed, with that (or -Wfloat-conversion) GCC picks up the scent pretty well: http://goo.gl/9xq3fG

   In function 'void normalize(float_t*, int)':
   11 : warning: conversion to 'float_t {aka float}' from 
        'double' may alter its value [-Wfloat-conversion]
ICC gives an excellent error message with -Wconversion also, perhaps the clearest of the bunch: http://goo.gl/cjXLjq

   warning #2259: non-pointer conversion from "double" to 
                  "float_t={float}" may lose significant bits
       for(i = 0; i < length; i++) v[i] /= magnitude;
Clang remained silent with all the options I tried, but perhaps I missed the right one.
Re:Results of the 2015 Underhanded C Contest by asgfoi on Thursday February 04, 2016 @08:13AM (#11033675)
I tried to compile it, and I also couldn't find any other flag, than -Wconversion, that would detect this:

  for(i = 0; i < length; i++) sum += a[i] * b[i];
                                     ^
I managed to hide the error if return values, which are double, are replaced with float_t. I believe in that case the bug stays in, but -Wconversion doesn't detect it.

Of course the warning can always be silenced by doing something like:

  for(i = 0; i < length; i++) sum += ( double )( a[i] * b[i] );
and adding a misleading comment about the explicit cast.
Re:Results of the 2015 Underhanded C Contest by scatters on Thursday February 04, 2016 @12:46PM (#11035669)
The interesting thing about changing return types is that you can alter the return type of spectral_contrast to float_t without affecting behavior, since float is promoted to double for argument passing (it's passed and returned in the xmm registers).
Re:Results of the 2015 Underhanded C Contest by saghul on Thursday February 04, 2016 @07:35AM (#11033555)
Did you try with -Weverything on Clang? That will enable more warnings than -Wall and -Wextra.
Re:Results of the 2015 Underhanded C Contest by nkurz on Thursday February 04, 2016 @02:28PM (#11036493)
Thanks for mentioning that option. I hadn't known about it.

Clang does give a warning with -Weverything, but I don't understand what it means: http://goo.gl/M9qjmx

  13 : warning: no previous prototype for function spectral_contrast' [-Wmissing-prototypes]
       double spectral_contrast(float_t *a, float_t *b, int length) {
It gives the same warning if I change all the float_t's to float, or if I change all the float_t's to double, so I think it's not actually useful or relevant.
Re:Results of the 2015 Underhanded C Contest by 3JPLW on Thursday February 04, 2016 @03:43PM (#11037058)
It's effectively catching the fact that `spectral_contrast.c` does not `#include "match.h"` — this is indeed directly a part of the underhandedness.

The warning occurs because the function is not static — and therefore callable by other modules. Since it's missing a forward definition (a previous prototype), those modules must blindly declare the prototypes themselves… and their prototypes can get out of sync with the actual definition. That's what's happening here. `match.h` has the forward prototype, but it's very subtly different from the definition. Were match.h included, there'd be a much more glaring warning (or maybe even error).

Re:Results of the 2015 Underhanded C Contest by 3JPLW on Thursday February 04, 2016 @02:22PM (#11036450)

    $ clang -Wall -Weverything -pedantic -c -o spectral_contrast.o spectral_contrast.c
    spectral_contrast.c:16:8: warning: no previous prototype for function 'spectral_contrast' [-Wmissing-prototypes]
    double spectral_contrast(float_t *a, float_t *b, int length) {
           ^
    1 warning generated.