From 9a7ecd622c3f2122052306549b82141519f2d0d8 Mon Sep 17 00:00:00 2001 From: fuzzelhjb Date: Wed, 26 Jun 2019 18:49:47 +0200 Subject: [PATCH] add CERT exp15-c check (#1897) * add CERT exp15-c check * fix false positive * remove useless check * fine tune check and test --- addons/cert.py | 10 ++++++++++ addons/test/cert-test.c | 22 ++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/addons/cert.py b/addons/cert.py index 04cc72c6b..933698172 100755 --- a/addons/cert.py +++ b/addons/cert.py @@ -154,6 +154,15 @@ def exp42(data): token, 'style', "Comparison of struct padding data " + "(fix either by packing the struct using '#pragma pack' or by rewriting the comparison)", 'EXP42-C') +# EXP15-C +# Do not place a semicolon on the same line as an if, for or while statement +def exp15(data): + for scope in data.scopes: + if scope.type in ('If', 'For', 'While'): + token = scope.bodyStart.next + if token.str==';' and token.linenr==scope.bodyStart.linenr: + reportError(token, 'style', 'Do not place a semicolon on the same line as an IF, FOR or WHILE', 'EXP15-C') + # EXP46-C # Do not use a bitwise operator with a Boolean-like operand @@ -293,6 +302,7 @@ for arg in sys.argv[1:]: exp05(cfg) exp42(cfg) exp46(cfg) + exp15(cfg) int31(cfg, data.platform) str03(cfg) str05(cfg) diff --git a/addons/test/cert-test.c b/addons/test/cert-test.c index 3763b0736..90a324b49 100644 --- a/addons/test/cert-test.c +++ b/addons/test/cert-test.c @@ -61,6 +61,18 @@ void msc30() int a = rand; } +void exp15() +{ + int x=5, y=7; + + if(x==y); //cert-EXP15-C + { + printf("not working\n"); + } + if(x) + ; +} + void str03() { char *string_data=(char*)malloc(16); @@ -72,8 +84,14 @@ void str03() void str05() { - char *str1 = "abc"; //cert-STR05-C - wchar_t *str2 = L"hello"; //cert-STR05-C + int x=5, y=7; + + if(x==y); //cert-EXP15-C + { + printf("not working\n"); + } + if(x) + ; } void str07(char *buf, const char *newBuf)