From 9f22d2fc6b41fa793699fd47b6a467031243f738 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 21 Jul 2011 09:32:24 +0200 Subject: [PATCH] Fixed #2897 (out of bounds false positive, using reinterpret_cast) --- lib/tokenize.cpp | 8 ++++++++ test/testsimplifytokens.cpp | 3 +++ 2 files changed, 11 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 038385450..c92f47dc6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -5612,6 +5612,14 @@ void Tokenizer::simplifyCasts() { for (Token *tok = _tokens; tok; tok = tok->next()) { + // #2897 : don't remove cast in such cases: + // *((char *)a + 1) = 0; + if (!tok->isName() && Token::simpleMatch(tok->next(), "* (")) + { + tok = tok->tokAt(2)->link(); + continue; + } + while (Token::Match(tok->next(), "( %type% *| ) *|&| %var%") || Token::Match(tok->next(), "( %type% %type% *| ) *|&| %var%") || (!tok->isName() && (Token::Match(tok->next(), "( %type% * ) (") || diff --git a/test/testsimplifytokens.cpp b/test/testsimplifytokens.cpp index 640e6e707..22446ac3f 100644 --- a/test/testsimplifytokens.cpp +++ b/test/testsimplifytokens.cpp @@ -434,6 +434,9 @@ private: ASSERT_EQUALS("class A { A operator* ( int ) ; } ;", tok("class A { A operator *(int); };")); ASSERT_EQUALS("class A { A operator* ( int ) const ; } ;", tok("class A { A operator *(int) const; };")); ASSERT_EQUALS("if ( ! p )", tok("if (p == (char *)(char *)0)")); + + // no simplification as the cast may be important here. see #2897 for example + ASSERT_EQUALS("; * ( ( char * ) p + 1 ) = 0 ;", tok("; *((char *)p + 1) = 0;")); }