Skip to content

Commit

Permalink
Merge branch 'main' into lcartey/rule-11-4-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lcartey authored Sep 18, 2024
2 parents 0628e50 + 2db3484 commit bf1c4ce
Show file tree
Hide file tree
Showing 21 changed files with 416 additions and 225 deletions.
22 changes: 15 additions & 7 deletions c/misra/src/codingstandards/c/misra/EssentialTypes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ class EssentialBinaryLogicalOperationExpr extends EssentialExpr, BinaryLogicalOp
override Type getEssentialType() { result instanceof BoolType }
}

class EssentialUnaryLogicalOperationExpr extends EssentialExpr, UnaryLogicalOperation {
override Type getEssentialType() { result instanceof BoolType }
}

class EssentialEqualityOperationExpr extends EssentialExpr, EqualityOperation {
override Type getEssentialType() { result instanceof BoolType }
}
Expand Down Expand Up @@ -355,13 +359,17 @@ class EssentialLiteral extends EssentialExpr, Literal {
else (
if this.(CharLiteral).getCharacter().length() = 1
then result instanceof PlainCharType
else (
getStandardType().(IntegralType).isSigned() and
result = stlr(this)
or
not getStandardType().(IntegralType).isSigned() and
result = utlr(this)
)
else
exists(Type underlyingStandardType |
underlyingStandardType = getStandardType().getUnderlyingType()
|
if underlyingStandardType instanceof IntType
then
if underlyingStandardType.(IntType).isSigned()
then result = stlr(this)
else result = utlr(this)
else result = underlyingStandardType
)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ predicate isInappropriateEssentialType(
child =
[
operator.(BinaryBitwiseOperation).getAnOperand(),
operator.(Bitwise::AssignBitwiseOperation).getAnOperand()
operator.(Bitwise::AssignBitwiseOperation).getAnOperand(),
operator.(ComplementExpr).getAnOperand()
] and
not operator instanceof LShiftExpr and
not operator instanceof RShiftExpr and
Expand Down Expand Up @@ -240,7 +241,7 @@ string getRationaleMessage(int rationaleId, EssentialTypeCategory etc) {
result = "Bitwise operator applied to operand of " + etc + " and not essentially unsigned."
or
rationaleId = 7 and
result = "Right hand operatand of shift operator is " + etc + " and not not essentially unsigned."
result = "Right hand operand of shift operator is " + etc + " and not not essentially unsigned."
or
rationaleId = 8 and
result =
Expand All @@ -251,4 +252,4 @@ from Expr operator, Expr child, int rationaleId, EssentialTypeCategory etc
where
not isExcluded(operator, EssentialTypesPackage::operandsOfAnInappropriateEssentialTypeQuery()) and
isInappropriateEssentialType(operator, child, etc, rationaleId)
select operator, getRationaleMessage(rationaleId, etc)
select child, getRationaleMessage(rationaleId, etc)
47 changes: 42 additions & 5 deletions c/misra/src/rules/RULE-12-2/RightHandOperandOfAShiftRange.ql
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,51 @@ class ShiftExpr extends BinaryBitwiseOperation {
ShiftExpr() { this instanceof LShiftExpr or this instanceof RShiftExpr }
}

from ShiftExpr e, Expr right, int max_val
MacroInvocation getAMacroInvocation(ShiftExpr se) { result.getAnExpandedElement() = se }

Macro getPrimaryMacro(ShiftExpr se) {
exists(MacroInvocation mi |
mi = getAMacroInvocation(se) and
not exists(MacroInvocation otherMi |
otherMi = getAMacroInvocation(se) and otherMi.getParentInvocation() = mi
) and
result = mi.getMacro()
)
}

from
ShiftExpr e, Expr right, int max_val, float lowerBound, float upperBound, Type essentialType,
string extraMessage, Locatable optionalPlaceholderLocation, string optionalPlaceholderMessage
where
not isExcluded(right, Contracts7Package::rightHandOperandOfAShiftRangeQuery()) and
right = e.getRightOperand().getFullyConverted() and
max_val = (8 * getEssentialType(e.getLeftOperand()).getSize()) - 1 and
essentialType = getEssentialType(e.getLeftOperand()) and
max_val = (8 * essentialType.getSize()) - 1 and
upperBound = upperBound(right) and
lowerBound = lowerBound(right) and
(
lowerBound < 0 or
upperBound > max_val
) and
// If this shift happens inside a macro, then report the macro as well
// for easier validation
(
lowerBound(right) < 0 or
upperBound(right) > max_val
if exists(getPrimaryMacro(e))
then
extraMessage = " from expansion of macro $@" and
exists(Macro m |
m = getPrimaryMacro(e) and
optionalPlaceholderLocation = m and
optionalPlaceholderMessage = m.getName()
)
else (
extraMessage = "" and
optionalPlaceholderLocation = e and
optionalPlaceholderMessage = ""
)
)
select right,
"The right hand operand of the shift operator shall lie in the range 0 to " + max_val + "."
"The possible range of the right operand of the shift operator (" + lowerBound + ".." + upperBound
+ ") is outside the the valid shift range (0.." + max_val +
") for the essential type of the left operand (" + essentialType + ")" + extraMessage + ".",
optionalPlaceholderLocation, optionalPlaceholderMessage
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,25 @@ class NotUniqueExternalIdentifier extends ExternalIdentifiers {

Declaration getAConflictingDeclaration() {
not result = this and
isConflictingDeclaration(result, getName())
isConflictingDeclaration(result, getName()) and
// We only consider a declaration to be conflicting if it shares a link target with the external
// identifier. This avoids reporting false positives where multiple binaries or libraries are
// built in the same CodeQL database, but are not intended to be linked together.
exists(LinkTarget lt |
// External declaration can only be a function or global variable
lt = this.(Function).getALinkTarget() or
lt = this.(GlobalVariable).getALinkTarget()
|
lt = result.(Function).getALinkTarget()
or
lt = result.(GlobalVariable).getALinkTarget()
or
exists(Class c | c.getAMember() = result and c.getALinkTarget() = lt)
or
result.(LocalVariable).getFunction().getALinkTarget() = lt
or
result.(Class).getALinkTarget() = lt
)
}
}

Expand Down
35 changes: 35 additions & 0 deletions c/misra/test/c/misra/EssentialTypes.expected
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,38 @@
| test.c:26:3:26:3 | f | float | float | essentially Floating type |
| test.c:27:3:27:5 | f32 | float32_t | float32_t | essentially Floating type |
| test.c:28:3:28:6 | cf32 | float | float | essentially Floating type |
| test.c:32:3:32:3 | 1 | signed char | signed char | essentially Signed type |
| test.c:33:3:33:4 | 1 | unsigned char | unsigned char | essentially Unsigned type |
| test.c:34:3:34:5 | 1 | unsigned long | unsigned long | essentially Unsigned type |
| test.c:38:13:38:16 | 1 | bool | bool | essentially Boolean type |
| test.c:38:13:38:16 | (bool)... | bool | bool | essentially Boolean type |
| test.c:39:20:39:20 | 1 | signed char | signed char | essentially Signed type |
| test.c:39:20:39:20 | (unsigned int)... | unsigned int | unsigned int | essentially Unsigned type |
| test.c:40:23:40:23 | 1 | signed char | signed char | essentially Signed type |
| test.c:40:23:40:23 | (unsigned short)... | unsigned short | unsigned short | essentially Unsigned type |
| test.c:41:17:41:18 | 1 | signed char | signed char | essentially Signed type |
| test.c:42:21:42:21 | 1 | signed char | signed char | essentially Signed type |
| test.c:42:21:42:21 | (signed short)... | signed short | signed short | essentially Signed type |
| test.c:44:3:44:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:44:4:44:4 | b | bool | bool | essentially Boolean type |
| test.c:45:3:45:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:45:4:45:4 | u | unsigned int | unsigned int | essentially Unsigned type |
| test.c:46:3:46:5 | ! ... | bool | bool | essentially Boolean type |
| test.c:46:4:46:5 | us | unsigned short | unsigned short | essentially Unsigned type |
| test.c:47:3:47:4 | ! ... | bool | bool | essentially Boolean type |
| test.c:47:4:47:4 | s | signed int | signed int | essentially Signed type |
| test.c:48:3:48:5 | ! ... | bool | bool | essentially Boolean type |
| test.c:48:4:48:5 | ss | signed short | signed short | essentially Signed type |
| test.c:50:3:50:4 | ~ ... | int | int | essentially Signed type |
| test.c:50:4:50:4 | (int)... | int | int | essentially Signed type |
| test.c:50:4:50:4 | b | bool | bool | essentially Boolean type |
| test.c:51:3:51:4 | ~ ... | unsigned int | unsigned int | essentially Unsigned type |
| test.c:51:4:51:4 | u | unsigned int | unsigned int | essentially Unsigned type |
| test.c:52:3:52:5 | ~ ... | unsigned short | unsigned short | essentially Unsigned type |
| test.c:52:4:52:5 | (int)... | int | int | essentially Signed type |
| test.c:52:4:52:5 | us | unsigned short | unsigned short | essentially Unsigned type |
| test.c:53:3:53:4 | ~ ... | signed int | signed int | essentially Signed type |
| test.c:53:4:53:4 | s | signed int | signed int | essentially Signed type |
| test.c:54:3:54:5 | ~ ... | int | int | essentially Signed type |
| test.c:54:4:54:5 | (int)... | int | int | essentially Signed type |
| test.c:54:4:54:5 | ss | signed short | signed short | essentially Signed type |
26 changes: 26 additions & 0 deletions c/misra/test/c/misra/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,30 @@ void testCategoriesForComplexTypes() {
f; // Should be essentially Floating type
f32; // Should be essentially Floating type
cf32; // Should be essentially Floating type
}

void testConstants() {
1; // Essentially signed char
1U; // Essentially unsigned char
1UL; // Essentially unsigned long
}

void testUnary() {
_Bool b = true;
unsigned int u = 1;
unsigned short us = 1;
signed int s = 1;
signed short ss = 1;

!b; // Should be boolean
!u; // Should be boolean
!us; // Should be boolean
!s; // Should be boolean
!ss; // Should be boolean

~b; // Should be essentially signed
~u; // Should be essentially unsigned
~us; // Should be essentially unsigned
~s; // Should be essentially signed
~ss; // Should be essentially signed
}
Loading

0 comments on commit bf1c4ce

Please sign in to comment.