From 90392faa2b45f4bf70f300f7fd5871cfe645a1db Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 9 Jun 2022 18:25:17 +0200 Subject: [PATCH] Fix crash on nullptr deref (#4189) --- lib/token.cpp | 2 +- test/testsymboldatabase.cpp | 16 +++++++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index f61ae5c91..149bc1baa 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2270,7 +2270,7 @@ std::pair Token::typeDecl(const Token * tok) if (!function) return {}; return { function->retDef, function->returnDefEnd() }; - } else if (tok->previous()->function()) { + } else if (tok->previous() && tok->previous()->function()) { const Function *function = tok->previous()->function(); return {function->retDef, function->returnDefEnd()}; } else if (Token::simpleMatch(tok, "=")) { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 593ff3b3e..4030da2c1 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -4816,11 +4816,17 @@ private: } void symboldatabase92() { // daca crash - GET_SYMBOL_DB("template struct a;\n" - "template \n" - "struct a : a<1, d...> {};\n" - "template struct f : a<0, e...> {};"); - ASSERT_EQUALS("", errout.str()); + { + GET_SYMBOL_DB("template struct a;\n" + "template \n" + "struct a : a<1, d...> {};\n" + "template struct f : a<0, e...> {};"); + ASSERT_EQUALS("", errout.str()); + } + { + GET_SYMBOL_DB("b.f();"); + ASSERT_EQUALS("", errout.str()); + } } void symboldatabase93() { // alignas attribute