Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude pointer assign from bitwise assign #390

Merged
merged 13 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import cpp
import codingstandards.c.misra
import codingstandards.c.misra.EssentialTypes
import codingstandards.cpp.Bitwise

/**
* Holds if the operator `operator` has an operand `child` that is of an inappropriate essential type
Expand Down Expand Up @@ -177,7 +178,7 @@ predicate isInappropriateEssentialType(
child =
[
operator.(BinaryBitwiseOperation).getAnOperand(),
operator.(AssignBitwiseOperation).getAnOperand()
operator.(AssignBitwiseOperationFixed).getAnOperand()
lcartey marked this conversation as resolved.
Show resolved Hide resolved
] and
not operator instanceof LShiftExpr and
not operator instanceof RShiftExpr and
Expand Down
2 changes: 2 additions & 0 deletions c/misra/test/rules/RULE-10-1/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,6 @@ void pointerType() {
b || b; // COMPLIANT
p || b; // NON_COMPLIANT
b || p; // NON_COMPLIANT
p += 1; // COMPLIANT
p -= 1; // COMPLIANT
}
1 change: 1 addition & 0 deletions change_notes/2023-10-04-m5-0-20-exclude-pointer-bitwise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* `M5-0-20`, `M5-0-21`, `RULE-10-1` - exclude pointer assignment operators as bitwise operators.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Bitwise

predicate isBinaryBitwiseOperation(Operation o, VariableAccess l, VariableAccess r) {
exists(BinaryBitwiseOperation bbo | bbo = o |
l = bbo.getLeftOperand() and r = bbo.getRightOperand()
)
or
exists(AssignBitwiseOperation abo | abo = o | l = abo.getLValue() and r = abo.getRValue())
exists(AssignBitwiseOperationFixed abo | abo = o |
lcartey marked this conversation as resolved.
Show resolved Hide resolved
l = abo.getLValue() and
r = abo.getRValue()
)
}

from Operation o, Variable left, Variable right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Bitwise

from Operation o, VariableAccess va
where
not isExcluded(o, ExpressionsPackage::bitwiseOperatorAppliedToSignedTypesQuery()) and
(
o instanceof UnaryBitwiseOperation or
o instanceof BinaryBitwiseOperation or
o instanceof AssignBitwiseOperation
o instanceof AssignBitwiseOperationFixed
lcartey marked this conversation as resolved.
Show resolved Hide resolved
) and
o.getAnOperand() = va and
va.getTarget().getUnderlyingType().(IntegralType).isSigned()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.Bitwise

class ShiftOperation extends Operation {
Expr leftOperand;
Expand All @@ -33,7 +34,7 @@ class ShiftOperation extends Operation {
rightOperand = o.getRightOperand()
)
or
exists(AssignBitwiseOperation o | this = o |
exists(AssignBitwiseOperationFixed o | this = o |
lcartey marked this conversation as resolved.
Show resolved Hide resolved
(
o instanceof AssignLShiftExpr
or
Expand Down
5 changes: 5 additions & 0 deletions cpp/autosar/test/rules/M5-0-20/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,8 @@ void test463_2_instantiations() {
char shift2 = 2;
test463_2(val, shift2);
}

void test_add(char *val) {
int add = 2;
val += add; // COMPLIANT
}
4 changes: 4 additions & 0 deletions cpp/autosar/test/rules/M5-0-21/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ void test() {
u ^= u; // COMPLIANT
u | 0; // COMPLIANT
u |= 0; // COMPLIANT

int *p = 0;
p += 1; // COMPLIANT
p -= 1; // COMPLIANT
}
18 changes: 18 additions & 0 deletions cpp/common/src/codingstandards/cpp/Bitwise.qll
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* A library for addressing issues in bitwise operator modelling in our database schema.
*/

import cpp

/**
* A binary bitwise assign operation, excluding += and -= on pointers, which seem to be erroneously
* included.
*/
class AssignBitwiseOperationFixed extends AssignBitwiseOperation {
AssignBitwiseOperationFixed() {
// exclude += and -= on pointers, which seem to be erroneously included
// in the database schema
not this instanceof AssignPointerAddExpr and
not this instanceof AssignPointerSubExpr
}
}
lcartey marked this conversation as resolved.
Show resolved Hide resolved
Loading