Skip to content

Commit f4f1160

Browse files
committed
Rule 8.13: Correctly handle assignment into structs
1 parent 4aacef1 commit f4f1160

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

c/misra/src/rules/RULE-8-13/PointerShouldPointToConstTypeWhenPossible.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class NonConstPointerVariableCandidate extends Variable {
4141
not exists(VariableEffect effect |
4242
effect.getTarget() = this and
4343
// but not pointers that are only themselves modified
44-
not effect.(AssignExpr).getLValue() = effect.getAnAccess() and
45-
not effect.(CrementOperation).getOperand() = effect.getAnAccess()
44+
not effect.(AssignExpr).getLValue() = this.getAnAccess() and
45+
not effect.(CrementOperation).getOperand() = this.getAnAccess()
4646
) and
4747
// exclude pointers assigned to another pointer to a non-const-qualified type
4848
not exists(Variable a |

c/misra/test/rules/RULE-8-13/PointerShouldPointToConstTypeWhenPossible.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
| test.c:66:23:66:24 | p1 | $@ points to a non-const-qualified type. | test.c:66:23:66:24 | p1 | p1 |
1313
| test.c:71:17:71:18 | p1 | $@ points to a non-const-qualified type. | test.c:71:17:71:18 | p1 | p1 |
1414
| test.c:75:15:75:16 | p1 | $@ points to a non-const-qualified type. | test.c:75:15:75:16 | p1 | p1 |
15+
| test.c:97:30:97:30 | s | $@ points to a non-const-qualified type. | test.c:97:30:97:30 | s | s |

c/misra/test/rules/RULE-8-13/test.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,16 @@ test_r(int16_t *value) { // COMPLIANT - ignored because of the use of ASM
8484
int16_t result;
8585
__asm__("movb %bh (%eax)");
8686
return result;
87+
}
88+
89+
struct S {
90+
int x;
91+
};
92+
93+
void test_struct(struct S *s) { // COMPLIANT
94+
s->x = 1;
95+
}
96+
97+
void test_struct_2(struct S *s) { // NON_COMPLIANT - could be const
98+
s = 0;
8799
}

0 commit comments

Comments
 (0)