Skip to content

Commit 5d79dd5

Browse files
author
Nikita Kraiouchkine
committed
Limit scope of Pointers2 to ARR39-C and update rule file
1 parent f2f6110 commit 5d79dd5

File tree

4 files changed

+6
-148
lines changed

4 files changed

+6
-148
lines changed

c/cert/src/rules/ARR39-C/DoNotAddOrSubtractAScaledIntegerToAPointer.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ This query implements the CERT-C rule ARR39-C:
55
> Do not add or subtract a scaled integer to a pointer
66
77

8-
98
## Description
109

1110
Pointer arithmetic is appropriate only when the pointer argument refers to an array (see [ARR37-C. Do not add or subtract an integer to a pointer to a non-array object](https://wiki.sei.cmu.edu/confluence/display/c/ARR37-C.+Do+not+add+or+subtract+an+integer+to+a+pointer+to+a+non-array+object)), including an array of bytes. When performing pointer arithmetic, the size of the value to add to or subtract from a pointer is automatically scaled to the size of the type of the referenced array object. Adding or subtracting a scaled integer value to or from a pointer is invalid because it may yield a pointer that does not point to an element within or one past the end of the array. (See [ARR30-C. Do not form or use out-of-bounds pointers or array subscripts](https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts).)

cpp/common/src/codingstandards/cpp/exclusions/c/Pointers2.qll

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import cpp
33
import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

6-
newtype Pointers2Query =
7-
TDoNotAddOrSubtractAScaledIntegerToAPointerQuery() or
8-
TDoNotAccessVolatileObjectWithNonvolatileReferenceQuery() or
9-
TDoNotCastPointerToMoreStrictlyAlignedPointerTypeQuery() or
10-
TDoNotAccessVariableWithPointerOfIncompatibleTypeQuery() or
11-
TUndefinedBehaviorWithRestrictQualifiedPointerQuery()
6+
newtype Pointers2Query = TDoNotAddOrSubtractAScaledIntegerToAPointerQuery()
127

138
predicate isPointers2QueryMetadata(Query query, string queryId, string ruleId) {
149
query =
@@ -18,38 +13,6 @@ predicate isPointers2QueryMetadata(Query query, string queryId, string ruleId) {
1813
// `@id` for the `doNotAddOrSubtractAScaledIntegerToAPointer` query
1914
"c/cert/do-not-add-or-subtract-a-scaled-integer-to-a-pointer" and
2015
ruleId = "ARR39-C"
21-
or
22-
query =
23-
// `Query` instance for the `doNotAccessVolatileObjectWithNonvolatileReference` query
24-
Pointers2Package::doNotAccessVolatileObjectWithNonvolatileReferenceQuery() and
25-
queryId =
26-
// `@id` for the `doNotAccessVolatileObjectWithNonvolatileReference` query
27-
"c/cert/do-not-access-volatile-object-with-nonvolatile-reference" and
28-
ruleId = "EXP32-C"
29-
or
30-
query =
31-
// `Query` instance for the `doNotCastPointerToMoreStrictlyAlignedPointerType` query
32-
Pointers2Package::doNotCastPointerToMoreStrictlyAlignedPointerTypeQuery() and
33-
queryId =
34-
// `@id` for the `doNotCastPointerToMoreStrictlyAlignedPointerType` query
35-
"c/cert/do-not-cast-pointer-to-more-strictly-aligned-pointer-type" and
36-
ruleId = "EXP36-C"
37-
or
38-
query =
39-
// `Query` instance for the `doNotAccessVariableWithPointerOfIncompatibleType` query
40-
Pointers2Package::doNotAccessVariableWithPointerOfIncompatibleTypeQuery() and
41-
queryId =
42-
// `@id` for the `doNotAccessVariableWithPointerOfIncompatibleType` query
43-
"c/cert/do-not-access-variable-with-pointer-of-incompatible-type" and
44-
ruleId = "EXP39-C"
45-
or
46-
query =
47-
// `Query` instance for the `undefinedBehaviorWithRestrictQualifiedPointer` query
48-
Pointers2Package::undefinedBehaviorWithRestrictQualifiedPointerQuery() and
49-
queryId =
50-
// `@id` for the `undefinedBehaviorWithRestrictQualifiedPointer` query
51-
"c/cert/undefined-behavior-with-restrict-qualified-pointer" and
52-
ruleId = "EXP43-C"
5316
}
5417

5518
module Pointers2Package {
@@ -59,32 +22,4 @@ module Pointers2Package {
5922
// `Query` type for `doNotAddOrSubtractAScaledIntegerToAPointer` query
6023
TQueryC(TPointers2PackageQuery(TDoNotAddOrSubtractAScaledIntegerToAPointerQuery()))
6124
}
62-
63-
Query doNotAccessVolatileObjectWithNonvolatileReferenceQuery() {
64-
//autogenerate `Query` type
65-
result =
66-
// `Query` type for `doNotAccessVolatileObjectWithNonvolatileReference` query
67-
TQueryC(TPointers2PackageQuery(TDoNotAccessVolatileObjectWithNonvolatileReferenceQuery()))
68-
}
69-
70-
Query doNotCastPointerToMoreStrictlyAlignedPointerTypeQuery() {
71-
//autogenerate `Query` type
72-
result =
73-
// `Query` type for `doNotCastPointerToMoreStrictlyAlignedPointerType` query
74-
TQueryC(TPointers2PackageQuery(TDoNotCastPointerToMoreStrictlyAlignedPointerTypeQuery()))
75-
}
76-
77-
Query doNotAccessVariableWithPointerOfIncompatibleTypeQuery() {
78-
//autogenerate `Query` type
79-
result =
80-
// `Query` type for `doNotAccessVariableWithPointerOfIncompatibleType` query
81-
TQueryC(TPointers2PackageQuery(TDoNotAccessVariableWithPointerOfIncompatibleTypeQuery()))
82-
}
83-
84-
Query undefinedBehaviorWithRestrictQualifiedPointerQuery() {
85-
//autogenerate `Query` type
86-
result =
87-
// `Query` type for `undefinedBehaviorWithRestrictQualifiedPointer` query
88-
TQueryC(TPointers2PackageQuery(TUndefinedBehaviorWithRestrictQualifiedPointerQuery()))
89-
}
9025
}

rule_packages/c/Pointers2.json

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,12 @@
1212
"precision": "high",
1313
"severity": "error",
1414
"short_name": "DoNotAddOrSubtractAScaledIntegerToAPointer",
15-
"tags": [
15+
"tags":[
1616
"correctness"
1717
]
1818
}
1919
],
2020
"title": "Do not add or subtract a scaled integer to a pointer"
21-
},
22-
"EXP32-C": {
23-
"properties": {
24-
"obligation": "rule"
25-
},
26-
"queries": [
27-
{
28-
"description": "",
29-
"kind": "problem",
30-
"name": "Do not access a volatile object through a nonvolatile reference",
31-
"precision": "very-high",
32-
"severity": "error",
33-
"short_name": "DoNotAccessVolatileObjectWithNonvolatileReference",
34-
"tags": [
35-
"correctness"
36-
]
37-
}
38-
],
39-
"title": "Do not access a volatile object through a nonvolatile reference"
40-
},
41-
"EXP36-C": {
42-
"properties": {
43-
"obligation": "rule"
44-
},
45-
"queries": [
46-
{
47-
"description": "Converting a pointer value to a pointer type which is more strictly aligned results in undefined behaviour.",
48-
"kind": "problem",
49-
"name": "Do not cast pointers into more strictly aligned pointer types",
50-
"precision": "very-high",
51-
"severity": "error",
52-
"short_name": "DoNotCastPointerToMoreStrictlyAlignedPointerType",
53-
"tags": [
54-
"correctness"
55-
]
56-
}
57-
],
58-
"title": "Do not cast pointers into more strictly aligned pointer types"
59-
},
60-
"EXP39-C": {
61-
"properties": {
62-
"obligation": "rule"
63-
},
64-
"queries": [
65-
{
66-
"description": "Accessing an object's stored value by means of anything other than an lvalue of an allowable type results in undefined behavior.",
67-
"kind": "problem",
68-
"name": "Do not access a variable through a pointer of an incompatible type",
69-
"precision": "very-high",
70-
"severity": "error",
71-
"short_name": "DoNotAccessVariableWithPointerOfIncompatibleType",
72-
"tags": [
73-
"correctness"
74-
]
75-
}
76-
],
77-
"title": "Do not access a variable through a pointer of an incompatible type"
78-
},
79-
"EXP43-C": {
80-
"properties": {
81-
"obligation": "rule"
82-
},
83-
"queries": [
84-
{
85-
"description": "",
86-
"kind": "problem",
87-
"name": "Avoid undefined behavior when using restrict-qualified pointers",
88-
"precision": "very-high",
89-
"severity": "error",
90-
"short_name": "UndefinedBehaviorWithRestrictQualifiedPointer",
91-
"tags": [
92-
"correctness"
93-
]
94-
}
95-
],
96-
"title": "Avoid undefined behavior when using restrict-qualified pointers"
9721
}
9822
}
9923
}

rules.csv

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -516,16 +516,16 @@ c,CERT-C,ERR32-C,Yes,Rule,,,Do not rely on indeterminate values of errno,,Contra
516516
c,CERT-C,ERR33-C,Yes,Rule,,,Detect and handle standard library errors,MEM52-CPP,Contracts,Hard,
517517
c,CERT-C,ERR34-C,OutOfScope,Rule,,,Detect errors when converting a string to a number,,,,
518518
c,CERT-C,EXP30-C,Yes,Rule,,,Do not depend on the order of evaluation for side effects,EXP50-CPP,SideEffects1,Easy,
519-
c,CERT-C,EXP32-C,Yes,Rule,,,Do not access a volatile object through a nonvolatile reference,,Pointers2,Easy,
519+
c,CERT-C,EXP32-C,Yes,Rule,,,Do not access a volatile object through a nonvolatile reference,,Pointers3,Easy,
520520
c,CERT-C,EXP33-C,Yes,Rule,,,Do not read uninitialized memory,EXP53-CPP,InvalidMemory,Easy,
521521
c,CERT-C,EXP34-C,Yes,Rule,,,Do not dereference null pointers,A5-3-2,InvalidMemory,Medium,
522522
c,CERT-C,EXP35-C,Yes,Rule,,,Do not modify objects with temporary lifetime,,InvalidMemory,Hard,
523-
c,CERT-C,EXP36-C,Yes,Rule,,,Do not cast pointers into more strictly aligned pointer types,,Pointers2,Medium,
523+
c,CERT-C,EXP36-C,Yes,Rule,,,Do not cast pointers into more strictly aligned pointer types,,Pointers3,Medium,
524524
c,CERT-C,EXP37-C,Yes,Rule,,,Call functions with the correct number and type of arguments,,Expressions,Easy,
525-
c,CERT-C,EXP39-C,Yes,Rule,,,Do not access a variable through a pointer of an incompatible type,,Pointers2,Medium,
525+
c,CERT-C,EXP39-C,Yes,Rule,,,Do not access a variable through a pointer of an incompatible type,,Pointers3,Medium,
526526
c,CERT-C,EXP40-C,Yes,Rule,,,Do not modify constant objects,,Contracts,Medium,
527527
c,CERT-C,EXP42-C,Yes,Rule,,,Do not compare padding data,,Memory,Medium,
528-
c,CERT-C,EXP43-C,Yes,Rule,,,Avoid undefined behavior when using restrict-qualified pointers,,Pointers2,Medium,
528+
c,CERT-C,EXP43-C,Yes,Rule,,,Avoid undefined behavior when using restrict-qualified pointers,,Pointers3,Medium,
529529
c,CERT-C,EXP44-C,Yes,Rule,,,"Do not rely on side effects in operands to sizeof, _Alignof, or _Generic",M5-3-4,SideEffects1,Medium,
530530
c,CERT-C,EXP45-C,Yes,Rule,,,Do not perform assignments in selection statements,M6-2-1,SideEffects1,Medium,
531531
c,CERT-C,EXP46-C,Yes,Rule,,,Do not use a bitwise operator with a Boolean-like operand,,Expressions,Easy,

0 commit comments

Comments
 (0)