From f5ad7482a8a30ca0cbe85de9b8dfbe393964d465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 18 Dec 2016 22:10:30 +0100 Subject: [PATCH] CheckBufferOverrun: Skip warnings about array index out of bounds in unions. Theoretically, the array is at least as large as the biggest union member. --- lib/checkbufferoverrun.cpp | 3 +++ test/testbufferoverrun.cpp | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/lib/checkbufferoverrun.cpp b/lib/checkbufferoverrun.cpp index 50374574c..03e1832bc 100644 --- a/lib/checkbufferoverrun.cpp +++ b/lib/checkbufferoverrun.cpp @@ -1507,6 +1507,9 @@ void CheckBufferOverrun::bufferOverrun() if (var->dimension(0) <= 1 && Token::simpleMatch(var->nameToken()->linkAt(1),"] ; }")) continue; + if (var->scope() && var->scope()->type == Scope::eUnion) + continue; + ArrayInfo arrayInfo(var, symbolDatabase); arrayInfo.varname(varname); diff --git a/test/testbufferoverrun.cpp b/test/testbufferoverrun.cpp index 6573c0b39..d931074c6 100644 --- a/test/testbufferoverrun.cpp +++ b/test/testbufferoverrun.cpp @@ -563,6 +563,12 @@ private: " ab->a[0] = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("union { char a[1]; int b; } ab;\n" + "void f() {\n" + " ab.a[2] = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); }