misra: implement rule 22.10

This commit is contained in:
Daniel Marjamäki 2021-08-13 18:03:37 +02:00
parent 2a3657154b
commit 4b126db9ca
2 changed files with 35 additions and 1 deletions

View File

@ -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 = []

View File

@ -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 ) {}
}