Skip to content

Commit f5ad748

Browse files
committed
CheckBufferOverrun: Skip warnings about array index out of bounds in unions. Theoretically, the array is at least as large as the biggest union member.
1 parent 4558701 commit f5ad748

2 files changed

Lines changed: 9 additions & 0 deletions

File tree

lib/checkbufferoverrun.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,9 @@ void CheckBufferOverrun::bufferOverrun()
15071507
if (var->dimension(0) <= 1 && Token::simpleMatch(var->nameToken()->linkAt(1),"] ; }"))
15081508
continue;
15091509

1510+
if (var->scope() && var->scope()->type == Scope::eUnion)
1511+
continue;
1512+
15101513
ArrayInfo arrayInfo(var, symbolDatabase);
15111514
arrayInfo.varname(varname);
15121515

test/testbufferoverrun.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ class TestBufferOverrun : public TestFixture {
563563
" ab->a[0] = 0;\n"
564564
"}");
565565
ASSERT_EQUALS("", errout.str());
566+
567+
check("union { char a[1]; int b; } ab;\n"
568+
"void f() {\n"
569+
" ab.a[2] = 0;\n"
570+
"}");
571+
ASSERT_EQUALS("", errout.str());
566572
}
567573

568574

0 commit comments

Comments
 (0)