Skip to content

Commit a674741

Browse files
authored
fix #13854: memleak might show misleading location (danmar#7796)
This sets the column of added braces to the column of the token before it, so we can point to the last token of the scope. ``` Checking tickets/13854.c ... tickets/13854.c:3:30: error: Memory leak: p1 [memleak] void * p1 = malloc(5); ^ ```
1 parent f47acb0 commit a674741

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/tokenize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6801,6 +6801,7 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
68016801
}
68026802
tokEnd->insertToken("}");
68036803
Token * tokCloseBrace = tokEnd->next();
6804+
tokCloseBrace->column(tokEnd->column());
68046805

68056806
Token::createMutualLinks(tokOpenBrace, tokCloseBrace);
68066807
tokBracesEnd = tokCloseBrace;
@@ -6835,6 +6836,7 @@ Token *Tokenizer::simplifyAddBracesPair(Token *tok, bool commandWithCondition)
68356836

68366837
tokEnd->insertToken("}");
68376838
Token * tokCloseBrace=tokEnd->next();
6839+
tokCloseBrace->column(tokEnd->column());
68386840

68396841
Token::createMutualLinks(tokOpenBrace,tokCloseBrace);
68406842
tokBracesEnd=tokCloseBrace;

test/testleakautovar.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,17 +2230,17 @@ class TestLeakAutoVar : public TestFixture {
22302230
" void * p2 = malloc(2);\n"
22312231
" return;\n"
22322232
"}");
2233-
ASSERT_EQUALS("[test.c:3:0]: (error) Memory leak: p1 [memleak]\n"
2234-
"[test.c:5:0]: (error) Memory leak: p2 [memleak]\n", errout_str());
2233+
ASSERT_EQUALS("[test.c:3:30]: (error) Memory leak: p1 [memleak]\n"
2234+
"[test.c:5:30]: (error) Memory leak: p2 [memleak]\n", errout_str());
22352235

22362236
check("void f() {\n"
22372237
" if (x > 0)\n"
22382238
" void * p1 = malloc(5);\n"
22392239
" else\n"
22402240
" void * p2 = malloc(2);\n"
22412241
"}");
2242-
ASSERT_EQUALS("[test.c:3:0]: (error) Memory leak: p1 [memleak]\n"
2243-
"[test.c:5:0]: (error) Memory leak: p2 [memleak]\n", errout_str());
2242+
ASSERT_EQUALS("[test.c:3:30]: (error) Memory leak: p1 [memleak]\n"
2243+
"[test.c:5:30]: (error) Memory leak: p2 [memleak]\n", errout_str());
22442244
}
22452245

22462246
void ifelse21() {

0 commit comments

Comments
 (0)