Skip to content

Commit 56bfa9f

Browse files
authored
Fix #9498: false positive: misra-c2012-18.8 (#5600)
1 parent 083c4aa commit 56bfa9f

3 files changed

Lines changed: 37 additions & 33 deletions

File tree

addons/misra.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,14 @@ def isConstantExpression(expr):
897897
return False
898898
return True
899899

900+
def isUnknownConstantExpression(expr):
901+
if expr.isName and not isEnumConstant(expr) and expr.variable is None:
902+
return True
903+
if expr.astOperand1 and isUnknownConstantExpression(expr.astOperand1):
904+
return True
905+
if expr.astOperand2 and isUnknownConstantExpression(expr.astOperand2):
906+
return True
907+
return False
900908

901909
def isUnsignedInt(expr):
902910
return expr and expr.valueType and expr.valueType.type in ('short', 'int') and expr.valueType.sign == 'unsigned'
@@ -3258,6 +3266,32 @@ def misra_17_3(self, cfg):
32583266
tok = tok.next
32593267

32603268
def misra_config(self, data):
3269+
for var in data.variables:
3270+
if not var.isArray or var.nameToken is None or not cppcheckdata.simpleMatch(var.nameToken.next, '['):
3271+
continue
3272+
tok = var.nameToken.next
3273+
while tok.str == '[':
3274+
sz = tok.astOperand2
3275+
if sz and sz.getKnownIntValue() is None:
3276+
has_var = False
3277+
unknown_constant = False
3278+
tokens = [sz]
3279+
while len(tokens) > 0:
3280+
t = tokens[-1]
3281+
tokens = tokens[:-1]
3282+
if t:
3283+
if t.isName and t.getKnownIntValue() is None:
3284+
if t.varId or t.variable:
3285+
has_var = True
3286+
continue
3287+
unknown_constant = True
3288+
self.report_config_error(tok, 'Unknown constant {}, please review configuration'.format(t.str))
3289+
if t.isArithmeticalOp:
3290+
tokens += [t.astOperand1, t.astOperand2]
3291+
if not unknown_constant and not has_var:
3292+
self.report_config_error(tok, 'Unknown array size, please review configuration')
3293+
tok = tok.link.next
3294+
32613295
for token in data.tokenlist:
32623296
if token.str not in ("while", "if"):
32633297
continue
@@ -3367,7 +3401,7 @@ def misra_18_8(self, data):
33673401
# Unknown define or syntax error
33683402
if not typetok.astOperand2:
33693403
continue
3370-
if not isConstantExpression(typetok.astOperand2):
3404+
if not isConstantExpression(typetok.astOperand2) and not isUnknownConstantExpression(typetok.astOperand2):
33713405
self.reportError(var.nameToken, 18, 8)
33723406

33733407
def misra_19_2(self, data):

addons/misra_9.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -396,38 +396,6 @@ def unwindAndContinue(self):
396396
break
397397

398398
def misra_9_x(self, data, rule, rawTokens = None):
399-
# If there are arrays with unknown size constants then we need to warn about missing configuration
400-
# and bailout
401-
has_config_errors = False
402-
for var in data.variables:
403-
if not var.isArray or var.nameToken is None or not cppcheckdata.simpleMatch(var.nameToken.next,'['):
404-
continue
405-
tok = var.nameToken.next
406-
while tok.str == '[':
407-
sz = tok.astOperand2
408-
if sz and sz.getKnownIntValue() is None:
409-
has_var = False
410-
unknown_constant = False
411-
tokens = [sz]
412-
while len(tokens) > 0:
413-
t = tokens[-1]
414-
tokens = tokens[:-1]
415-
if t:
416-
if t.isName and t.getKnownIntValue() is None:
417-
if t.varId or t.variable:
418-
has_var = True
419-
continue
420-
unknown_constant = True
421-
cppcheckdata.reportError(sz, 'error', 'Unknown constant {}, please review configuration'.format(t.str), 'misra', 'config')
422-
has_config_errors = True
423-
if t.isArithmeticalOp:
424-
tokens += [t.astOperand1, t.astOperand2]
425-
if not unknown_constant and not has_var:
426-
cppcheckdata.reportError(sz, 'error', 'Unknown array size, please review configuration', 'misra', 'config')
427-
has_config_errors = True
428-
tok = tok.link.next
429-
if has_config_errors:
430-
return
431399

432400
parser = InitializerParser()
433401

addons/test/misra/misra-test.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,6 +1783,8 @@ static void misra_18_8(int x) {
17831783
int buf1[10];
17841784
int buf2[sizeof(int)];
17851785
int vla[x]; // 18.8
1786+
// #9498
1787+
int vlb[y]; // config
17861788
static const unsigned char arr18_8_1[] = UNDEFINED_ID;
17871789
static uint32_t enum_test_0[R18_8_ENUM_CONSTANT_0] = {0};
17881790
}

0 commit comments

Comments
 (0)