Menu

invalidPrintfArgType_sint/invalidPrintfArgType_float false positive

2025-11-18
2025-11-18
  • Nikita Leontiev

    Nikita Leontiev - 2025-11-18

    cppcheck 2.18.0 generates invalidPrintfArgType_sint and invalidPrintfArgType_float false positives for the following code:

    #include <assert.h>
    #include <stdio.h>
    
    class Object
    {
    private:
        int m_i;
        double m_d;
    public:
        Object(int i, double d) : m_i(i), m_d(d) {}
    
        template<typename T>
        T get()
        {
            assert(false);
            return T();
        }
        template<>
        int get()
        {
            return m_i;
        }
        template<>
        double get()
        {
            return m_d;
        }
    };
    
    int main()
    {
        Object obj(1, 1.5);
        printf("%d, %f\n", obj.get<int>(), obj.get<double>());
        return 0;
    }
    
    test\main.cpp:33:2: warning: %d in format string (no. 1) requires 'int' but the argument type is 'double'. [invalidPrintfArgType_sint]
     printf("%d, %f\n", obj.get<int>(), obj.get<double>());
     ^
    test\main.cpp:33:2: warning: %f in format string (no. 2) requires 'double' but the argument type is 'signed int'. [invalidPrintfArgType_float]
     printf("%d, %f\n", obj.get<int>(), obj.get<double>());
     ^
    

    invalidPrintfArgType_sint/invalidPrintfArgType_float is not triggered if get method is const.

     
  • CHR

    CHR - 2025-11-18

    Thanks for reporting, ticket is here: https://trac.cppcheck.net/ticket/14278

     

Log in to post a comment.