Skip to content

Commit 9605d79

Browse files
committed
Rule 1.2: Correct detection of variable length arrays
1 parent a6409e4 commit 9605d79

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

c/common/src/codingstandards/c/Extensions.qll

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,16 @@ class CEmptyStructExtension extends CCompilerExtension, Struct {
169169
}
170170

171171
// Reference: https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html#Variable-Length
172-
class CVariableLengthArraysExtension extends CCompilerExtension, DeclarationEntry {
172+
class CVariableLengthArraysExtension extends CCompilerExtension, Field {
173173
CVariableLengthArraysExtension() {
174174
getType() instanceof ArrayType and
175-
not getType().(ArrayType).hasArraySize()
175+
not getType().(ArrayType).hasArraySize() and
176+
// Not the final member of the struct, which is allowed to be variably sized
177+
not exists(int lastIndex, Class declaringStruct |
178+
declaringStruct = getDeclaringType() and
179+
lastIndex = count(declaringStruct.getACanonicalMember()) - 1 and
180+
this = declaringStruct.getCanonicalMember(lastIndex)
181+
)
176182
}
177183

178184
override string getMessage() {

0 commit comments

Comments
 (0)