Skip to content

Commit be71c2e

Browse files
Fix #13694, #13704, #13708, #14356 fuzzing crashes (#8076)
Co-authored-by: chrchr-github <[email protected]>
1 parent 0539dee commit be71c2e

8 files changed

+16
-4
lines changed

lib/templatesimplifier.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ bool TemplateSimplifier::getTemplateDeclarations()
690690
else if (Token::Match(tok2, "{|=|;")) {
691691
const int namepos = getTemplateNamePosition(parmEnd);
692692
if (namepos > 0) {
693+
if (!tok->scopeInfo())
694+
syntaxError(tok);
693695
TokenAndName decl(tok, tok->scopeInfo()->name, parmEnd->tokAt(namepos), parmEnd);
694696
if (decl.isForwardDeclaration()) {
695697
// Declaration => add to mTemplateForwardDeclarations

lib/tokenize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8936,6 +8936,8 @@ void Tokenizer::findGarbageCode() const
89368936
syntaxError(tok);
89378937
if (Token::Match(tok, "& %comp%|&&|%oror%|&|%or%") && tok->strAt(1) != ">")
89388938
syntaxError(tok);
8939+
if (Token::Match(tok, "%comp%|&&|%oror%|&|%or% }") && tok->str() != ">")
8940+
syntaxError(tok);
89398941
if (Token::Match(tok, "^ %op%") && !Token::Match(tok->next(), "[>*+-!~]"))
89408942
syntaxError(tok);
89418943
if (Token::Match(tok, ": [)]=]"))
@@ -8944,6 +8946,8 @@ void Tokenizer::findGarbageCode() const
89448946
syntaxError(tok);
89458947
if (Token::Match(tok, "typedef [,;:]"))
89468948
syntaxError(tok);
8949+
if (Token::Match(tok, "? %assign%"))
8950+
syntaxError(tok);
89478951
if (Token::Match(tok, "!|~ %comp%") &&
89488952
!(cpp && tok->strAt(1) == ">" && Token::simpleMatch(tok->tokAt(-1), "operator")))
89498953
syntaxError(tok);

lib/tokenlist.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,8 @@ void TokenList::insertTokens(Token *dest, const Token *src, nonneg int n)
295295
std::stack<Token *> link;
296296

297297
while (n > 0) {
298+
if (!src)
299+
throw InternalError(dest, std::string(__func__) + ": invalid source range", InternalError::INTERNAL);
298300
dest->insertToken(src->str(), src->originalName());
299301
dest = dest->next();
300302

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{template<>i}template<>fact2(){fact2<3>()}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(c[?=3:4])p
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
c<a<s>s=t,{;{}}>l
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(S=n){n*,n&&}

test/cli/fuzz_test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def test_fuzz_crash():
1111

1212
fuzz_crash_dir = os.path.join(__script_dir, 'fuzz-crash')
1313
for f in os.listdir(fuzz_crash_dir):
14-
ret, stdout, _ = cppcheck(['-q', '--language=c++', '--enable=all', '--inconclusive', f], cwd=fuzz_crash_dir)
14+
ret, stdout, stderr = cppcheck(['-q', '--language=c++', '--enable=all', '--inconclusive', f], cwd=fuzz_crash_dir)
1515
if ret != 0:
16-
failures[f] = stdout
16+
failures[f] = stdout + stderr
1717

1818
assert failures == {}
1919

@@ -26,9 +26,9 @@ def test_fuzz_crash_c():
2626
if not os.path.exists(fuzz_crash_dir):
2727
return
2828
for f in os.listdir(fuzz_crash_dir):
29-
ret, stdout, _ = cppcheck(['-q', '--language=c', '--enable=all', '--inconclusive', f], cwd=fuzz_crash_dir)
29+
ret, stdout, stderr = cppcheck(['-q', '--language=c', '--enable=all', '--inconclusive', f], cwd=fuzz_crash_dir)
3030
if ret != 0:
31-
failures[f] = stdout
31+
failures[f] = stdout + stderr
3232

3333
assert failures == {}
3434

0 commit comments

Comments
 (0)