Fix #12079: Misra: calling unknown function in condition => false pos… (#5568)

…itive 14.4, missing misra-config warning
This commit is contained in:
Swasti Shrivastava 2023-10-19 23:08:20 +05:30 committed by GitHub
parent f2c8153231
commit 04ece4f65b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 24 deletions

View File

@ -2805,7 +2805,9 @@ class MisraChecker:
continue continue
if not token.astOperand1 or not (token.astOperand1.str in ['if', 'while']): if not token.astOperand1 or not (token.astOperand1.str in ['if', 'while']):
continue continue
if not isBoolExpression(token.astOperand2): if isBoolExpression(token.astOperand2):
continue
if token.astOperand2.valueType:
self.reportError(token, 14, 4) self.reportError(token, 14, 4)
def misra_15_1(self, data): def misra_15_1(self, data):
@ -3185,6 +3187,39 @@ class MisraChecker:
for w in cfg.clang_warnings: for w in cfg.clang_warnings:
if w['message'].endswith('[-Wimplicit-function-declaration]'): if w['message'].endswith('[-Wimplicit-function-declaration]'):
self.reportError(cppcheckdata.Location(w), 17, 3) self.reportError(cppcheckdata.Location(w), 17, 3)
for token in cfg.tokenlist:
if token.str not in ["while", "if"]:
continue
if token.next.str != "(":
continue
tok = token.next
end_token = token.next.link
while tok != end_token:
if tok.isName and tok.function is None and tok.valueType is None and tok.next.str == "(" and \
tok.next.valueType is None and not isKeyword(tok.str) and not isStdLibId(tok.str):
self.reportError(tok, 17, 3)
break
tok = tok.next
def misra_config(self, data):
for token in data.tokenlist:
if token.str not in ["while", "if"]:
continue
if token.next.str != "(":
continue
tok = token.next
while tok != token.next.link:
if tok.str == "(" and tok.isCast:
tok = tok.link
continue
if not tok.isName or tok.function or tok.variable or tok.varId or tok.valueType \
or tok.next.str == "(" or tok.str in ["EOF"] \
or isKeyword(tok.str) or isStdLibId(tok.str):
tok = tok.next
continue
errmsg = tok.str + " Variable is unknown"
self.reportError(token, 0, 0, "config")
break
def misra_17_6(self, rawTokens): def misra_17_6(self, rawTokens):
for token in rawTokens: for token in rawTokens:
@ -4093,21 +4128,30 @@ class MisraChecker:
self.addSuppressedRule(ruleNum) self.addSuppressedRule(ruleNum)
def reportError(self, location, num1, num2): def reportError(self, location, num1, num2, other_id = None):
ruleNum = num1 * 100 + num2 if not other_id:
ruleNum = num1 * 100 + num2
else:
ruleNum = other_id
if self.isRuleGloballySuppressed(ruleNum): if self.isRuleGloballySuppressed(ruleNum):
return return
if self.settings.verify: if self.settings.verify:
self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2)) if not other_id:
self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2))
else:
self.verify_actual.append('%s:%d %s' % (location.file, location.linenr, other_id))
elif self.isRuleSuppressed(location.file, location.linenr, ruleNum): elif self.isRuleSuppressed(location.file, location.linenr, ruleNum):
# Error is suppressed. Ignore # Error is suppressed. Ignore
self.suppressionStats.setdefault(ruleNum, 0) self.suppressionStats.setdefault(ruleNum, 0)
self.suppressionStats[ruleNum] += 1 self.suppressionStats[ruleNum] += 1
return return
else: else:
errorId = 'c2012-' + str(num1) + '.' + str(num2) if not other_id:
errorId = 'c2012-' + str(num1) + '.' + str(num2)
else:
errorId = 'c2012-' + other_id
misra_severity = 'Undefined' misra_severity = 'Undefined'
cppcheck_severity = 'style' cppcheck_severity = 'style'
if ruleNum in self.ruleTexts: if ruleNum in self.ruleTexts:
@ -4293,7 +4337,7 @@ class MisraChecker:
rule_re = re.compile(r'[0-9]+\.[0-9]+') rule_re = re.compile(r'[0-9]+\.[0-9]+')
if tok.str.startswith('//') and 'TODO' not in tok.str: if tok.str.startswith('//') and 'TODO' not in tok.str:
for word in tok.str[2:].split(' '): for word in tok.str[2:].split(' '):
if rule_re.match(word): if rule_re.match(word) or word == "config":
verify_expected.append('%s:%d %s' % (tok.file, tok.linenr, word)) verify_expected.append('%s:%d %s' % (tok.file, tok.linenr, word))
data = cppcheckdata.parsedump(dumpfile) data = cppcheckdata.parsedump(dumpfile)
@ -4431,6 +4475,7 @@ class MisraChecker:
self.executeCheck(1701, self.misra_17_1, cfg) self.executeCheck(1701, self.misra_17_1, cfg)
self.executeCheck(1702, self.misra_17_2, cfg) self.executeCheck(1702, self.misra_17_2, cfg)
self.executeCheck(1703, self.misra_17_3, cfg) self.executeCheck(1703, self.misra_17_3, cfg)
self.misra_config(cfg)
if cfgNumber == 0: if cfgNumber == 0:
self.executeCheck(1706, self.misra_17_6, data.rawTokens) self.executeCheck(1706, self.misra_17_6, data.rawTokens)
self.executeCheck(1707, self.misra_17_7, cfg) self.executeCheck(1707, self.misra_17_7, cfg)
@ -4751,7 +4796,7 @@ def main():
for misra_id in ids: for misra_id in ids:
rules_violated[misra_id] = rules_violated.get(misra_id, 0) + 1 rules_violated[misra_id] = rules_violated.get(misra_id, 0) + 1
print("MISRA rules violated:") print("MISRA rules violated:")
convert = lambda text: int(text) if text.isdigit() else text convert = lambda text: int(text) if text.isdigit() else 0
misra_sort = lambda key: [convert(c) for c in re.split(r'[\.-]([0-9]*)', key)] misra_sort = lambda key: [convert(c) for c in re.split(r'[\.-]([0-9]*)', key)]
for misra_id in sorted(rules_violated.keys(), key=misra_sort): for misra_id in sorted(rules_violated.keys(), key=misra_sort):
res = re.match(r'misra-c2012-([0-9]+)\\.([0-9]+)', misra_id) res = re.match(r'misra-c2012-([0-9]+)\\.([0-9]+)', misra_id)

View File

@ -21,13 +21,13 @@ static void foo(void)
{ {
for(i = 0; i < 10; i++) for(i = 0; i < 10; i++)
{ {
if(misra_5_2_func3()) //14.4 if(misra_5_2_func3()) //17.3
{ {
int misra_5_2_var_hides_var_1____31x; int misra_5_2_var_hides_var_1____31x;
int misra_5_2_var_hides_var_1____31y;//5.2 int misra_5_2_var_hides_var_1____31y;//5.2
} }
} }
} while(misra_5_2_func2()); //14.4 } while(misra_5_2_func2()); //17.3
} }
} }
} }

View File

@ -58,7 +58,7 @@ typedef unsigned int u32;
typedef signed int s32; typedef signed int s32;
typedef unsigned long long u64; typedef unsigned long long u64;
static void misra_1_2(void) static void misra_1_2(bool expr)
{ {
(void)(condition ? : 0); // 1.2 (void)(condition ? : 0); // 1.2
a = 1 + ({if (!expr) {code;} 1;}); // 1.2 a = 1 + ({if (!expr) {code;} 1;}); // 1.2
@ -67,7 +67,7 @@ static void misra_1_2(void)
static _Atomic int misra_1_4_var; // 1.4 static _Atomic int misra_1_4_var; // 1.4
static _Noreturn void misra_1_4_func(void) // 1.4 static _Noreturn void misra_1_4_func(void) // 1.4
{ {
if (0 != _Generic(misra_1_4_var)) {} // 1.4 if (0 != _Generic(misra_1_4_var)) {} // 1.4 17.3
printf_s("hello"); // 1.4 printf_s("hello"); // 1.4
} }
@ -160,13 +160,13 @@ static void foo(void)
{ {
for(i = 0; i < 10; i++) for(i = 0; i < 10; i++)
{ {
if(misra_5_2_func3()) //14.4 if(misra_5_2_func3()) //17.3
{ {
int misra_5_2_var_hides_var_1____31x; int misra_5_2_var_hides_var_1____31x;
int misra_5_2_var_hides_var_1____31y;//5.2 int misra_5_2_var_hides_var_1____31y;//5.2
} }
} }
} while(misra_5_2_func2()); //14.4 } while(misra_5_2_func2()); //17.3
} }
break; break;
} }
@ -252,11 +252,11 @@ static void misra_5_5_func1(void)
{ {
do do
{ {
if(misra_5_5_func3()) //14.4 if(misra_5_5_func3()) //17.3
{ {
int misra_5_5_hides_macro________31y; //5.5 int misra_5_5_hides_macro________31y; //5.5
} }
} while(misra_5_5_func2()); //14.4 } while(misra_5_5_func2()); //17.3
} }
break; break;
} }
@ -1143,12 +1143,15 @@ static s13_4_t s13_4 =
.string = STRING_DEF_13_4 // no-warning .string = STRING_DEF_13_4 // no-warning
}; };
static void misra_13_4(void) { static void misra_13_4(int x, int z) {
int y;
if (x != (y = z)) {} // 13.4 if (x != (y = z)) {} // 13.4
else {} else {}
} }
static void misra_13_5(void) { static void misra_13_5(void) {
int x = 0;
int y = 0;
if (x && (y++ < 123)){} // 13.5 if (x && (y++ < 123)){} // 13.5
if (x || ((y += 19) > 33)){} // 13.5 if (x || ((y += 19) > 33)){} // 13.5
if (x || ((y = 25) > 33)){} // 13.5 13.4 if (x || ((y = 25) > 33)){} // 13.5 13.4
@ -1297,13 +1300,16 @@ struct {
unsigned int y:1; unsigned int y:1;
} r14_4_struct; // 8.4 } r14_4_struct; // 8.4
static void misra_14_4(bool b) { static void misra_14_4(bool b) {
if (x+4){} // 14.4 if (x+4){} //config
else {} else {}
if (b) {} if (b) {}
else {} else {}
if (r14_4_struct.x) {} if (r14_4_struct.x) {}
// #12079
if (z) {} //config
} }
static void misra_15_1(void) { static void misra_15_1(void) {
@ -1316,7 +1322,9 @@ label:
goto label; // 15.2 15.1 goto label; // 15.2 15.1
} }
static void misra_15_3(void) { static void misra_15_3(int a) {
int x = 0;
int y;
if (x!=0) { if (x!=0) {
goto L1; // 15.3 15.1 goto L1; // 15.3 15.1
if (y!=0) { if (y!=0) {
@ -1431,14 +1439,14 @@ static void misra_15_4(void) {
} }
} }
static int misra_15_5(void) { static int misra_15_5(int x) {
if (x!=0) { if (x!=0) {
return 1; // 15.5 return 1; // 15.5
} else {} } else {}
return 2; return 2;
} }
static void misra_15_6(void) { static void misra_15_6(int x) {
if (x!=0); // 15.6 if (x!=0); // 15.6
else{} else{}
@ -1470,7 +1478,7 @@ static void misra_15_6_fp(void)
#if defined(M_20_9) && M_20_9 > 1 // no-warning (#10380) #if defined(M_20_9) && M_20_9 > 1 // no-warning (#10380)
#endif #endif
static void misra_15_7(void) { static void misra_15_7(int x, int a, int b) {
uint32_t var = 0; uint32_t var = 0;
uint32_t var2 = 0; uint32_t var2 = 0;
@ -1506,7 +1514,7 @@ static void misra_16_1(int32_t i) {
} }
} }
static void misra_16_2(void) { static void misra_16_2(int y) {
switch (x) { switch (x) {
default: default:
break; break;
@ -1518,7 +1526,8 @@ static void misra_16_2(void) {
} }
} }
static void misra_16_3(void) { static void misra_16_3(int b) {
int a;
switch (x) { switch (x) {
case 1: case 1:
case 2: case 2:
@ -1690,6 +1699,16 @@ static void misra_17_2_5(void) {
misra_17_2_1(); // no-warning misra_17_2_1(); // no-warning
} }
bool (*dostuff)(); //8.2 8.4
static void misra_17_3(void) {
if (dostuff()) {}
}
static void misra_config(const char* str) {
if (strlen(str) > 3){} //10.4
if (sizeof(int) > 1){} //10.4
}
static void misra_17_6(int x[static 20]) {(void)x;} // 17.6 static void misra_17_6(int x[static 20]) {(void)x;} // 17.6
static int calculation(int x) { return x + 1; } static int calculation(int x) { return x + 1; }

View File

@ -1,5 +1,5 @@
misra-c2012-21.6:*/misra-suppressions1-test.c:7 misra-c2012-21.6:*/misra-suppressions1-test.c:7
misra-c2012-14.4 misra-c2012-17.3
misra-c2012-5.2 misra-c2012-5.2
misra-c2012-8.4:*/misra-suppressions1-test.c misra-c2012-8.4:*/misra-suppressions1-test.c
misra-c2012-16.4:*/misra-suppressions1-test.c misra-c2012-16.4:*/misra-suppressions1-test.c