Skip to content

Commit

Permalink
misra: implement rule 22.5
Browse files Browse the repository at this point in the history
  • Loading branch information
danmar committed Jul 7, 2021
1 parent cf049cb commit 4ecf3cc
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
5 changes: 5 additions & 0 deletions addons/cppcheckdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ def getKnownIntValue(self):
return value.intvalue
return None

def isUnaryOp(self, op):
return self.astOperand1 and (self.astOperand2 is None) and self.str == op

def isBinaryOp(self):
return self.astOperand1 and self.astOperand2

class Scope:
"""
Expand Down
8 changes: 8 additions & 0 deletions addons/misra.py
Original file line number Diff line number Diff line change
Expand Up @@ -2872,6 +2872,13 @@ def misra_21_12(self, data):
'fetestexcept')):
self.reportError(token, 21, 12)

def misra_22_5(self, cfg):
for token in cfg.tokenlist:
if token.isUnaryOp("*") or (token.isBinaryOp() and token.str == '.'):
fileptr = token.astOperand1
if fileptr.variable and cppcheckdata.simpleMatch(fileptr.variable.typeStartToken, 'FILE *'):
self.reportError(token, 22, 5)

def get_verify_expected(self):
"""Return the list of expected violations in the verify test"""
return self.verify_expected
Expand Down Expand Up @@ -3414,6 +3421,7 @@ def fillVerifyExpected(verify_expected, tok):
self.executeCheck(2111, self.misra_21_11, cfg)
self.executeCheck(2112, self.misra_21_12, cfg)
# 22.4 is already covered by Cppcheck writeReadOnlyFile
self.executeCheck(2205, self.misra_22_5, cfg)

def analyse_ctu_info(self, files):
all_typedef_info = []
Expand Down
7 changes: 6 additions & 1 deletion addons/test/misra/misra-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
typedef struct {
union { // 19.2
struct {
unsigned a : 2;
unsigned a : 2; // 8.1
unsigned : 14;
};
uint16_t value;
Expand Down Expand Up @@ -1698,3 +1698,8 @@ static uint8_t misra_13_1_large_bad[1024] = { // 13.1
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

void misra_22_5(FILE *f) {
int x = *f; // 22.5
int y = f->pos; // 22.5
}

0 comments on commit 4ecf3cc

Please sign in to comment.