From 6a37942431499b1dcd8a9fb8f5bd69515e98f39c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 7 Oct 2012 18:38:05 +0200 Subject: [PATCH] Fixed #3935 (False report for accessing array out of bounds after casting to short) --- lib/tokenize.cpp | 6 ++++++ test/testtokenize.cpp | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 312f669de..bb7d7480c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -4521,6 +4521,12 @@ void Tokenizer::simplifyCasts() tok = tok->linkAt(2); continue; } + // #3935 : don't remove cast in such cases: + // ((char *)a)[1] = 0; + if (tok->str() == "(" && Token::simpleMatch(tok->link(), ") [")) { + tok = tok->link(); + continue; + } // #4164 : ((unsigned char)1) => (1) if (Token::Match(tok->next(), "( unsigned| %type% ) %num%") && tok->next()->link()->previous()->isStandardType()) { const MathLib::bigint value = MathLib::toLongNumber(tok->next()->link()->next()->str()); diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index 7c43e317c..86b9aa4eb 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -84,6 +84,7 @@ private: TEST_CASE(removeCast9); TEST_CASE(removeCast10); TEST_CASE(removeCast11); + TEST_CASE(removeCast12); TEST_CASE(inlineasm); @@ -832,6 +833,11 @@ private: ASSERT_EQUALS("; x = 0 ;", tokenizeAndStringify("; *(int *)&x = 0;", true)); } + void removeCast12() { + // #3935 - don't remove this cast + ASSERT_EQUALS("; ( ( short * ) data ) [ 5 ] = 0 ;", tokenizeAndStringify("; ((short*)data)[5] = 0;", true)); + } + void inlineasm() { ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("asm { mov ax,bx };")); ASSERT_EQUALS("asm ( \"mov ax , bx\" ) ;", tokenizeAndStringify("_asm { mov ax,bx };"));