Skip to content

Commit 1f97e30

Browse files
committed
Fixed calculation of array size (danmar#6487)
1 parent ae4b86c commit 1f97e30

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,8 +2050,8 @@ void Tokenizer::simplifyDoublePlusAndDoubleMinus()
20502050

20512051
void Tokenizer::arraySize()
20522052
{
2053-
bool addlength = false;
20542053
for (Token *tok = list.front(); tok; tok = tok->next()) {
2054+
bool addlength = false;
20552055
if (Token::Match(tok, "%name% [ ] = { %str% } ;")) {
20562056
Token *t = tok->tokAt(3);
20572057
t->deleteNext();
@@ -2061,9 +2061,8 @@ void Tokenizer::arraySize()
20612061

20622062
if (addlength || Token::Match(tok, "%name% [ ] = %str% ;")) {
20632063
tok = tok->next();
2064-
std::size_t sz = tok->strAt(3).length() - 1;
2064+
std::size_t sz = Token::getStrSize(tok->tokAt(3));
20652065
tok->insertToken(MathLib::toString((unsigned int)sz));
2066-
addlength = false;
20672066
tok = tok->tokAt(5);
20682067
}
20692068

test/testsimplifytokens.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,10 @@ class TestSimplifyTokens : public TestFixture {
717717
const char code3[] = "char str [ ] = \"\\0\";";
718718
const char expected3[] = "char str [ 2 ] = \"\\0\" ;";
719719
ASSERT_EQUALS(expected3, tok(code3));
720+
721+
const char code4[] = "char str [ ] = \"\\n\\n\";";
722+
const char expected4[] = "char str [ 3 ] = \"\\n\\n\" ;";
723+
ASSERT_EQUALS(expected4, tok(code4));
720724
}
721725

722726
void dontRemoveIncrement() {

0 commit comments

Comments
 (0)