Skip to content

Commit 9dfe737

Browse files
committed
Remove deceptive uses of AssignBitwiseOperator
AssignBitwiseOperator erroneously includes pointer += and -=.
1 parent 9efce94 commit 9dfe737

File tree

8 files changed

+33
-11
lines changed

8 files changed

+33
-11
lines changed

c/misra/src/rules/RULE-10-1/OperandsOfAnInappropriateEssentialType.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import cpp
1515
import codingstandards.c.misra
1616
import codingstandards.c.misra.EssentialTypes
17+
import codingstandards.cpp.Bitwise
1718

1819
/**
1920
* Holds if the operator `operator` has an operand `child` that is of an inappropriate essential type
@@ -177,7 +178,7 @@ predicate isInappropriateEssentialType(
177178
child =
178179
[
179180
operator.(BinaryBitwiseOperation).getAnOperand(),
180-
operator.(AssignBitwiseOperation).getAnOperand()
181+
operator.(AssignBitwiseOperationFixed).getAnOperand()
181182
] and
182183
not operator instanceof LShiftExpr and
183184
not operator instanceof RShiftExpr and

c/misra/test/rules/RULE-10-1/test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,4 +492,6 @@ void pointerType() {
492492
b || b; // COMPLIANT
493493
p || b; // NON_COMPLIANT
494494
b || p; // NON_COMPLIANT
495+
p+=1; // COMPLIANT
496+
p-=1; // COMPLIANT
495497
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* `M5-0-20` - exclude pointer assignment operators as bitwise operators.
1+
* `M5-0-20`, `M5-0-21`, `RULE-10-1` - exclude pointer assignment operators as bitwise operators.

cpp/autosar/src/rules/M5-0-20/BitwiseOperatorOperandsHaveDifferentUnderlyingType.ql

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,14 @@
1616

1717
import cpp
1818
import codingstandards.cpp.autosar
19+
import codingstandards.cpp.Bitwise
1920

2021
predicate isBinaryBitwiseOperation(Operation o, VariableAccess l, VariableAccess r) {
2122
exists(BinaryBitwiseOperation bbo | bbo = o |
2223
l = bbo.getLeftOperand() and r = bbo.getRightOperand()
2324
)
2425
or
25-
exists(AssignBitwiseOperation abo |
26-
abo = o and
27-
// exclude += and -= on pointers, which seem to be erroneously included
28-
// in the database schema
29-
not abo instanceof AssignPointerAddExpr and
30-
not abo instanceof AssignPointerSubExpr
31-
|
26+
exists(AssignBitwiseOperationFixed abo | abo = o |
3227
l = abo.getLValue() and
3328
r = abo.getRValue()
3429
)

cpp/autosar/src/rules/M5-0-21/BitwiseOperatorAppliedToSignedTypes.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Bitwise
2021

2122
from Operation o, VariableAccess va
2223
where
2324
not isExcluded(o, ExpressionsPackage::bitwiseOperatorAppliedToSignedTypesQuery()) and
2425
(
2526
o instanceof UnaryBitwiseOperation or
2627
o instanceof BinaryBitwiseOperation or
27-
o instanceof AssignBitwiseOperation
28+
o instanceof AssignBitwiseOperationFixed
2829
) and
2930
o.getAnOperand() = va and
3031
va.getTarget().getUnderlyingType().(IntegralType).isSigned()

cpp/autosar/src/rules/M5-8-1/RightBitShiftOperandIsNegativeOrTooWide.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Bitwise
2021

2122
class ShiftOperation extends Operation {
2223
Expr leftOperand;
@@ -33,7 +34,7 @@ class ShiftOperation extends Operation {
3334
rightOperand = o.getRightOperand()
3435
)
3536
or
36-
exists(AssignBitwiseOperation o | this = o |
37+
exists(AssignBitwiseOperationFixed o | this = o |
3738
(
3839
o instanceof AssignLShiftExpr
3940
or

cpp/autosar/test/rules/M5-0-21/test.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ void test() {
4545
u ^= u; // COMPLIANT
4646
u | 0; // COMPLIANT
4747
u |= 0; // COMPLIANT
48+
49+
int *p = 0;
50+
p += 1; // COMPLIANT
51+
p -= 1; // COMPLIANT
4852
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* A library for addressing issues in bitwise operator modelling in our database schema.
3+
*/
4+
5+
import cpp
6+
7+
/**
8+
* A binary bitwise assign operation, excluding += and -= on pointers, which seem to be erroneously
9+
* included.
10+
*/
11+
class AssignBitwiseOperationFixed extends AssignBitwiseOperation {
12+
AssignBitwiseOperationFixed() {
13+
// exclude += and -= on pointers, which seem to be erroneously included
14+
// in the database schema
15+
not this instanceof AssignPointerAddExpr and
16+
not this instanceof AssignPointerSubExpr
17+
}
18+
}

0 commit comments

Comments
 (0)