Skip to content

Commit b03547b

Browse files
Merge pull request #967 from github/jeongsoolee09/MISRA-C++-2023-Memory
Implement `Memory1` (`RULE-8-7-1`)
2 parents 4e83559 + b6eff54 commit b03547b

15 files changed

Lines changed: 3072 additions & 2 deletions

cpp/common/src/codingstandards/cpp/OutOfBounds.qll

Lines changed: 1364 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Memory1Query =
7+
TPointerArithmeticFormsAnInvalidPointerQuery() or
8+
TPointerArgumentToCstringFunctionIsInvalidQuery()
9+
10+
predicate isMemory1QueryMetadata(Query query, string queryId, string ruleId, string category) {
11+
query =
12+
// `Query` instance for the `pointerArithmeticFormsAnInvalidPointer` query
13+
Memory1Package::pointerArithmeticFormsAnInvalidPointerQuery() and
14+
queryId =
15+
// `@id` for the `pointerArithmeticFormsAnInvalidPointer` query
16+
"cpp/misra/pointer-arithmetic-forms-an-invalid-pointer" and
17+
ruleId = "RULE-8-7-1" and
18+
category = "required"
19+
or
20+
query =
21+
// `Query` instance for the `pointerArgumentToCstringFunctionIsInvalid` query
22+
Memory1Package::pointerArgumentToCstringFunctionIsInvalidQuery() and
23+
queryId =
24+
// `@id` for the `pointerArgumentToCstringFunctionIsInvalid` query
25+
"cpp/misra/pointer-argument-to-cstring-function-is-invalid" and
26+
ruleId = "RULE-8-7-1" and
27+
category = "required"
28+
}
29+
30+
module Memory1Package {
31+
Query pointerArithmeticFormsAnInvalidPointerQuery() {
32+
//autogenerate `Query` type
33+
result =
34+
// `Query` type for `pointerArithmeticFormsAnInvalidPointer` query
35+
TQueryCPP(TMemory1PackageQuery(TPointerArithmeticFormsAnInvalidPointerQuery()))
36+
}
37+
38+
Query pointerArgumentToCstringFunctionIsInvalidQuery() {
39+
//autogenerate `Query` type
40+
result =
41+
// `Query` type for `pointerArgumentToCstringFunctionIsInvalid` query
42+
TQueryCPP(TMemory1PackageQuery(TPointerArgumentToCstringFunctionIsInvalidQuery()))
43+
}
44+
}

cpp/common/src/codingstandards/cpp/exclusions/cpp/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import Linkage2
6565
import Literals
6666
import Loops
6767
import Macros
68+
import Memory1
6869
import Memory2
6970
import Memory3
7071
import Memory4
@@ -171,6 +172,7 @@ newtype TCPPQuery =
171172
TLiteralsPackageQuery(LiteralsQuery q) or
172173
TLoopsPackageQuery(LoopsQuery q) or
173174
TMacrosPackageQuery(MacrosQuery q) or
175+
TMemory1PackageQuery(Memory1Query q) or
174176
TMemory2PackageQuery(Memory2Query q) or
175177
TMemory3PackageQuery(Memory3Query q) or
176178
TMemory4PackageQuery(Memory4Query q) or
@@ -277,6 +279,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
277279
isLiteralsQueryMetadata(query, queryId, ruleId, category) or
278280
isLoopsQueryMetadata(query, queryId, ruleId, category) or
279281
isMacrosQueryMetadata(query, queryId, ruleId, category) or
282+
isMemory1QueryMetadata(query, queryId, ruleId, category) or
280283
isMemory2QueryMetadata(query, queryId, ruleId, category) or
281284
isMemory3QueryMetadata(query, queryId, ruleId, category) or
282285
isMemory4QueryMetadata(query, queryId, ruleId, category) or

cpp/common/test/includes/standard-library/cstdlib

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,8 @@ using ::strtoll;
2626
using ::strtoul;
2727
using ::strtoull;
2828
using ::system;
29+
using ::malloc;
30+
using ::calloc;
31+
using ::realloc;
2932
} // namespace std
3033
#endif // _GHLIBCPP_CSTDLIB

cpp/common/test/includes/standard-library/stdlib.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,10 @@ long double strtold(const char *str, char **endptr);
3737

3838
int rand(void);
3939

40+
int mblen (const char *, size_t);
41+
int mbtowc (wchar_t *__restrict, const char *__restrict, size_t);
42+
int wctomb (char *, wchar_t);
43+
size_t mbstowcs (wchar_t *__restrict, const char *__restrict, size_t);
44+
size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
45+
4046
#endif // _GHLIBCPP_STDLIB

cpp/common/test/includes/standard-library/string.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,6 @@ void *memcpy(void *dest, const void *src, size_t count);
4343
void *memset(void *dest, int ch, size_t count);
4444
void *memmove(void *dest, const void *src, size_t count);
4545
int memcmp(const void *lhs, const void *rhs, size_t count);
46+
void *memchr (const void *, int, size_t);
4647

47-
#endif // _GHLIBCPP_STRINGH
48+
#endif // _GHLIBCPP_STRINGH

cpp/common/test/includes/standard-library/wchar.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ double wcstod(const wchar_t *str, wchar_t **endptr);
1717
float wcstof(const wchar_t *str, wchar_t **endptr);
1818
long double wcstold(const wchar_t *str, wchar_t **endptr);
1919

20+
size_t wcsftime (wchar_t *__restrict, size_t, const wchar_t *__restrict, const struct tm *__restrict);
21+
size_t wcsxfrm (wchar_t *__restrict, const wchar_t *__restrict, size_t);
22+
2023
// Character classification and conversion types
2124
typedef struct {
2225
int __count;
@@ -26,4 +29,4 @@ typedef struct {
2629
} __value;
2730
} mbstate_t;
2831

29-
#endif // _GHLIBCPP_WCHAR
32+
#endif // _GHLIBCPP_WCHAR
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id cpp/misra/pointer-argument-to-cstring-function-is-invalid
3+
* @name RULE-8-7-1: Pointer and index arguments passed to functions in <cstring> shall not be invalid
4+
* @description Pointer and index arguments passed to functions in <cstring> should result in valid
5+
* reads and/or writes.
6+
* @kind problem
7+
* @precision high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-7-1
10+
* scope/system
11+
* correctness
12+
* security
13+
* external/misra/enforcement/undecidable
14+
* external/misra/obligation/required
15+
*/
16+
17+
import cpp
18+
import codingstandards.cpp.misra
19+
import codingstandards.cpp.OutOfBounds // for OOB::problems
20+
import codingstandards.cpp.Exclusions // for isExcluded(Element, Query)
21+
22+
from
23+
OOB::BufferAccessLibraryFunctionCall fc, string message, Expr bufferArg, string bufferArgStr,
24+
Expr sizeOrOtherBufferArg, string otherStr
25+
where
26+
not isExcluded(fc, Memory1Package::pointerArgumentToCstringFunctionIsInvalidQuery()) and
27+
OOB::problems(fc, message, bufferArg, bufferArgStr, sizeOrOtherBufferArg, otherStr)
28+
select fc, message, bufferArg, bufferArgStr, sizeOrOtherBufferArg, otherStr

0 commit comments

Comments
 (0)