From 864d6462d06ba956cf5cad3d12759d88d0aed536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Aug 2021 20:50:20 +0200 Subject: [PATCH] misra: implement rule 21.16 --- addons/misra.py | 21 +++++++++++++++++++++ addons/test/misra/misra-test.c | 10 +++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/addons/misra.py b/addons/misra.py index e534b49aa..b9666e622 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -3522,6 +3522,26 @@ class MisraChecker: continue 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): for token in cfg.tokenlist: if token.str == 'system': @@ -4168,6 +4188,7 @@ class MisraChecker: self.executeCheck(2112, self.misra_21_12, cfg) self.executeCheck(2114, self.misra_21_14, 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) # 22.4 is already covered by Cppcheck writeReadOnlyFile self.executeCheck(2205, self.misra_22_5, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index c906f3c7e..6067de8e2 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -1652,7 +1652,7 @@ static void misra_21_12(void) { static void misra_21_14(uint8_t *x) { (void)strcpy(x, "123"); (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) { @@ -1661,6 +1661,14 @@ static void misra_21_15(uint8_t *x, uint16_t *y) { (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. 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,