JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++


LANG.TYPE.NCPFO : Non-const Predicate Function Object

Summary

The function call operator of a user-defined predicate function object modifies internal object state.

A predicate function object P will only trigger a warning of this class if:

For the sake of this warning class, a predicate function object is an object of a class that overrides operator() to return bool or unsigned char.

Properties

Class Name Non-const Predicate Function Object
Significance style
Mnemonic LANG.TYPE.NCPFO
Categories
AUTOSARC++14 AUTOSARC++14:A25-1-1 Non-static data members or captured values of predicate function objects that are state related to this object's identity shall not be copied.
MisraC++2023 MisraC++2023:28.3.1 Predicates shall not have persistent side effects
CERT-CPP CERT-CPP:CTR58-CPP Predicate function objects should not be mutable
Availability Available for C++ only (not C).
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Non-const Predicate Function Object"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

#include <vector>
#include <algorithm>

class PfoNotWrapped {
private:
    size_t timesCalled;
public:
    PfoNotWrapped() : timesCalled(0) {}
    bool operator() (const int &) {                                // 'Non-const Predicate Function Object' warning issued here
        ++timesCalled;
        return timesCalled == 3;
    }
};

class PfoWrapped {
private:
    size_t timesCalled;
public:
    PfoWrapped() : timesCalled(0) {}
    bool operator() (const int &) {                                           // ok: all uses are wrapped with std::reference_wrapper
        ++timesCalled;
        return timesCalled == 3;
    }
};

class PfoConst {
  private:
    size_t timesCalled;
  public:
    PfoConst() : timesCalled(0) {}
    bool operator() (const int &) const {                                     // ok: uses are not wrapped, but operator() does not modify object state
        return timesCalled == 3;
    }
};

size_t non_const_pred_func_object(void) {
    std::vector<int> v{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    size_t timesCalled = 0;

    auto it1 = std::remove_if(v.begin(),
                              v.end(),
                              [timesCalled](const int &) mutable { // 'Non-const Predicate Function Object' warning issued here
                                  ++timesCalled;
                                  return timesCalled == 3;
                              });

    PfoNotWrapped predNW;
    auto it2 = std::remove_if(v.begin(),
                              it1,
                              predNW);            // not in a std::reference_wrapper: causes warning to be issued for PfoNotWrapped::operator(), above

    PfoWrapped predW;
    auto it3 = std::remove_if(v.begin(),
                              it2,
                              std::ref(predW));                               // std::ref() creates a std::reference_wrapper                                              

    PfoConst predC;
    auto it4 = std::remove_if(v.begin(),
                              it3,
			      predC);                                         // predC() does not mutate internal state so does not need to be wrapped

    v.erase(it4, v.end());
    return v.size();
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.