Menu

array of size -1

2025-09-11
2025-09-23
  • Yannick HOUDOT

    Yannick HOUDOT - 2025-09-11
    #define ASSERT(cond) { static const char condition_failed[(cond) ? 1 : -1] = {0}; (void)condition_failed[0]; }
    
    void func()
    { ASSERT(0); }
    

    This code does not compile: "error: size of array ‘condition_failed’ is negative"

    but cppcheck says:
    error: Array 'condition_failed[-1]' accessed at index 0, which is out of bounds. [arrayIndexOutOfBounds]

    I think there's something amiss in the analysis, no ?

     
  • Oliver Stöneberg

    Cppcheck is more lenient than compilers so it is possible that invalid code can still be successfully analyzed. The same is actually true for clang-tidy which might also report findings even if you are getting compiler errors.

    If the code cannot be compiled to begin with I see no issue here.

     

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.