From 4b126db9ca7b0cafa9cf23609f1a062c80ccf789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 13 Aug 2021 18:03:37 +0200 Subject: [PATCH] misra: implement rule 22.10 --- addons/misra.py | 25 ++++++++++++++++++++++++- addons/test/misra/misra-test.c | 11 +++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/addons/misra.py b/addons/misra.py index 6ffb92933..4162e8886 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1082,7 +1082,7 @@ def generateTable(): numberOfRules[19] = 2 numberOfRules[20] = 14 numberOfRules[21] = 12 - numberOfRules[22] = 6 + numberOfRules[22] = 10 # Rules that can be checked with compilers: # compiler = ['1.1', '1.2'] @@ -3453,6 +3453,28 @@ class MisraChecker: if fileptr.variable and cppcheckdata.simpleMatch(fileptr.variable.typeStartToken, 'FILE *'): self.reportError(token, 22, 5) + def misra_22_10(self, cfg): + errno_setting_functions = ('ftell', 'fgetpos', 'fsetpos', 'fgetwc', 'fputwc' + 'strtoimax', 'strtoumax', 'strtol', 'strtoul', + 'strtoll', 'strtoull', 'strtof', 'strtod', 'strtold' + 'wcstoimax', 'wcstoumax', 'wcstol', 'wcstoul', + 'wcstoll', 'wcstoull', 'wcstof', 'wcstod', 'wcstold' + 'wcrtomb', 'wcsrtombs', 'mbrtowc') + + last_function_call = None + for token in cfg.tokenlist: + if token.str == '(' and not simpleMatch(token.link, ') {'): + name, args = cppcheckdata.get_function_call_name_args(token.previous) + last_function_call = name + if token.str == '}': + last_function_call = None + if token.str == 'errno' and token.astParent and token.astParent.isComparisonOp: + if last_function_call is None: + self.reportError(token, 22, 10) + if last_function_call not in errno_setting_functions: + self.reportError(token, 22, 10) + + def get_verify_expected(self): """Return the list of expected violations in the verify test""" return self.verify_expected @@ -4022,6 +4044,7 @@ class MisraChecker: self.executeCheck(2115, self.misra_21_15, cfg) # 22.4 is already covered by Cppcheck writeReadOnlyFile self.executeCheck(2205, self.misra_22_5, cfg) + self.executeCheck(2210, self.misra_22_10, cfg) def analyse_ctu_info(self, ctu_info_files): all_typedef_info = [] diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 40f78dbcb..496b05587 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -1601,6 +1601,7 @@ static int misra_21_1(void) { } static int _misra_21_1_2(void); // no warning #define errno 11 // 21.1 +#undef errno // 20.5 #define __BUILTIN_SOMETHING 123 // 21.2 21.1 extern void *memcpy ( void *restrict s1, const void *restrict s2, size_t n ); // 21.2 8.14 @@ -1786,3 +1787,13 @@ static void misra_22_5(FILE *f) { int y = f->pos; // 22.5 } +static void misra_22_10(void) +{ + errno = 0; + f = atof ( "A.12" ); // 21.7 + if ( 0 == errno ) {} // 22.10 + + errno = 0; + f = strtod ( "A.12", NULL ); + if ( 0 == errno ) {} +}