Menu

Issue in cppcheckdata.py on astIsFloat()

2024-08-30
2024-08-30
  • Robert Zickler

    Robert Zickler - 2024-08-30

    Background: I try to force the developer to append a "F" as suffix for float constants declarations e.g. 1.0 -> 1.0F or 2.0f -> 2.0F. The first case will force the use of the FPU the latter for uniformity with U and L suffixes.
    I detected the following but realized that using token.isFloat to check if a token is of type float, works perfectly. But I will keep my findings anyway for someone of interest. When I first wrote the addon I may misunderstood the ast-part and misused cppcheckdata.astIsFloat(token) for my case. Maybe the following is a feature and no issue at all:


    I used if cppcheckdata.astIsFloat(token) and not token.variable: to check if a constant type declaration is of type float.
    It worked quite reliable but I was confused to find UCHAR_MAX in the findings. It is defined as #define UCHAR_MAX 0xff and should have nothing to do with float. I investigated that all variables with an f will return true if checked with cppcheckdata.astIsFloat(token).

    Test with:

    uint16_t test1 = 0xfa;  //<-- cppcheckdata.astIsFloat(0xfa) => True  (not correct)
    uint16_t test2 = 0xaf;  //<-- cppcheckdata.astIsFloat(0xaf) => True  (not correct)
     uint16_t test3 = 0xaa;  //<-- cppcheckdata.astIsFloat(0xaa) => False (correct)
     uint16_t test4 = 0xFa;  //<-- cppcheckdata.astIsFloat(0xFa) => False (correct)
    

    The problem is the implementation in cppcheckdata.py of the check with:

    ...
    if token.str[0].isdigit():
        for c in token.str:
            if c == 'f' or c == '.' or c == 'E':
                return True
    ...
    

    More valid floats falsely not detected: 1e10, 0X0p-1, 0x0P-1

    Also: if token.str[0].isdigit(): will result in all token of valid negative floats not getting detected.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.