Opened 4 years ago

Closed 4 years ago

#11162 closed defect (fixed)

FP: knownConditionTrueFalse

Reported by: orbitcowboy Owned by: pfultz2
Priority: Normal Milestone: 2.9
Component: False positive Version:
Keywords: knownConditionTrueFalse valueflow Cc:

Description

#include <string>
#include <vector>

bool bar(const std::string &, std::vector<std::string>&);
bool f(const std::string& p)
{
    std::vector<std::string> data;
    if (!bar(p, data))
    {
        return false;
    }
    if (data.size() != 10U)
    {
        return false;
    }
    for (std::vector<std::string>::iterator it = data.begin(); it != data.cend(); ++it)
    {
        if (it->size() < 9U)
        {
            return false; // << 
        }
        it->erase(it->cbegin(), it->cbegin() + 9U);
    }
    return true;
}
$ gcc -c test.cpp && cppcheck --enable=style test.cpp
Checking test.cpp ...
test.cpp:18:24: style: Condition 'it->size()<9U' is always false [knownConditionTrueFalse]
        if (it->size() < 9U)
                       ^
test.cpp:12:21: note: Assuming that condition 'data.size()!=10U' is not redundant
    if (data.size() != 10U)
                    ^
test.cpp:18:24: note: Condition 'it->size()<9U' is always false
        if (it->size() < 9U)
                       ^

Tested with https://github.com/danmar/cppcheck/commit/596f75e2afb87bc7eefa44bec06068582a2d48d2

Change History (5)

comment:1 by chrchr, 4 years ago

Reduced:

bool f(std::vector<std::vector<int>> v) {    
    if (v.size() != 10U)
        return false;

    for (auto it = v.begin(); it != v.end(); ++it) {
        if (it->size() < 9U)
            return false;
        it->erase(it->begin(), it->begin() + 9U);
    }
    return true;
}

comment:2 by pfultz2, 4 years ago

Keywords: valueflow added

comment:3 by pfultz2, 4 years ago

Milestone: 2.9
Owner: changed from noone to pfultz2
Status: newaccepted

comment:5 by chrchr, 4 years ago

Resolution: fixed
Status: acceptedclosed
Note: See TracTickets for help on using tickets.