Skip to content

Extend nullPointerArithmetic to check for addition as well - #1114

Closed
pfultz2 wants to merge 8 commits into
cppcheck-opensource:masterfrom
pfultz2:null-pointer-arithmetic
Closed

pfultz2 wants to merge 8 commits into
cppcheck-opensource:masterfrom
pfultz2:null-pointer-arithmetic

Conversation

@pfultz2

@pfultz2 pfultz2 commented Mar 12, 2018

Copy link
Copy Markdown
Collaborator

Check for addition with null pointer as it is most likely an error:

#include <stdlib.h>
int* f() {
    int *x = NULL;
    return x + 5;
}

@pfultz2 pfultz2 changed the title Extend to nullPointerArithmetic to check for addition as well Extend nullPointerArithmetic to check for addition as well Mar 12, 2018

@orbitcowboy orbitcowboy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Are other operators like %, * or / ? Are they handled already?

@pfultz2

pfultz2 commented Mar 12, 2018

Copy link
Copy Markdown
Collaborator Author

Are other operators like %, * or / ?

Those operators are not valid with pointers.

@orbitcowboy

Copy link
Copy Markdown
Collaborator

Thanks! Did you try += and -= operators as well?

@pfultz2

pfultz2 commented Mar 12, 2018

Copy link
Copy Markdown
Collaborator Author

Did you try += and -= operators as well?

Thats a good point. Let me add those.

@pfultz2

pfultz2 commented Mar 13, 2018

Copy link
Copy Markdown
Collaborator Author

So it seems dataflow analysis doesn't work that well with p += n. It has the newly assigned value and not the previous value. Since we are checking for 0, I could check that the value is the same when using the assignment operators.

@danmar danmar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I have a few stylistic nits, but overall it looks promising.

Comment thread lib/checknullpointer.cpp Outdated
if (value->condition && !_settings->isEnabled(Settings::WARNING))
continue;
arithmeticError(tok,value);
if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer early continue. And a Token::Match should be used:

if (!Token::Match(tok, "-|+|+=|-="))
    continue;

Comment thread lib/checknullpointer.cpp Outdated
if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=")
{
const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()};
for(const Token *argument:arguments) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, we have not started using range for loops yet. We want to support some old compilers that only has partial c++11 support.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What C++11 features are allowed?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c++11 should be mostly avoided. off the top of my head we do use; nullptr, long long, r-value references, right angle brackets. The nullptr can be emulated easily if the compiler does not support it.

Even though I'd say we have been very conservative, people are sometimes contacting me needing help to compile with old versions of GCC.

Comment thread lib/checknullpointer.cpp Outdated
{
const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()};
for(const Token *argument:arguments) {
if(argument && argument->valueType() && argument->valueType()->pointer) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer early continue(s) here.

Comment thread lib/checknullpointer.cpp Outdated
// checking for zero we check that the value is equal
// to RHS
if (tok->astOperand2()->hasKnownIntValue()) {
if (tok->str() == "-=") checkValue -= tok->astOperand2()->values().front().intvalue;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please write the conditional code on its own line to match the normal cppcheck coding style.

if (tok->str() == "-=")
    checkValue -= tok->astOperand2()->values().front().intvalue;

Comment thread lib/checknullpointer.cpp Outdated
arithmeticError(tok,value);
if (tok->str() == "-" || tok->str() == "+" || tok->str() == "+=" || tok->str() == "-=")
{
const Token *arguments[] = {tok->astOperand1(), tok->astOperand2()};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Imho operands would be a better name.

@orbitcowboy

Copy link
Copy Markdown
Collaborator

Did you take increment and decrement-operators into account?

For example:

int* f7() { int *x = NULL; return ++x; }
int* f8() { int *x = NULL; return --x; }
int* f9() { int *x = NULL; return x--; }
int* f10() { int *x = NULL; return x++; } 

@danmar danmar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more stylistic nits

Comment thread lib/checknullpointer.cpp Outdated
continue;
arithmeticError(tok,value);
const Token *operands[] = {tok->astOperand1(), tok->astOperand2()};
for(const Token **operand_it = begin(operands); operand_it != end(operands);operand_it++) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this code.

How about:

for (int i = 1; i <= 2; i++) {
    const Token * const operand = (i==1) ? tok->astOperand1() : tok->astOperand2();

Or how about breaking out the for loop body in a function so no for loop is needed.

@pfultz2 pfultz2 Mar 14, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whats wrong with the regular for loop there? It follows the idiomatic for loop over a range in C++98:

Range r;
for(Iterator it = begin(r); it != end(r);r++)
    ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::begin and std::end are not available in c++98. and I believe, unlike those functions you are not using an iterator.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::begin and std::end are not available in c++98.

They are available as member functions in C++98. In C++11, they added free functions, so they can be used on arrays.

Also, I added begin and end function for arrays in utils.h, so they are available in C++98.

believe, unlike those functions you are not using an iterator.

A pointer meets all the requirements for a random access iterator. In fact, std::iterator_traits are specialized for pointers. This C++98 program prints out "Pointer is a random access iterator":

#include <iterator>
#include <cstdio>

char is_random_access(std::random_access_iterator_tag);
long is_random_access(...);

int main() {
    if (sizeof(is_random_access(std::iterator_traits<int*>::iterator_category())) == 1)
        printf("Pointer is a random access iterator\n");
}

@danmar danmar Mar 27, 2018 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for late reply. I would like some kind of refactoring. I am skeptic about adding these begin and end. I think that range for loops would be elegant, the day we start using c++11 I want to use range for loops here.

I know that pointers can be used but I am not sure that it is specified they are used. For instance sometimes debug random access iterators implement extra runtime checks. access out of bounds etc. That can not be implemented in the library if it just uses a pointer typedef.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about skipping the for loop.

const Token *pointerOperand;
if (tok->astOperand1()->valueType() && tok->astOperand1()->valueType()->pointer>0)
    pointerOperand = tok->astOperand1();
else if (tok->astOperand2()->valueType() && tok->astOperand2()->valueType()->pointer>0)
    pointerOperand = tok->astOperand2();
else
    continue;

@pfultz2 pfultz2 Mar 28, 2018 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry for late reply. I would like some kind of refactoring. I am skeptic about adding these begin and end. I think that range for loops would be elegant, the day we start using c++11 I want to use range for loops here.

Well with PR #1138, I added a foreach emulation for older compilers so we can start using this now.

I know that pointers can be used but I am not sure that it is specified they are used.

For iterator_traits, the standard library is required to provide the pointer specialization, and the begin and end array overloads are specified to return pointers as well(see here).

For instance sometimes debug random access iterators implement extra runtime checks. access out of bounds etc.

That is not a requirement for an iterator, and in fact, some older STL implementations used raw pointers directly.

That can not be implemented in the library if it just uses a pointer typedef.

Is it necessary for the iterators to operands to have out of bounds check for debug? Accessing the array directly with operands[i] has no out of bounds check in debug, but I could add iterators with debug checking if you think its necessary.

Comment thread lib/checknullpointer.cpp Outdated
const Token *operand = *operand_it;
if(!(operand && operand->valueType() && operand->valueType()->pointer))
continue;
long checkValue = 0;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest MathLib::bigint instead of long.

@pfultz2

pfultz2 commented Mar 14, 2018

Copy link
Copy Markdown
Collaborator Author

Did you take increment and decrement-operators into account?

I added those.

@danmar

danmar commented Apr 3, 2018

Copy link
Copy Markdown
Collaborator

Thanks! I applied the changes with d240a36

@danmar danmar closed this Apr 3, 2018
@pfultz2
pfultz2 deleted the null-pointer-arithmetic branch May 19, 2019 05:25
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