Menu

False positive with volatile variables in branch conditions

2021-03-05
2021-03-29
  • Jeffrey Knockel

    Jeffrey Knockel - 2021-03-05

    Hello cppcheck community,

    Using cppcheck 2.3, I found that cppcheck can generate a false positive (knownConditionTrueFalse) when a volatile variable is checked in a branch condition. Consider the following example:

    #include <stdio.h>
    #include <signal.h>
    
    static volatile sig_atomic_t is_running;
    
    static void handler(int signum) {
        is_running = 0;
    }
    
    int main(void) {
        signal(SIGINT, &handler);
        is_running = 1;
        while (is_running) {}
        printf("Interrupted!\n");
        return 0;
    }
    

    When run with the --enable=all option, cppcheck generates the following warning:

    example.c:13:12: style: Condition 'is_running' is always true [knownConditionTrueFalse]
        while (is_running) {}
               ^
    

    Thank you for your attention in this matter.

     
  • Daniel Marjamäki

    Thanks! I can reproduce. I created https://trac.cppcheck.net/ticket/10223

     

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.