Skip to content

Commit de8ee5b

Browse files
author
Zachary Blair
committed
Fixed #4554 (false negative: buffer access out of bounds)
1 parent bfb1bc5 commit de8ee5b

4 files changed

Lines changed: 69 additions & 17 deletions

File tree

‎lib/token.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ class CPPCHECKLIB Token {
202202
_type == eBoolean; // TODO: "true"/"false" aren't really a name...
203203
}
204204
bool isUpperCaseName() const;
205+
bool isLiteral() const {
206+
return _type == eNumber || _type == eString || _type == eChar ||
207+
_type == eBoolean || _type == eLiteral;
208+
}
205209
bool isNumber() const {
206210
return _type == eNumber;
207211
}

‎lib/tokenize.cpp‎

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,23 +3369,8 @@ bool Tokenizer::simplifyTokenList()
33693369
// Simplify simple calculations..
33703370
simplifyCalculations();
33713371

3372-
// Replace "*(str + num)" => "str[num]"
3373-
for (Token *tok = list.front(); tok; tok = tok->next()) {
3374-
if (!Token::Match(tok, "%var%") && !tok->isNumber()
3375-
&& !Token::Match(tok, "]|)")
3376-
&& (Token::Match(tok->next(), "* ( %var% + %num%|%var% )"))) {
3377-
// remove '* ('
3378-
tok->deleteNext(2);
3379-
3380-
tok = tok->tokAt(2);
3381-
// '+'->'['
3382-
tok->str("[");
3383-
3384-
tok = tok->tokAt(2);
3385-
tok->str("]");
3386-
Token::createMutualLinks(tok->tokAt(-2), tok);
3387-
}
3388-
}
3372+
// Replace "*(ptr + num)" => "ptr[num]"
3373+
simplifyOffsetPointerDereference();
33893374

33903375
// Replace "&str[num]" => "(str + num)"
33913376
std::set<unsigned int> pod;
@@ -6760,8 +6745,39 @@ bool Tokenizer::simplifyCalculations()
67606745
return TemplateSimplifier::simplifyCalculations(list.front());
67616746
}
67626747

6748+
void Tokenizer::simplifyOffsetPointerDereference()
6749+
{
6750+
// Replace "*(str + num)" => "str[num]" and
6751+
// Replace "*(str - num)" => "str[-num]"
6752+
for (Token *tok = list.front(); tok; tok = tok->next()) {
6753+
if (!tok->isName() && !tok->isLiteral()
6754+
&& !Token::Match(tok, "]|)|++|--")
6755+
&& Token::Match(tok->next(), "* ( %var% +|- %num%|%var% )")) {
67636756

6757+
// remove '* ('
6758+
tok->deleteNext(2);
6759+
6760+
// '+'->'['
6761+
tok = tok->tokAt(2);
6762+
Token* const openBraceTok = tok;
6763+
const bool isNegativeIndex = (tok->str() == "-");
6764+
tok->str("[");
6765+
6766+
// Insert a "-" in front of the number or variable
6767+
if (isNegativeIndex) {
6768+
if (tok->next()->isName()) {
6769+
tok->insertToken("-");
6770+
tok = tok->next();
6771+
} else
6772+
tok->next()->str(std::string("-") + tok->next()->str());
6773+
}
67646774

6775+
tok = tok->tokAt(2);
6776+
tok->str("]");
6777+
Token::createMutualLinks(openBraceTok, tok);
6778+
}
6779+
}
6780+
}
67656781

67666782
void Tokenizer::simplifyGoto()
67676783
{

‎lib/tokenize.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,13 @@ class CPPCHECKLIB Tokenizer {
165165
*/
166166
bool simplifyCalculations();
167167

168+
/**
169+
* Simplify dereferencing a pointer offset by a number:
170+
* "*(ptr + num)" => "ptr[num]"
171+
* "*(ptr - num)" => "ptr[-num]"
172+
*/
173+
void simplifyOffsetPointerDereference();
174+
168175
/** Insert array size where it isn't given */
169176
void arraySize();
170177

‎test/testtokenize.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,11 +567,36 @@ class TestTokenizer : public TestFixture {
567567
ASSERT_EQUALS("; x = ( a + m ) & p [ n ] ;", tokenizeAndStringify("; x = ( a + m ) & p [ n ] ;", true));*/
568568
// "*(p+1)" => "p[1]"
569569
ASSERT_EQUALS("; x = p [ 1 ] ;", tokenizeAndStringify("; x = * ( p + 1 ) ;", true));
570+
ASSERT_EQUALS("; x = p [ 10 ] ;", tokenizeAndStringify("; x = * ( p + 0xA ) ;", true));
570571
ASSERT_EQUALS("; x = p [ n ] ;", tokenizeAndStringify("; x = * ( p + n ) ;", true));
571572
ASSERT_EQUALS("; x = y * ( p + n ) ;", tokenizeAndStringify("; x = y * ( p + n ) ;", true));
572573
ASSERT_EQUALS("; x = 10 * ( p + n ) ;", tokenizeAndStringify("; x = 10 * ( p + n ) ;", true));
573574
ASSERT_EQUALS("; x = y [ 10 ] * ( p + n ) ;", tokenizeAndStringify("; x = y [ 10 ] * ( p + n ) ;", true));
574575
ASSERT_EQUALS("; x = ( a + m ) * ( p + n ) ;", tokenizeAndStringify("; x = ( a + m ) * ( p + n ) ;", true));
576+
577+
// "*(p-1)" => "p[-1]" and "*(p-n)" => "p[-n]"
578+
ASSERT_EQUALS("; x = p [ -1 ] ;", tokenizeAndStringify("; x = *(p - 1);", true));
579+
ASSERT_EQUALS("; x = p [ -10 ] ;", tokenizeAndStringify("; x = *(p - 0xA);", true));
580+
ASSERT_EQUALS("; x = p [ - n ] ;", tokenizeAndStringify("; x = *(p - n);", true));
581+
ASSERT_EQUALS("; x = y * ( p - 1 ) ;", tokenizeAndStringify("; x = y * (p - 1);", true));
582+
ASSERT_EQUALS("; x = 10 * ( p - 1 ) ;", tokenizeAndStringify("; x = 10 * (p - 1);", true));
583+
ASSERT_EQUALS("; x = y [ 10 ] * ( p - 1 ) ;", tokenizeAndStringify("; x = y[10] * (p - 1);", true));
584+
ASSERT_EQUALS("; x = ( a - m ) * ( p - n ) ;", tokenizeAndStringify("; x = (a - m) * (p - n);", true));
585+
586+
// Test that the array-index simplification is not applied when there's no dereference:
587+
// "(x-y)" => "(x-y)" and "(x+y)" => "(x+y)"
588+
ASSERT_EQUALS("; a = b * ( x - y ) ;", tokenizeAndStringify("; a = b * (x - y);", true));
589+
ASSERT_EQUALS("; a = b * x [ - y ] ;", tokenizeAndStringify("; a = b * *(x - y);", true));
590+
ASSERT_EQUALS("; a = a * ( x - y ) ;", tokenizeAndStringify("; a *= (x - y);", true));
591+
ASSERT_EQUALS("; z = a ++ * ( x - y ) ;", tokenizeAndStringify("; z = a++ * (x - y);", true));
592+
ASSERT_EQUALS("; z = a ++ * ( x + y ) ;", tokenizeAndStringify("; z = a++ * (x + y);", true));
593+
ASSERT_EQUALS("; z = a -- * ( x - y ) ;", tokenizeAndStringify("; z = a-- * (x - y);", true));
594+
ASSERT_EQUALS("; z = a -- * ( x + y ) ;", tokenizeAndStringify("; z = a-- * (x + y);", true));
595+
ASSERT_EQUALS("; z = 'a' * ( x - y ) ;", tokenizeAndStringify("; z = 'a' * (x - y);", true));
596+
ASSERT_EQUALS("; z = \"a\" * ( x - y ) ;", tokenizeAndStringify("; z = \"a\" * (x - y);", true));
597+
ASSERT_EQUALS("; z = 'a' * ( x + y ) ;", tokenizeAndStringify("; z = 'a' * (x + y);", true));
598+
ASSERT_EQUALS("; z = \"a\" * ( x + y ) ;", tokenizeAndStringify("; z = \"a\" * (x + y);", true));
599+
ASSERT_EQUALS("; z = foo ( ) * ( x + y ) ;", tokenizeAndStringify("; z = foo() * (x + y);", true));
575600
}
576601

577602
void tokenize7() {

0 commit comments

Comments
 (0)