MCPcopy Create free account
hub / github.com/cppcheck-opensource/cppcheck / checkPointerAdditionResultNotNull

Method checkPointerAdditionResultNotNull

lib/checkcondition.cpp:1793–1832  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1791
1792
1793void CheckConditionImpl::checkPointerAdditionResultNotNull()
1794{
1795 if (!mSettings.severity.isEnabled(Severity::warning))
1796 return;
1797
1798 logChecker("CheckCondition::checkPointerAdditionResultNotNull"); // warning
1799
1800 const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
1801 for (const Scope * scope : symbolDatabase->functionScopes) {
1802
1803 for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
1804 if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2())
1805 continue;
1806
1807 // Macros might have pointless safety checks
1808 if (tok->isExpandedMacro())
1809 continue;
1810
1811 const Token *calcToken, *exprToken;
1812 if (tok->astOperand1()->str() == "+") {
1813 calcToken = tok->astOperand1();
1814 exprToken = tok->astOperand2();
1815 } else if (tok->astOperand2()->str() == "+") {
1816 calcToken = tok->astOperand2();
1817 exprToken = tok->astOperand1();
1818 } else
1819 continue;
1820
1821 // pointer comparison against NULL (ptr+12==0)
1822 if (calcToken->hasKnownIntValue())
1823 continue;
1824 if (!calcToken->valueType() || calcToken->valueType()->pointer==0)
1825 continue;
1826 if (!exprToken->hasKnownIntValue() || !exprToken->getValue(0))
1827 continue;
1828
1829 pointerAdditionResultNotNullError(tok, calcToken);
1830 }
1831 }
1832}
1833
1834void CheckConditionImpl::pointerAdditionResultNotNullError(const Token *tok, const Token *calc)
1835{

Callers 1

runChecksMethod · 0.80

Calls 7

nextMethod · 0.80
astOperand1Method · 0.80
astOperand2Method · 0.80
isEnabledMethod · 0.45
strMethod · 0.45
hasKnownIntValueMethod · 0.45
getValueMethod · 0.45

Tested by

no test coverage detected