Menu ▾ ▴

knownConditionTrueFalse - independent smart-pointer and value-flow false positives

Long Huang
2026-10-29
5 days ago
  • Long Huang

    Long Huang - 2026-10-29

    Repro

    Save as value-flow.cpp:

    #include <cassert>
    #include <memory>
    
    bool DifferentOwners(std::unique_ptr<int>& first, std::unique_ptr<int>& second)
    {
       return static_cast<void*>(first.get()) != static_cast<void*>(second.get());
    }
    
    double UnknownNumber();
    
    void CompareCopiedValue()
    {
       const double number = UnknownNumber();
       const double copy = number;
       assert(!(number <= copy));
       assert(!(number < copy));
       assert(!(number >= copy));
       assert(!(number > copy));
    }
    
    class Label
    {
    public:
       int Height() const;
    };
    
    class Overlay
    {
    public:
       Label* Child();
       void SetText(const char*);
    };
    
    void CheckChildResized()
    {
       Overlay overlay;
       const Label* label = overlay.Child();
       const int previous = label->Height();
       overlay.SetText("longer");
       assert(label->Height() > previous);
    }
    

    Version: Cppcheck 2.22.0 (Linux).

    Expected

    • unique_ptr::get() returns pointee addresses, not container iterators.
      Comparing two owners' addresses is valid and can establish distinct ownership.
    • A copied floating-point value can be NaN. Both number <= copy and
      number >= copy are then false, so they cannot be classified as always true.
      The strict comparisons are intentionally false controls in this example.
    • Overlay::SetText can change the child returned by Child(). Calling a
      const getter through a retained child pointer does not freeze its value.

    Actual

    value-flow.cpp:6: error: Iterators of different containers 'first' and 'second' are used together. [mismatchingContainers]
    value-flow.cpp:15: style: The comparison 'number <= copy' is always true because 'number' and 'copy' represent the same value. [knownConditionTrueFalse]
    value-flow.cpp:16: style: The comparison 'number < copy' is always false because 'number' and 'copy' represent the same value. [knownConditionTrueFalse]
    value-flow.cpp:17: style: The comparison 'number >= copy' is always true because 'number' and 'copy' represent the same value. [knownConditionTrueFalse]
    value-flow.cpp:18: style: The comparison 'number > copy' is always false because 'number' and 'copy' represent the same value. [knownConditionTrueFalse]
    value-flow.cpp:40: style: The comparison 'label->Height() > previous' is always false because 'label->Height()' and 'previous' represent the same value. [knownConditionTrueFalse]
    
     
  • CHR

    CHR - 2026-10-30

    https://trac.cppcheck.net/ticket/15158 tracks the mismatchingContainersmessage.
    Having different smart pointers own the same resource is a bug waiting to happen, so I think showing a warning is desirable.

    Other issues still open.

     
  • CHR

    CHR - 5 days ago

    knownConditionTrueFalse is tracked here: https://trac.cppcheck.net/ticket/15171

     

Log in to post a comment.