misra: implement rule 21.16

This commit is contained in:
Daniel Marjamäki 2021-08-15 20:50:20 +02:00
parent ecba12a6b8
commit 864d6462d0
2 changed files with 30 additions and 1 deletions

View File

@ -3522,6 +3522,26 @@ class MisraChecker:
continue continue
self.reportError(token, 21, 15) self.reportError(token, 21, 15)
def misra_21_16(self, cfg):
for token in cfg.tokenlist:
if token.str != 'memcmp':
continue
name, args = cppcheckdata.get_function_call_name_args(token)
if name is None:
continue
if len(args) != 3:
continue
for arg in args[:2]:
if arg.valueType is None:
continue
if arg.valueType.pointer > 1:
continue
if arg.valueType.sign in ('unsigned', 'signed'):
continue
if arg.valueType.isEnum():
continue
self.reportError(token, 21, 16)
def misra_21_21(self, cfg): def misra_21_21(self, cfg):
for token in cfg.tokenlist: for token in cfg.tokenlist:
if token.str == 'system': if token.str == 'system':
@ -4168,6 +4188,7 @@ class MisraChecker:
self.executeCheck(2112, self.misra_21_12, cfg) self.executeCheck(2112, self.misra_21_12, cfg)
self.executeCheck(2114, self.misra_21_14, cfg) self.executeCheck(2114, self.misra_21_14, cfg)
self.executeCheck(2115, self.misra_21_15, cfg) self.executeCheck(2115, self.misra_21_15, cfg)
self.executeCheck(2116, self.misra_21_16, cfg)
self.executeCheck(2121, self.misra_21_21, cfg) self.executeCheck(2121, self.misra_21_21, cfg)
# 22.4 is already covered by Cppcheck writeReadOnlyFile # 22.4 is already covered by Cppcheck writeReadOnlyFile
self.executeCheck(2205, self.misra_22_5, cfg) self.executeCheck(2205, self.misra_22_5, cfg)

View File

@ -1652,7 +1652,7 @@ static void misra_21_12(void) {
static void misra_21_14(uint8_t *x) { static void misra_21_14(uint8_t *x) {
(void)strcpy(x, "123"); (void)strcpy(x, "123");
(void)memcmp(x, y, 100); // 21.14 (void)memcmp(x, y, 100); // 21.14
(void)memcmp("abc", y, 100); // 21.14 (void)memcmp("abc", y, 100); // 21.14 21.16
} }
static void misra_21_15(uint8_t *x, uint16_t *y) { static void misra_21_15(uint8_t *x, uint16_t *y) {
@ -1661,6 +1661,14 @@ static void misra_21_15(uint8_t *x, uint16_t *y) {
(void)memcmp(x, y, 10); // 21.15 (void)memcmp(x, y, 10); // 21.15
} }
struct misra_21_16_S { int a; int b; };
static void misra_21_16_f1(struct misra_21_16_S *s1, struct misra_21_16_S *s2) {
(void)memcmp(s1, s2, 10); // 21.16
}
static void misra_21_16_f2(char *x, char *y) {
(void)memcmp(x, y, 10); // 21.16
}
// Large arrays for R13.1. Size exceeds default Python's max recursion depth. // Large arrays for R13.1. Size exceeds default Python's max recursion depth.
static uint8_t misra_13_1_large_ok[1024] = { static uint8_t misra_13_1_large_ok[1024] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,