From 5e3da9e82af41d111be32cd782d32fc3c612e69a Mon Sep 17 00:00:00 2001 From: fuzzelhjb Date: Mon, 17 Jun 2019 20:42:23 +0200 Subject: [PATCH] add CERT STR05-C check (#1899) * add CERT STR05-C check * update check --- addons/cert.py | 16 ++++++++++++++++ addons/test/cert-test.c | 9 +++++++++ 2 files changed, 25 insertions(+) diff --git a/addons/cert.py b/addons/cert.py index 8909cf04f..6f4a22d7c 100755 --- a/addons/cert.py +++ b/addons/cert.py @@ -234,6 +234,21 @@ def msc30(data): if simpleMatch(token, "rand ( )") and isStandardFunction(token): reportError(token, 'style', 'Do not use the rand() function for generating pseudorandom numbers', 'MSC30-c') + +# STR05-C +# Use pointers to const when referring to string literals +def str05(data): + for token in data.tokenlist: + if token.isString: + parent = token.astParent + if parent is None: + continue + parentOp1 = parent.astOperand1 + if parent.isAssignmentOp and parentOp1.valueType: + if (parentOp1.valueType.type in ('char', 'wchar_t')) and parentOp1.valueType.pointer and not parentOp1.valueType.constness: + reportError(parentOp1, 'style', 'Use pointers to const when referring to string literals', 'STR05-C') + + for arg in sys.argv[1:]: if arg == '-verify': VERIFY = True @@ -259,6 +274,7 @@ for arg in sys.argv[1:]: exp42(cfg) exp46(cfg) int31(cfg, data.platform) + str05(cfg) msc30(cfg) if VERIFY: diff --git a/addons/test/cert-test.c b/addons/test/cert-test.c index 4b405079f..d57b107eb 100644 --- a/addons/test/cert-test.c +++ b/addons/test/cert-test.c @@ -1,5 +1,7 @@ // To test: // ~/cppcheck/cppcheck --dump cert-test.c && python ../cert.py -verify cert-test.c.dump +#include +#include struct S { short a; @@ -61,3 +63,10 @@ void msc30() int rand = 5; int a = rand; } + +void str05() +{ + char *str1 = "abc"; //cert-STR05-C + wchar_t *str2 = L"hello"; //cert-STR05-C +} +