From 71bf0f076f93571e20b02e94807bcd3bd7810c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Nov 2015 19:34:36 +0100 Subject: [PATCH] CheckAutoVariables: Handle 'x-y' better in isAutoVarArray --- lib/checkautovariables.cpp | 7 +++++++ test/testautovariables.cpp | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/lib/checkautovariables.cpp b/lib/checkautovariables.cpp index 419383699..4f61f102d 100644 --- a/lib/checkautovariables.cpp +++ b/lib/checkautovariables.cpp @@ -99,6 +99,13 @@ bool CheckAutoVariables::isAutoVarArray(const Token *tok) if (tok->str() == "+") return isAutoVarArray(tok->astOperand1()) || isAutoVarArray(tok->astOperand2()); + // x-intexpr + if (tok->str() == "-") + return isAutoVarArray(tok->astOperand1()) && + tok->astOperand2() && + tok->astOperand2()->valueType() && + tok->astOperand2()->valueType()->isIntegral(); + const Variable *var = tok->variable(); if (!var) return false; diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 756e33269..c664a108a 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -680,6 +680,12 @@ private: " return x+5;\n" "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Pointer to local array variable returned.\n", errout.str()); + + check("char *foo(int y) {\n" + " char x[10] = {0};\n" + " return (x+8)-y;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Pointer to local array variable returned.\n", errout.str()); } void returnLocalVariable5() { // cast