Menu

Potential false positive - duplicateExpression

Cez Chi
2023-09-05
2023-09-12
  • Cez Chi

    Cez Chi - 2023-09-05

    Hi, I have the following loop which gives me a [duplicateExpression] and a [unsignedPositive] false positives, which I think are related:

    const uint32_t time_out = some_value;
    uint32_t start_time = get_time_ms();
    uint32_t elapsed = 0;
    
    while (elapsed <= time_out) {
        do_something_until_timeout();
        // Update elapsed time.
        elapsed = get_time_ms() - start_time;
    }
    

    I think it is because cppcheck doesn't know that get_time_ms() returns a different value everytime. Is there a way to fix this or should I ignore it? Or maybe I did a silly mistake that I overlooked.

     
  • CHR

    CHR - 2023-09-05

    Can you post a minimal example which reproduces the problem?

     
  • Cez Chi

    Cez Chi - 2023-09-12

    Hi, apologies for the late reply. Here is a minimal example which reproduces the problem:

    static void wait(const HAL* hal, const uint32_t time_out) {
        uint32_t start_time = hal->get_time_ms();
        uint32_t elapsed = 0;
    
        while (elapsed <= time_out) {
            // Update elapsed time.
            elapsed = hal->get_time_ms() - start_time;
        }
    }
    
     
  • CHR

    CHR - 2023-09-12

    I only get one FP:

    foo.cpp:26:20: style: Unsigned expression 'time_out' can't be negative so it is unnecessary to test it. [unsignedPositive]
        while (elapsed <= time_out) {
                       ^
    foo.cpp:24:24: note: Assignment 'elapsed=0', assigned value is 0
        uint32_t elapsed = 0;
                           ^
    foo.cpp:26:20: note: Unsigned positive
        while (elapsed <= time_out) {
                       ^
    

    Ticket is here: https://trac.cppcheck.net/ticket/11982

     

    Last edit: CHR 2023-09-12

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.