Skip to content

Commit

Permalink
misra: Fix crash in misra_9x when there is unknown constant used as a…
Browse files Browse the repository at this point in the history
…rray size
  • Loading branch information
danmar committed Jul 10, 2023
1 parent 276aace commit 711e8e6
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/CI-unixish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ jobs:
run: |
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety addons/test/threadsafety
./cppcheck --error-exitcode=1 --inline-suppr --addon=threadsafety --std=c++03 addons/test/threadsafety
./cppcheck --error-exitcode=1 --addon=misra addons/test/misra/crash*.c
./cppcheck --error-exitcode=1 --inline-suppr --addon=misra addons/test/misra/crash*.c
./cppcheck --addon=misra --enable=style --inline-suppr --enable=information --error-exitcode=1 addons/test/misra/misra-ctu-*-test.c
pushd addons/test
# We'll force C89 standard to enable an additional verification for
Expand Down
30 changes: 30 additions & 0 deletions addons/misra_9.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import cppcheckdata

# Holds information about an array, struct or union's element definition.
class ElementDef:
def __init__(self, elementType, name, valueType, dimensions = None):
Expand Down Expand Up @@ -381,6 +383,34 @@ def unwindAndContinue(self):
break

def misra_9_x(self, data, rule, rawTokens = None):
# If there are arrays with unknown size constants then we need to warn about missing configuration
# and bailout
has_config_errors = False
for var in data.variables:
if not var.isArray or var.nameToken is None or not cppcheckdata.simpleMatch(var.nameToken.next,'['):
continue
tok = var.nameToken.next
while tok.str == '[':
sz = tok.astOperand2
if sz and sz.getKnownIntValue() is None:
has_config_errors = True
err = False
tokens = [sz]
while len(tokens) > 0:
t = tokens[-1]
tokens = tokens[:-1]
if t:
if t.isName and t.getKnownIntValue() is None:
err = True
cppcheckdata.reportError(sz, 'error', f'Unknown constant {t.str}, please review configuration', 'misra', 'config')
if t.isArithmeticalOp:
tokens += [t.astOperand1, t.astOperand2]
if not err:
cppcheckdata.reportError(sz, 'error', 'Unknown array size, please review configuration', 'misra', 'config')
tok = tok.link.next
if has_config_errors:
return

parser = InitializerParser()

for variable in data.variables:
Expand Down
1 change: 1 addition & 0 deletions addons/test/misra/crash4.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct ConDesDesc {
unsigned Import;
};

// cppcheck-suppress misra-config
static ConDesDesc ConDes[CD_TYPE_COUNT] = {
{ 0, 0 },
{ 0, 0 },
Expand Down
23 changes: 23 additions & 0 deletions addons/test/misra/crash5.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

struct _boardcnf_ch {
// cppcheck-suppress misra-config
uint8_t ddr_density[CS_CNT];
uint64_t ca_swap;
};

struct _boardcnf {

uint16_t dqdm_dly_r;
// cppcheck-suppress misra-config
struct _boardcnf_ch ch[DRAM_CH_CNT];
};

static const struct _boardcnf boardcnfs[1] = {
{
0x0a0,
{
{ {0x02, 0x02}, 0x00345201 },
{ {0x02, 0x02}, 0x00302154 }
}
},
};

0 comments on commit 711e8e6

Please sign in to comment.