From 682c4510a94d8d7a9b6ce6313b250f2d2fb31e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 15 Aug 2021 13:39:14 +0200 Subject: [PATCH] misra: implement rule 1.4 --- addons/misra.py | 19 +++++++++++++++++++ addons/test/misra/misra-test.c | 8 ++++++++ 2 files changed, 27 insertions(+) diff --git a/addons/misra.py b/addons/misra.py index fa2c860b9..d4530e449 100755 --- a/addons/misra.py +++ b/addons/misra.py @@ -1367,6 +1367,24 @@ class MisraChecker: cppcheckdata.reportSummary(dumpfile, 'MisraUsage', names) + def misra_1_4(self, cfg): + for token in cfg.tokenlist: + if token.str in ('_Atomic', '_Noreturn', '_Generic', '_Thread_local', '_Alignas', '_Alignof'): + self.reportError(token, 1, 4) + if token.str.endswith('_s') and isFunctionCall(token.next): + # See C specification C11 - Annex K, page 578 + if token.str in ('tmpfile_s', 'tmpnam_s', 'fopen_s', 'freopen_s', 'fprintf_s', 'fscanf_s', 'printf_s', 'scanf_s', + 'snprintf_s', 'sprintf_s', 'sscanf_s', 'vfprintf_s', 'vfscanf_s', 'vprintf_s', 'vscanf_s', + 'vsnprintf_s', 'vsprintf_s', 'vsscanf_s', 'gets_s', 'set_constraint_handler_s', 'abort_handler_s', + 'ignore_handler_s', 'getenv_s', 'bsearch_s', 'qsort_s', 'wctomb_s', 'mbstowcs_s', 'wcstombs_s', + 'memcpy_s', 'memmove_s', 'strcpy_s', 'strncpy_s', 'strcat_s', 'strncat_s', 'strtok_s', 'memset_s', + 'strerror_s', 'strerrorlen_s', 'strnlen_s', 'asctime_s', 'ctime_s', 'gmtime_s', 'localtime_s', + 'fwprintf_s', 'fwscanf_s', 'snwprintf_s', 'swprintf_s', 'swscanf_s', 'vfwprintf_s', 'vfwscanf_s', + 'vsnwprintf_s', 'vswprintf_s', 'vswscanf_s', 'vwprintf_s', 'vwscanf_s', 'wprintf_s', 'wscanf_s', + 'wcscpy_s', 'wcsncpy_s', 'wmemcpy_s', 'wmemmove_s', 'wcscat_s', 'wcsncat_s', 'wcstok_s', 'wcsnlen_s', + 'wcrtomb_s', 'mbsrtowcs_s', 'wcsrtombs_s'): + self.reportError(token, 1, 4) + def misra_2_3(self, dumpfile, typedefInfo): self._save_ctu_summary_typedefs(dumpfile, typedefInfo) @@ -3971,6 +3989,7 @@ class MisraChecker: if not self.settings.quiet: self.printStatus('Checking %s, config %s...' % (dumpfile, cfg.name)) + self.executeCheck(104, self.misra_1_4, cfg) self.executeCheck(203, self.misra_2_3, dumpfile, cfg.typedefInfo) self.executeCheck(204, self.misra_2_4, dumpfile, cfg) self.executeCheck(205, self.misra_2_5, dumpfile, cfg) diff --git a/addons/test/misra/misra-test.c b/addons/test/misra/misra-test.c index 513b56bdf..1110fce93 100644 --- a/addons/test/misra/misra-test.c +++ b/addons/test/misra/misra-test.c @@ -58,6 +58,14 @@ typedef unsigned int u32; typedef signed int s32; typedef unsigned long long u64; +static _Atomic int misra_1_4_var; // 1.4 +static _Noreturn void misra_1_4_func(void) // 1.4 +{ + if (0 != _Generic(misra_1_4_var)) {} // 1.4 + printf_s("hello"); // 1.4 +} + + /* // */ // 3.1 /* /* */ // 3.1 ////