Skip to content

Commit

Permalink
Fix #12079: Misra: calling unknown function in condition => false pos…
Browse files Browse the repository at this point in the history
…itive 14.4, missing misra-config warning
  • Loading branch information
swasti16 committed Oct 18, 2023
1 parent dd76504 commit 774766d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
45 changes: 40 additions & 5 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2805,7 +2805,9 @@ def misra_14_4(self, data):
continue
if not token.astOperand1 or not (token.astOperand1.str in ['if', 'while']):
continue
if not isBoolExpression(token.astOperand2):
if isBoolExpression(token.astOperand2):
continue
if token.astOperand2.valueType:
self.reportError(token, 14, 4)

def misra_15_1(self, data):
Expand Down Expand Up @@ -3185,7 +3187,36 @@ def misra_17_3(self, cfg):
for w in cfg.clang_warnings:
if w['message'].endswith('[-Wimplicit-function-declaration]'):
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
while (tok != token.next.link):
if tok.isName and not tok.function and tok.next.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 ["errno","EOF"]:
tok = tok.next
continue
errmsg = tok.str + " Variable is unknown"
# cppcheckdata.reportError(tok, "style", errmsg, 'misra', "config", [])
self.reportError(token, 0, 0,"misra-config")
break
def misra_17_6(self, rawTokens):
for token in rawTokens:
if simpleMatch(token, '[ static'):
Expand Down Expand Up @@ -4093,14 +4124,17 @@ def setSuppressionList(self, suppressionlist):

self.addSuppressedRule(ruleNum)

def reportError(self, location, num1, num2):
def reportError(self, location, num1, num2,otherID = None):
ruleNum = num1 * 100 + num2

if self.isRuleGloballySuppressed(ruleNum):
return

if self.settings.verify:
self.verify_actual.append('%s:%d %d.%d' % (location.file, location.linenr, num1, num2))
if not otherID:
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, otherID))
elif self.isRuleSuppressed(location.file, location.linenr, ruleNum):
# Error is suppressed. Ignore
self.suppressionStats.setdefault(ruleNum, 0)
Expand Down Expand Up @@ -4293,7 +4327,7 @@ def fillVerifyExpected(verify_expected, tok):
rule_re = re.compile(r'[0-9]+\.[0-9]+')
if tok.str.startswith('//') and 'TODO' not in tok.str:
for word in tok.str[2:].split(' '):
if rule_re.match(word):
if rule_re.match(word) or word == "misra-config":
verify_expected.append('%s:%d %s' % (tok.file, tok.linenr, word))

data = cppcheckdata.parsedump(dumpfile)
Expand Down Expand Up @@ -4431,6 +4465,7 @@ def fillVerifyExpected(verify_expected, tok):
self.executeCheck(1701, self.misra_17_1, cfg)
self.executeCheck(1702, self.misra_17_2, cfg)
self.executeCheck(1703, self.misra_17_3, cfg)
self.misra_config(cfg)
if cfgNumber == 0:
self.executeCheck(1706, self.misra_17_6, data.rawTokens)
self.executeCheck(1707, self.misra_17_7, cfg)
Expand Down
57 changes: 30 additions & 27 deletions addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ typedef unsigned long long u64;
static void misra_1_2(void)
{
(void)(condition ? : 0); // 1.2
a = 1 + ({if (!expr) {code;} 1;}); // 1.2
a = 1 + ({if (!expr) {code;} 1;}); // 1.2 misra-config
}

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
if (0 != _Generic(misra_1_4_var)) {} // 1.4 17.3
printf_s("hello"); // 1.4
}

Expand Down Expand Up @@ -160,13 +160,13 @@ static void foo(void)
{
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____31y;//5.2
}
}
} while(misra_5_2_func2()); //14.4
} while(misra_5_2_func2()); //17.3
}
break;
}
Expand Down Expand Up @@ -252,11 +252,11 @@ static void misra_5_5_func1(void)
{
do
{
if(misra_5_5_func3()) //14.4
if(misra_5_5_func3()) //17.3
{
int misra_5_5_hides_macro________31y; //5.5
}
} while(misra_5_5_func2()); //14.4
} while(misra_5_5_func2()); //17.3
}
break;
}
Expand Down Expand Up @@ -1144,15 +1144,15 @@ static s13_4_t s13_4 =
};

static void misra_13_4(void) {
if (x != (y = z)) {} // 13.4
if (x != (y = z)) {} // 13.4 misra-config
else {}
}

static void misra_13_5(void) {
if (x && (y++ < 123)){} // 13.5
if (x || ((y += 19) > 33)){} // 13.5
if (x || ((y = 25) > 33)){} // 13.5 13.4
if (x || ((--y) > 33)){} // 13.5
if (x && (y++ < 123)){} // 13.5 misra-config
if (x || ((y += 19) > 33)){} // 13.5 misra-config
if (x || ((y = 25) > 33)){} // 13.5 13.4 misra-config
if (x || ((--y) > 33)){} // 13.5 misra-config
else {}
}

Expand Down Expand Up @@ -1297,13 +1297,16 @@ struct {
unsigned int y:1;
} r14_4_struct; // 8.4
static void misra_14_4(bool b) {
if (x+4){} // 14.4
if (x+4){} //misra-config
else {}

if (b) {}
else {}

if (r14_4_struct.x) {}

// #12079
if (z) {} //misra-config
}

static void misra_15_1(void) {
Expand All @@ -1317,22 +1320,22 @@ static void misra_15_2(void) {
}

static void misra_15_3(void) {
if (x!=0) {
if (x!=0) { //misra-config
goto L1; // 15.3 15.1
if (y!=0) {
if (y!=0) { //misra-config
L1:
} else {}
} else {}

switch (x) {
case 0:
if (x == y) {
if (x == y) { //misra-config
goto L2; // 15.3 15.1
}
goto L2; // 15.3 15.1
L3:
foo();
if (a == 0x42) {
if (a == 0x42) { //misra-config
// Compliant:
goto L3; // 15.1 15.2
}
Expand Down Expand Up @@ -1432,14 +1435,14 @@ static void misra_15_4(void) {
}

static int misra_15_5(void) {
if (x!=0) {
if (x!=0) { // misra-config
return 1; // 15.5
} else {}
return 2;
}

static void misra_15_6(void) {
if (x!=0); // 15.6
if (x!=0); // 15.6 misra-config
else{}

#if A>1 // 20.9
Expand All @@ -1454,7 +1457,7 @@ static void misra_15_6(void) {
#endif
{ (void)0; } // no-warning

do {} while (x<0); // no-warning
do {} while (x<0); // misra-config
}

static void misra_15_6_fp(void)
Expand All @@ -1474,11 +1477,11 @@ static void misra_15_7(void) {
uint32_t var = 0;
uint32_t var2 = 0;

if (x!=0){} // no-warning
if (x!=0){} else if(x==1){} // 15.7
if (x!=0){} else if(x==1){}else{;} // no-warning
if (x!=0){} // misra-config
if (x!=0){} else if(x==1){} // 15.7 misra-config
if (x!=0){} else if(x==1){}else{;} // misra-config

if (x!=0)
if (x!=0) // misra-config
{
}
else
Expand All @@ -1491,8 +1494,8 @@ static void misra_15_7(void) {
} // no-warning
}

if (a==2) {} else if (b==4) {} // 15.7
if (a==2) {} else { if (b==4) {} } // no-warning
if (a==2) {} else if (b==4) {} // 15.7 misra-config
if (a==2) {} else { if (b==4) {} } // no-warning misra-config
}

static void misra_16_1(int32_t i) {
Expand All @@ -1511,7 +1514,7 @@ static void misra_16_2(void) {
default:
break;
case 1:
while (y>4) {
while (y>4) { //misra-config
case 2: break; // 16.2
}
break;
Expand All @@ -1535,7 +1538,7 @@ static void misra_16_3(void) {
a=4;
break;
case 9:
if (a==b) {
if (a==b) { //misra-config
break;
}
case 10: // 16.3
Expand Down

0 comments on commit 774766d

Please sign in to comment.