Menu

#1584 stats.c:222: possible bad call to abs ?

None
closed-accepted
nobody
None
2015-05-18
2015-03-24
dcb
No

stats.c:222:8: warning: using integer absolute value function 'abs' when argument is of floating point type [-Wabsolute-value]

ad += abs(t);

Maybe

ad += fabs(t);

Discussion

  • Ethan Merritt

    Ethan Merritt - 2015-03-24
    • status: open --> pending-accepted
    • Group: -->
    • Priority: -->
     
  • Ethan Merritt

    Ethan Merritt - 2015-03-24

    Yup. You caught a bug.

    It's very annoying that the gcc compiler option that would flag this automatically (-Wconversion) also trips over so many false positives that it is basically useless.

    thanks
    
     
  • dcb

    dcb - 2015-03-24

    But gcc doesn't understand -Wabsolute-value flag.

    AFAIK, warning actually produced by clang, a different and very
    useful C / C++ compiler.

    Here is more of the same from clang

    ../../src/mouse.c:2051:13: warning: using floating point absolute value function
    'fabs' when argument is of integer type [-Wabsolute-value]
    ../../src/mouse.c:2052:13: warning: using floating point absolute value function
    'fabs' when argument is of integer type [-Wabsolute-value]
    ../../src/plot2d.c:3443:50: warning: using integer absolute value function 'abs'
    when argument is of floating point type [-Wabsolute-value]
    ../../src/plot2d.c:3445:25: warning: using integer absolute value function 'abs'
    when argument is of floating point type [-Wabsolute-value]
    ../../src/plot2d.c:3448:21: warning: using integer absolute value function 'abs'
    when argument is of floating point type [-Wabsolute-value]
    ../../src/stats.c:222:8: warning: using integer absolute value function 'abs' wh
    en argument is of floating point type [-Wabsolute-value]
    ../../src/wxterminal/gp_cairo.c:1156:22: warning: using floating point absolute
    value function 'fabs' when argument is of integer type [-Wabsolute-value]
    ../../src/wxterminal/gp_cairo.c:1157:22: warning: using floating point absolute
    value function 'fabs' when argument is of integer type [-Wabsolute-value]

    Suggest that if gcc is weak in some areas, then use clang as well.
    So you increase your chances of finding bugs.

     
  • Ethan Merritt

    Ethan Merritt - 2015-03-24

    Actually, I do use clang in addition to gcc for exactly this purpose. The problem is that even if I try to cut down on the noise by using

      -Wconversion -Wno-sign-conversion -Wno-shorten-64-to-32
    

    I still get hundreds (>800) false positives such as

    mouse.c:2458:19: warning: implicit conversion loses integer precision: 'const int' to 'char' [-Wconversion]
    mouse.c:2876:28: warning: implicit conversion turns floating-point number into integer: 'double' to 'int' [-Wconversion]
    multiplot.c:497:20: warning: implicit conversion loses floating-point precision: 'double' to 'float' [-Wconversion]
    hidden3d.c:1699:17: warning: implicit conversion turns floating-point number into integer: 'coordval' (aka 'double') to 'int' [-Wconversion]
    

    There may be real bugs hiding in there, but the noise level is too high to find them easily.

     
  • dcb

    dcb - 2015-03-24

    Righto. Now I see your problem. Suggest start by fixing the eight
    or so I mentioned from clang.

    For the four warnings you mention, #2 and #4 look the most important.
    Suggest grep for those warning message patterns only, to start with.

    I've also had good results with a static analyser called cppcheck.
    While it doesn't help in this case, it might be useful for other
    messages it produces.

     
  • Ethan Merritt

    Ethan Merritt - 2015-03-24

    gnuplot uses a mixture of floating point and integer math throughout, intentionally. In general floating point computation is used up until terminal coordinates are needed, but these are always integers.

    The problem is that -Wconversion in clang doesn't distinguish between double->int conversion as a result of assigment, which is very probably intentional and correct as in,

    terminal_x_coord = floating_expression()
    

    and implicit conversion as a result of function parameter mismatch, like the one with abs(x) that you caught. I want a "-Wno-conversion-on-assignment" flag but there doesn't seem to be one.

    My clang (3.3 33/final) doesn't seem to have a -Wabsolute-value option. Is that a recent addition?

     
  • dcb

    dcb - 2015-03-24

    Current clang is version 3.5 and 3.3 is dated Jun 2013.

     
  • Ethan Merritt

    Ethan Merritt - 2015-05-18
    • status: pending-accepted --> closed-accepted
     

Log in to post a comment.