symboldatabase: Fix potential null pointer dereference (#1072)

There is a  potential `nullPointer` dereference in symboldatabase. This PR attempts to fix this. Additionally, this could be detected by Cppcheck as well. 

Here is a reduced and compilable testcase, where Cppcheck fails to detect a potential `nullPointer` dereference:

```
class Scope
{
public:
    bool bar();
    int *definedType;
};

int f(Scope *new_scope)
{
    int ret = 1;
    if (new_scope)
    {
        if (new_scope->bar())
        {
            if (!new_scope->definedType) {} // check for null
            ret = *new_scope->definedType; // dereference
        }
    }
    return ret;
}
```
The corresponding ticket on track, addressing the false negative: https://trac.cppcheck.net/ticket/8375
This commit is contained in:
orbitcowboy 2018-01-31 11:00:42 +01:00 committed by GitHub
parent 7b02b45a76
commit ee1ba85e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 1 additions and 2 deletions

View File

@ -130,9 +130,8 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
// goto initial '{'
if (!new_scope->definedType) {
_tokenizer->syntaxError(nullptr); // #6808
tok2 = new_scope->definedType->initBaseInfo(tok, tok2);
}
tok2 = new_scope->definedType->initBaseInfo(tok, tok2);
// make sure we have valid code
if (!tok2) {
break;