Skip to content

Fix #10671: functionConst FN with begin/end and const_iterator#3749

Merged
danmar merged 1 commit intocppcheck-opensource:mainfrom
rikardfalkeborn:10671-fix-const-iterator-function-const
Feb 2, 2022
Merged

Fix #10671: functionConst FN with begin/end and const_iterator#3749
danmar merged 1 commit intocppcheck-opensource:mainfrom
rikardfalkeborn:10671-fix-const-iterator-function-const

Conversation

@rikardfalkeborn
Copy link
Copy Markdown
Contributor

Check if the iterator is assigned to a const_iterator or
const_revese_iterator, in which case it is possible the function can be
const. Unfortunately, it is not possible to remove the hard coding of
cbegin, cend, crbegin and crend due to the need to handle auto, as in
the following code snippet:

void cbegin_auto(void)
{
    for (auto it = m_str.cbegin(); it != m_str.cend(); ++it) {;}
}

Check if the iterator is assigned to a const_iterator or
const_revese_iterator, in which case it is possible the function can be
const. Unfortunately, it is not possible to remove the hard coding of
cbegin, cend, crbegin and crend due to the need to handle auto, as in
the following code snippet:

    void cbegin_auto(void)
    {
        for (auto it = m_str.cbegin(); it != m_str.cend(); ++it) {;}
    }
@danmar danmar merged commit 2cacb13 into cppcheck-opensource:main Feb 2, 2022
danmar added a commit that referenced this pull request Feb 3, 2022
@chrchr-github
Copy link
Copy Markdown
Collaborator

I just noticed that this has been reverted. There was a crash (reduced from http://cppcheck1.osuosl.org:8000/mp3diags) on

struct S {
  std::vector<T*> v;
  void f() const;
};
void S::f() const {
    for (std::vector<T*>::const_iterator it = v.begin(), end = v.end(); it != end; ++it)
        (*it)->g();
}

(called with inconclusive) due to not checking tokAt(-2)->variable() for NULL in

if ((var->isStlType() // assume all std::*::size() and std::*::empty() are const
                     && (Token::Match(end, "size|empty|cend|crend|cbegin|crbegin|max_size|length|count|capacity|get_allocator|c_str|str ( )") || Token::Match(end, "rfind|copy"))) ||

                    (lastVarTok->valueType() && lastVarTok->valueType()->container &&
                     ((lastVarTok->valueType()->container->getYield(end->str()) == Library::Container::Yield::START_ITERATOR) ||
                      (lastVarTok->valueType()->container->getYield(end->str()) == Library::Container::Yield::END_ITERATOR))
                     && (tok1->previous()->isComparisonOp() || (tok1->previous()->isAssignmentOp() && Token::Match(tok1->tokAt(-2)->variable()->typeEndToken(), "const_iterator|const_reverse_iterator")))))
                    ;

rikardfalkeborn added a commit to rikardfalkeborn/cppcheck that referenced this pull request Feb 6, 2022
orbitcowboy pushed a commit that referenced this pull request Feb 11, 2022
* Revert "Revert "Fix #10671: functionConst FN with begin/end and const_iterator (#3749)""

This reverts commit 9f6a36c.

* Fix crash by adding missing null check
@rikardfalkeborn rikardfalkeborn deleted the 10671-fix-const-iterator-function-const branch February 13, 2022 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants