Skip to content

Commit 2d5404d

Browse files
fallbergtbowmo
authored andcommitted
CppCheck cleanup (#975)
* Add support for inline cppcheck suppressions * Clean out all known cppcheck issues * Terminate toll-gate on found cppcheck issues
1 parent e2ac85a commit 2d5404d

File tree

23 files changed

+86
-50
lines changed

23 files changed

+86
-50
lines changed

.ci/static_analysis.groovy

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def cppCheck(config) {
55
sh """#!/bin/bash +x
66
cd ${config.repository_root}
77
echo "Doing cppcheck for AVR..."
8-
find . -type f \\( -iname \\*.c -o -iname \\*.cpp -o -iname \\*.ino \\) | cppcheck -j 4 --file-list=- --enable=style,information --platform=.mystools/cppcheck/config/avr.xml --suppressions-list=.mystools/cppcheck/config/suppressions.cfg --includes-file=.mystools/cppcheck/config/includes.cfg --language=c++ --xml --xml-version=2 2> cppcheck-avr.xml
8+
find . -type f \\( -iname \\*.c -o -iname \\*.cpp -o -iname \\*.ino \\) | cppcheck -j 4 --force --file-list=- --enable=style,information --platform=.mystools/cppcheck/config/avr.xml --suppressions-list=.mystools/cppcheck/config/suppressions.cfg --includes-file=.mystools/cppcheck/config/includes.cfg --language=c++ --inline-suppr --xml --xml-version=2 2> cppcheck-avr.xml
99
cppcheck-htmlreport --file="cppcheck-avr.xml" --title="cppcheck-avr" --report-dir=cppcheck-avr_cppcheck_reports --source-dir=."""
1010

1111
publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: true,
@@ -29,9 +29,8 @@ def cppCheck(config) {
2929
"grep -q \"<td>0</td><td>total</td>\" cppcheck-avr_cppcheck_reports/index.html || exit_code=\$?\n"+
3030
"exit \$((exit_code == 0 ? 0 : 1))")
3131
if (ret == 1) {
32-
config.pr.setBuildStatus(config, 'SUCCESS', 'Toll gate (Code analysis - Cppcheck)', 'Issues found (but are not considered critical)', '${BUILD_URL}CppCheck_AVR/index.html')
33-
//currentBuild.result = 'UNSTABLE'
34-
//error 'Terminating due to Cppcheck error'
32+
config.pr.setBuildStatus(config, 'ERROR', 'Toll gate (Code analysis - Cppcheck)', 'Issues found', '${BUILD_URL}CppCheck_AVR/index.html')
33+
error 'Terminating due to Cppcheck error'
3534
} else {
3635
config.pr.setBuildStatus(config, 'SUCCESS', 'Toll gate (Code analysis - Cppcheck)', 'Pass', '')
3736
}

.clang_complete

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
-IC:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino
2-
-IC:\Program Files (x86)\Arduino\hardware\tools\avr\avr\include
31
-Idrivers\Linux
42
-Idrivers\ATSHA204
53
-Icore
6-
-DMY_SIGNING_ATSHA204
7-
-DMY_SIGNING_SOFT
8-
-DARDUINO_ARCH_AVR
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
toomanyconfigs
2-
missingInclude
3-
missingIncludeSystem
41
ConfigurationNotChecked
52
unmatchedSuppression
3+
// This suppression is because the problem is in an in-lined macro so in-line-suppressions does not appear to take effect
4+
unreadVariable:*/MyHwNRF5.cpp

.mystools/cppcheck/options.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ OPTIONS="--quiet \
99
--library=${LIBRARY:-avr} \
1010
--platform="${TOOLCONFIG}"/${PLATFORM:-avr.xml} \
1111
--includes-file="${TOOLCONFIG}"/includes.cfg \
12+
--inline-suppr \
1213
--suppressions-list="${TOOLCONFIG}"/suppressions.cfg"
1314

1415
echo $OPTIONS

core/MyGatewayTransportEthernet.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,11 @@ MyMessage _ethernetMsg;
4949
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
5050

5151
typedef struct {
52+
// Suppress the warning about unused members in this struct because it is used through a complex
53+
// set of preprocessor directives
54+
// cppcheck-suppress unusedStructMember
5255
char string[MY_GATEWAY_MAX_RECEIVE_LENGTH];
56+
// cppcheck-suppress unusedStructMember
5357
uint8_t idx;
5458
} inputBuffer;
5559

core/MyLeds.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void ledsProcess()
5858
}
5959
prevTime = hwMillis();
6060

61+
#if defined(MY_DEFAULT_RX_LED_PIN) || defined(MY_DEFAULT_TX_LED_PIN) || defined(MY_DEFAULT_ERR_LED_PIN)
6162
uint8_t state;
63+
#endif
6264

6365
// For an On/Off ratio of 4, the pattern repeated will be [on, on, on, off]
6466
// until the counter becomes 0.

core/MyOTAFirmwareUpdate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ bool firmwareOTAUpdateProcess(void)
128128
MY_SERIALDEVICE.print(prbuf);
129129
}
130130
OTA_DEBUG(PSTR("\n"));
131-
8
132131
}
133132
#endif
134133
_firmwareBlock--;

core/MyProtocolMySensors.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ bool protocolParse(MyMessage &message, char *inputString)
5959
break;
6060
case 5: // Variable value
6161
if (command == C_STREAM) {
62-
blen = 0;
6362
while (*str) {
6463
uint8_t val;
6564
val = protocolH2i(*str++) << 4;
@@ -125,8 +124,6 @@ bool protocolMQTTParse(MyMessage &message, char* topic, uint8_t* payload, unsign
125124
{
126125
char *str, *p;
127126
uint8_t i = 0;
128-
uint8_t bvalue[MAX_PAYLOAD];
129-
uint8_t blen = 0;
130127
uint8_t command = 0;
131128
if (topic != strstr(topic, MY_MQTT_SUBSCRIBE_TOPIC_PREFIX)) {
132129
// Prefix doesn't match incoming topic
@@ -175,9 +172,10 @@ bool protocolMQTTParse(MyMessage &message, char* topic, uint8_t* payload, unsign
175172

176173
// Add payload
177174
if (command == C_STREAM) {
178-
blen = 0;
179-
uint8_t val;
175+
uint8_t bvalue[MAX_PAYLOAD];
176+
uint8_t blen = 0;
180177
while (*payload) {
178+
uint8_t val;
181179
val = protocolH2i(*payload++) << 4;
182180
val += protocolH2i(*payload++);
183181
bvalue[blen] = val;

core/MySigning.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ void signerInit(void)
111111
#endif
112112
#if (defined (MY_ENCRYPTION_FEATURE) || defined (MY_SIGNING_FEATURE)) &&\
113113
!defined (MY_SIGNING_SIMPLE_PASSWD)
114+
// Suppress this warning since it is only fixed on Linux builds and this keeps the code more tidy
115+
// cppcheck-suppress knownConditionTrueFalse
114116
if (!signerInternalValidatePersonalization()) {
115117
SIGN_DEBUG(PSTR("!SGN:PER:TAMPERED\n"));
116118
#if defined(MY_SIGNING_FEATURE)

core/MySigningAtsha204Soft.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,8 @@ bool signerAtsha204SoftInit(void)
103103
_signing_node_serial_info[8] = getNodeId();
104104
}
105105
#else
106-
if (init_ok) {
107-
hwReadConfigBlock((void*)_signing_hmac_key, (void*)EEPROM_SIGNING_SOFT_HMAC_KEY_ADDRESS, 32);
108-
hwReadConfigBlock((void*)_signing_node_serial_info, (void*)EEPROM_SIGNING_SOFT_SERIAL_ADDRESS, 9);
109-
}
106+
hwReadConfigBlock((void*)_signing_hmac_key, (void*)EEPROM_SIGNING_SOFT_HMAC_KEY_ADDRESS, 32);
107+
hwReadConfigBlock((void*)_signing_node_serial_info, (void*)EEPROM_SIGNING_SOFT_SERIAL_ADDRESS, 9);
110108
#endif
111109
if (!memcmp(_signing_node_serial_info, reset_serial, 9)) {
112110
unique_id_t uniqueID;

core/MyTransport.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ void transportProcessMessage(void)
940940

941941
void transportInvokeSanityCheck(void)
942942
{
943+
// Suppress this because the function may return a variable value in some configurations
944+
// cppcheck-suppress knownConditionTrueFalse
943945
if (!transportSanityCheck()) {
944946
TRANSPORT_DEBUG(PSTR("!TSF:SAN:FAIL\n")); // sanity check fail
945947
transportSwitchSM(stFailure);

drivers/AES/AES.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,19 @@ byte AES::cbc_encrypt (byte * plain, byte * cipher, int n_block)
407407

408408
/******************************************************************************/
409409

410-
byte AES::decrypt (byte plain [N_BLOCK], byte cipher [N_BLOCK])
410+
byte AES::decrypt (byte cipher [N_BLOCK], byte plain [N_BLOCK])
411411
{
412412
if (round) {
413413
byte s1 [N_BLOCK] ;
414-
copy_and_key (s1, plain, (byte*) (key_sched + round * N_BLOCK)) ;
414+
copy_and_key (s1, cipher, (byte*) (key_sched + round * N_BLOCK)) ;
415415
inv_shift_sub_rows (s1) ;
416416

417417
for (byte r = round ; --r ; ) {
418418
byte s2 [N_BLOCK] ;
419419
copy_and_key (s2, s1, (byte*) (key_sched + r * N_BLOCK)) ;
420420
inv_mix_sub_columns (s1, s2) ;
421421
}
422-
copy_and_key (cipher, s1, (byte*) (key_sched)) ;
422+
copy_and_key (plain, s1, (byte*) (key_sched)) ;
423423
} else {
424424
return AES_FAILURE ;
425425
}

drivers/ATSHA204/sha256.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ const uint8_t sha256InitState[] PROGMEM = {
3030
0x19,0xcd,0xe0,0x5b // H7
3131
};
3232

33+
// Suppress warning about uninitialized variables because initializing them in an init function
34+
// allows the compiler to optimize away the variables in case the class is only instantiated but
35+
// never used.
36+
// cppcheck-suppress uninitMemberVar
3337
Sha256Class::Sha256Class()
3438
{
3539
/*
@@ -165,6 +169,10 @@ uint8_t* Sha256Class::result(void)
165169
#define HMAC_IPAD 0x36
166170
#define HMAC_OPAD 0x5c
167171

172+
// Suppress warning about uninitialized variables because initializing them in an init function
173+
// allows the compiler to optimize away the variables in case the class is only instantiated but
174+
// never used.
175+
// cppcheck-suppress uninitMemberVar
168176
HmacClass::HmacClass()
169177
{
170178
}

drivers/AVR/DigitalIO/PinIO.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ class PinIO
4141
{
4242
public:
4343
/** Create a PinIO object with no assigned pin. */
44+
// Suppress warning about uninitialized variables because initializing them in an init function
45+
// allows the compiler to optimize away the variables in case the class is only instantiated but
46+
// never used.
47+
// cppcheck-suppress uninitMemberVar
4448
PinIO() : bit_(0), mask_(0XFF) {}
4549
/** Constructor
4650
* @param[in] pin Pin assigned to this object.
@@ -186,4 +190,4 @@ class PinIO
186190
volatile uint8_t* portReg_;
187191
};
188192
#endif // PinIO_h
189-
/** @} */
193+
/** @} */

drivers/AVR/DigitalIO/examples/SoftDS1307Utility/SoftDS1307Utility.ino

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ void dumpAll(void)
179179
//------------------------------------------------------------------------------
180180
void fillNvRam(void)
181181
{
182-
uint8_t buf[8];
183182
PgmPrint("Enter HEX value for all NV RAM locations (00-FF): ");
184183
uint16_t v;
185184
if (!hexRead(&v)) {
@@ -363,4 +362,4 @@ void loop(void)
363362
} else {
364363
PgmPrintln("Invalid option");
365364
}
366-
}
365+
}

drivers/AltSoftSerial/AltSoftSerial.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ void AltSoftSerial::writeByte(uint8_t b)
131131

132132
ISR(COMPARE_A_INTERRUPT)
133133
{
134-
uint8_t state, byte, bit, head, tail;
134+
uint8_t state, byte, head, tail;
135135
uint16_t target;
136136

137137
state = tx_state;
138138
byte = tx_byte;
139139
target = GET_COMPARE_A();
140140
while (state < 9) {
141+
uint8_t bit;
141142
target += ticks_per_bit;
142143
bit = byte & 1;
143144
byte >>= 1;
@@ -195,9 +196,8 @@ void AltSoftSerial::flushOutput(void)
195196

196197
ISR(CAPTURE_INTERRUPT)
197198
{
198-
uint8_t state, bit, head;
199-
uint16_t capture, target;
200-
int16_t offset;
199+
uint8_t state, bit;
200+
uint16_t capture;
201201

202202
capture = GET_INPUT_CAPTURE();
203203
bit = rx_bit;
@@ -217,8 +217,10 @@ ISR(CAPTURE_INTERRUPT)
217217
rx_state = 1;
218218
}
219219
} else {
220+
uint16_t target;
220221
target = rx_target;
221222
while (1) {
223+
int16_t offset;
222224
offset = capture - target;
223225
if (offset < 0) {
224226
break;
@@ -227,6 +229,7 @@ ISR(CAPTURE_INTERRUPT)
227229
target += ticks_per_bit;
228230
state++;
229231
if (state >= 9) {
232+
uint8_t head;
230233
DISABLE_INT_COMPARE_B();
231234
head = rx_buffer_head + 1;
232235
if (head >= RX_BUFFER_SIZE) {

drivers/BCM/bcm2835.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ unsigned int bcm2835_version(void)
113113
*/
114114
uint32_t bcm2835_peri_read(volatile uint32_t* paddr)
115115
{
116-
uint32_t ret;
117116
if (debug) {
118117
printf("bcm2835_peri_read paddr %08X\n", (unsigned) paddr);
119118
return 0;
120119
} else {
120+
uint32_t ret;
121121
__sync_synchronize();
122122
ret = *paddr;
123123
__sync_synchronize();
@@ -438,7 +438,7 @@ void bcm2835_delayMicroseconds(uint64_t micros)
438438

439439
if (debug) {
440440
/* Cant access sytem timers in debug mode */
441-
printf("bcm2835_delayMicroseconds %lld\n", micros);
441+
printf("bcm2835_delayMicroseconds %" PRIu64 "\n", micros);
442442
return;
443443
}
444444

drivers/Linux/Stream.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
// private method to read stream with timeout
3131
int Stream::timedRead()
3232
{
33-
int c;
3433
_startMillis = millis();
3534
do {
35+
int c;
3636
c = read();
3737
if(c >= 0) {
3838
return c;
@@ -45,9 +45,9 @@ int Stream::timedRead()
4545
// private method to peek stream with timeout
4646
int Stream::timedPeek()
4747
{
48-
int c;
4948
_startMillis = millis();
5049
do {
50+
int c;
5151
c = peek();
5252
if(c >= 0) {
5353
return c;
@@ -61,8 +61,8 @@ int Stream::timedPeek()
6161
// discards non-numeric characters
6262
int Stream::peekNextDigit()
6363
{
64-
int c;
6564
while(1) {
65+
int c;
6666
c = timedPeek();
6767
if(c < 0) {
6868
return c; // timeout

0 commit comments

Comments
 (0)