From b3210f6c2cccc6477a232d4107af5112167f9f79 Mon Sep 17 00:00:00 2001 From: Mike Kinney Date: Mon, 24 Jan 2022 07:00:14 +0000 Subject: [PATCH] fix some cppcheck warnings --- .gitignore | 2 ++ bin/check-all.sh | 18 ++++++++++++++++++ src/Power.cpp | 5 ++++- src/airtime.cpp | 8 +++++--- src/airtime.h | 6 +++--- src/esp32/SimpleAllocator.h | 4 ++-- src/gps/GeoCoord.cpp | 2 +- src/gps/GeoCoord.h | 10 +++++----- src/graphics/Screen.cpp | 12 ++++++------ src/input/RotaryEncoderInterruptBase.h | 12 ++++++------ src/mesh/CryptoEngine.h | 4 ++-- src/mesh/PhoneAPI.h | 4 ++-- src/mesh/RF95Interface.h | 4 ++-- src/mesh/RadioLibInterface.h | 4 ++-- src/mesh/StreamAPI.h | 6 +++--- src/plugins/EnvironmentalMeasurementPlugin.h | 8 ++++---- src/plugins/esp32/RangeTestPlugin.h | 2 +- src/plugins/esp32/SerialPlugin.h | 2 +- src/plugins/esp32/StoreForwardPlugin.cpp | 2 +- suppressions.txt | 12 ++++++++++++ 20 files changed, 82 insertions(+), 45 deletions(-) create mode 100755 bin/check-all.sh create mode 100644 suppressions.txt diff --git a/.gitignore b/.gitignore index 3d181a39a..d63accc92 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,5 @@ __pycache__ *.swp *.swo *~ + +venv/ diff --git a/bin/check-all.sh b/bin/check-all.sh new file mode 100755 index 000000000..f7fd445d0 --- /dev/null +++ b/bin/check-all.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +# Note: This is a prototype for how we could add static code analysis to the CI. + +set -e + +VERSION=`bin/buildinfo.py long` + +# The shell vars the build tool expects to find +export APP_VERSION=$VERSION + +# only check high and medium in our source +# TODO: only doing tbeam (to start; add all/more later) +pio check --flags "-DAPP_VERSION=${APP_VERSION} --suppressions-list=suppressions.txt" -e tbeam --skip-packages --severity=medium --severity=high --pattern="src/" +return_code=$? + +# TODO: not sure why return_code is 0 +echo "return_code:${return_code}" diff --git a/src/Power.cpp b/src/Power.cpp index 4d2eca29d..b46153c6e 100644 --- a/src/Power.cpp +++ b/src/Power.cpp @@ -149,7 +149,10 @@ class AnalogBatteryLevel : public HasBatteryLevel AnalogBatteryLevel analogLevel; -Power::Power() : OSThread("Power") {} +Power::Power() : OSThread("Power") { + statusHandler = {}; + low_voltage_counter = 0; +} bool Power::analogInit() { diff --git a/src/airtime.cpp b/src/airtime.cpp index d8f0229c9..d497e4dc7 100644 --- a/src/airtime.cpp +++ b/src/airtime.cpp @@ -2,7 +2,7 @@ #include "NodeDB.h" #include "configuration.h" -AirTime *airTime; +AirTime *airTime = NULL; // Don't read out of this directly. Use the helper functions. @@ -117,7 +117,9 @@ float AirTime::utilizationTXPercent() return (float(sum) / float(MS_IN_HOUR)) * 100; } -AirTime::AirTime() : concurrency::OSThread("AirTime") {} +AirTime::AirTime() : concurrency::OSThread("AirTime") { + airtimes = {}; +} int32_t AirTime::runOnce() { @@ -183,4 +185,4 @@ int32_t AirTime::runOnce() DEBUG_MSG("\n"); */ return (1000 * 1); -} \ No newline at end of file +} diff --git a/src/airtime.h b/src/airtime.h index 8845ec34d..6491e3648 100644 --- a/src/airtime.h +++ b/src/airtime.h @@ -49,8 +49,8 @@ class AirTime : private concurrency::OSThread float utilizationTXPercent(); float UtilizationPercentTX(); - uint32_t channelUtilization[CHANNEL_UTILIZATION_PERIODS]; - uint32_t utilizationTX[MINUTES_IN_HOUR]; + uint32_t channelUtilization[CHANNEL_UTILIZATION_PERIODS] = {0}; + uint32_t utilizationTX[MINUTES_IN_HOUR] = {0}; void airtimeRotatePeriod(); uint8_t getPeriodsToLog(); @@ -79,4 +79,4 @@ class AirTime : private concurrency::OSThread virtual int32_t runOnce() override; }; -extern AirTime *airTime; \ No newline at end of file +extern AirTime *airTime; diff --git a/src/esp32/SimpleAllocator.h b/src/esp32/SimpleAllocator.h index acaab3409..a529c1a50 100644 --- a/src/esp32/SimpleAllocator.h +++ b/src/esp32/SimpleAllocator.h @@ -14,9 +14,9 @@ */ class SimpleAllocator { - uint8_t bytes[POOL_SIZE]; + uint8_t bytes[POOL_SIZE] = {}; - uint32_t nextFree; + uint32_t nextFree = 0; public: SimpleAllocator(); diff --git a/src/gps/GeoCoord.cpp b/src/gps/GeoCoord.cpp index d09b1addb..98f9c842b 100644 --- a/src/gps/GeoCoord.cpp +++ b/src/gps/GeoCoord.cpp @@ -447,4 +447,4 @@ std::shared_ptr GeoCoord::pointAtDistance(double bearing, double range return std::make_shared(double(lat), double(lon), this->getAltitude()); -} \ No newline at end of file +} diff --git a/src/gps/GeoCoord.h b/src/gps/GeoCoord.h index b3d218700..a2ac56d74 100644 --- a/src/gps/GeoCoord.h +++ b/src/gps/GeoCoord.h @@ -86,11 +86,11 @@ class GeoCoord { int32_t _longitude = 0; int32_t _altitude = 0; - DMS _dms; - UTM _utm; - MGRS _mgrs; - OSGR _osgr; - OLC _olc; + DMS _dms = {}; + UTM _utm = {}; + MGRS _mgrs = {}; + OSGR _osgr = {}; + OLC _olc = {}; bool _dirty = true; diff --git a/src/graphics/Screen.cpp b/src/graphics/Screen.cpp index d18c4ebc8..768c3875e 100644 --- a/src/graphics/Screen.cpp +++ b/src/graphics/Screen.cpp @@ -449,10 +449,10 @@ static void drawGPScoordinates(OLEDDisplay *display, int16_t x, int16_t y, const if (gpsFormat == GpsCoordinateFormat_GpsFormatDec) { // Decimal Degrees sprintf(coordinateLine, "%f %f", geoCoord.getLatitude() * 1e-7, geoCoord.getLongitude() * 1e-7); } else if (gpsFormat == GpsCoordinateFormat_GpsFormatUTM) { // Universal Transverse Mercator - sprintf(coordinateLine, "%2i%1c %06i %07i", geoCoord.getUTMZone(), geoCoord.getUTMBand(), + sprintf(coordinateLine, "%2i%1c %06u %07u", geoCoord.getUTMZone(), geoCoord.getUTMBand(), geoCoord.getUTMEasting(), geoCoord.getUTMNorthing()); } else if (gpsFormat == GpsCoordinateFormat_GpsFormatMGRS) { // Military Grid Reference System - sprintf(coordinateLine, "%2i%1c %1c%1c %05i %05i", geoCoord.getMGRSZone(), geoCoord.getMGRSBand(), + sprintf(coordinateLine, "%2i%1c %1c%1c %05u %05u", geoCoord.getMGRSZone(), geoCoord.getMGRSBand(), geoCoord.getMGRSEast100k(), geoCoord.getMGRSNorth100k(), geoCoord.getMGRSEasting(), geoCoord.getMGRSNorthing()); } else if (gpsFormat == GpsCoordinateFormat_GpsFormatOLC) { // Open Location Code @@ -461,7 +461,7 @@ static void drawGPScoordinates(OLEDDisplay *display, int16_t x, int16_t y, const if (geoCoord.getOSGRE100k() == 'I' || geoCoord.getOSGRN100k() == 'I') // OSGR is only valid around the UK region sprintf(coordinateLine, "%s", "Out of Boundary"); else - sprintf(coordinateLine, "%1c%1c %05i %05i", geoCoord.getOSGRE100k(), geoCoord.getOSGRN100k(), + sprintf(coordinateLine, "%1c%1c %05u %05u", geoCoord.getOSGRE100k(), geoCoord.getOSGRN100k(), geoCoord.getOSGREasting(), geoCoord.getOSGRNorthing()); } @@ -479,9 +479,9 @@ static void drawGPScoordinates(OLEDDisplay *display, int16_t x, int16_t y, const } else { char latLine[22]; char lonLine[22]; - sprintf(latLine, "%2i° %2i' %2.4f\" %1c", geoCoord.getDMSLatDeg(), geoCoord.getDMSLatMin(), geoCoord.getDMSLatSec(), + sprintf(latLine, "%2i° %2i' %2u\" %1c", geoCoord.getDMSLatDeg(), geoCoord.getDMSLatMin(), geoCoord.getDMSLatSec(), geoCoord.getDMSLatCP()); - sprintf(lonLine, "%3i° %2i' %2.4f\" %1c", geoCoord.getDMSLonDeg(), geoCoord.getDMSLonMin(), geoCoord.getDMSLonSec(), + sprintf(lonLine, "%3i° %2i' %2u\" %1c", geoCoord.getDMSLonDeg(), geoCoord.getDMSLonMin(), geoCoord.getDMSLonSec(), geoCoord.getDMSLonCP()); display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(latLine))) / 2, y - FONT_HEIGHT_SMALL * 1, latLine); display->drawString(x + (SCREEN_WIDTH - (display->getStringWidth(lonLine))) / 2, y, lonLine); @@ -1050,7 +1050,7 @@ void Screen::handleStartBluetoothPinScreen(uint32_t pin) static FrameCallback btFrames[] = {drawFrameBluetooth}; - snprintf(btPIN, sizeof(btPIN), "%06lu", pin); + snprintf(btPIN, sizeof(btPIN), "%06u", pin); ui.disableAllIndicators(); ui.setFrames(btFrames, 1); diff --git a/src/input/RotaryEncoderInterruptBase.h b/src/input/RotaryEncoderInterruptBase.h index ae4af5262..443ba15fc 100644 --- a/src/input/RotaryEncoderInterruptBase.h +++ b/src/input/RotaryEncoderInterruptBase.h @@ -48,10 +48,10 @@ class RotaryEncoderInterruptBase : volatile RotaryEncoderInterruptBaseActionType action = ROTARY_ACTION_NONE; private: - uint8_t _pinA; - uint8_t _pinB; - char _eventCw; - char _eventCcw; - char _eventPressed; + uint8_t _pinA = 0; + uint8_t _pinB = 0; + char _eventCw = InputEventChar_KEY_NONE; + char _eventCcw = InputEventChar_KEY_NONE; + char _eventPressed = InputEventChar_KEY_NONE; const char *_originName; -}; \ No newline at end of file +}; diff --git a/src/mesh/CryptoEngine.h b/src/mesh/CryptoEngine.h index 9853f564a..179f4d139 100644 --- a/src/mesh/CryptoEngine.h +++ b/src/mesh/CryptoEngine.h @@ -20,9 +20,9 @@ class CryptoEngine { protected: /** Our per packet nonce */ - uint8_t nonce[16]; + uint8_t nonce[16] = {0}; - CryptoKey key; + CryptoKey key = {}; public: virtual ~CryptoEngine() {} diff --git a/src/mesh/PhoneAPI.h b/src/mesh/PhoneAPI.h index e818bba56..50ab3592c 100644 --- a/src/mesh/PhoneAPI.h +++ b/src/mesh/PhoneAPI.h @@ -45,7 +45,7 @@ class PhoneAPI /// We temporarily keep the nodeInfo here between the call to available and getFromRadio const NodeInfo *nodeInfoForPhone = NULL; - ToRadio toRadioScratch; // this is a static scratch object, any data must be copied elsewhere before returning + ToRadio toRadioScratch = {0}; // this is a static scratch object, any data must be copied elsewhere before returning /// Use to ensure that clients don't get confused about old messages from the radio uint32_t config_nonce = 0; @@ -86,7 +86,7 @@ class PhoneAPI protected: /// Our fromradio packet while it is being assembled - FromRadio fromRadioScratch; + FromRadio fromRadioScratch = {}; /** the last msec we heard from the client on the other side of this link */ uint32_t lastContactMsec = 0; diff --git a/src/mesh/RF95Interface.h b/src/mesh/RF95Interface.h index 59930adbd..d072f28e3 100644 --- a/src/mesh/RF95Interface.h +++ b/src/mesh/RF95Interface.h @@ -9,7 +9,7 @@ */ class RF95Interface : public RadioLibInterface { - RadioLibRF95 *lora; // Either a RFM95 or RFM96 depending on what was stuffed on this board + RadioLibRF95 *lora = NULL; // Either a RFM95 or RFM96 depending on what was stuffed on this board public: RF95Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, SPIClass &spi); @@ -63,4 +63,4 @@ class RF95Interface : public RadioLibInterface private: /** Some boards require GPIO control of tx vs rx paths */ void setTransmitEnable(bool txon); -}; \ No newline at end of file +}; diff --git a/src/mesh/RadioLibInterface.h b/src/mesh/RadioLibInterface.h index 8fe2e2b81..b38d6d553 100644 --- a/src/mesh/RadioLibInterface.h +++ b/src/mesh/RadioLibInterface.h @@ -95,7 +95,7 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified PhysicalLayer *iface; /// are _trying_ to receive a packet currently (note - we might just be waiting for one) - bool isReceiving; + bool isReceiving = false; public: /** Our ISR code currently needs this to find our active instance @@ -183,4 +183,4 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified virtual void addReceiveMetadata(MeshPacket *mp) = 0; virtual void setStandby() = 0; -}; \ No newline at end of file +}; diff --git a/src/mesh/StreamAPI.h b/src/mesh/StreamAPI.h index 58af95b34..ca6cbdf16 100644 --- a/src/mesh/StreamAPI.h +++ b/src/mesh/StreamAPI.h @@ -35,7 +35,7 @@ class StreamAPI : public PhoneAPI, protected concurrency::OSThread */ Stream *stream; - uint8_t rxBuf[MAX_STREAM_BUF_SIZE]; + uint8_t rxBuf[MAX_STREAM_BUF_SIZE] = {0}; size_t rxPtr = 0; /// time of last rx, used, to slow down our polling if we haven't heard from anyone @@ -81,5 +81,5 @@ class StreamAPI : public PhoneAPI, protected concurrency::OSThread bool canWrite = true; /// Subclasses can use this scratch buffer if they wish - uint8_t txBuf[MAX_STREAM_BUF_SIZE]; -}; \ No newline at end of file + uint8_t txBuf[MAX_STREAM_BUF_SIZE] = {0}; +}; diff --git a/src/plugins/EnvironmentalMeasurementPlugin.h b/src/plugins/EnvironmentalMeasurementPlugin.h index 9eed72c8f..14da3b5ba 100644 --- a/src/plugins/EnvironmentalMeasurementPlugin.h +++ b/src/plugins/EnvironmentalMeasurementPlugin.h @@ -35,10 +35,10 @@ class EnvironmentalMeasurementPlugin : private concurrency::OSThread, public Pro private: float CelsiusToFarenheit(float c); bool firstTime = 1; - DHT *dht; - OneWire *oneWire; - DS18B20 *ds18b20; + DHT *dht = NULL; + OneWire *oneWire = NULL; + DS18B20 *ds18b20 = NULL; Adafruit_BME280 bme; const MeshPacket *lastMeasurementPacket; uint32_t sensor_read_error_count = 0; -}; \ No newline at end of file +}; diff --git a/src/plugins/esp32/RangeTestPlugin.h b/src/plugins/esp32/RangeTestPlugin.h index d20a253a0..6b0b93289 100644 --- a/src/plugins/esp32/RangeTestPlugin.h +++ b/src/plugins/esp32/RangeTestPlugin.h @@ -25,7 +25,7 @@ extern RangeTestPlugin *rangeTestPlugin; */ class RangeTestPluginRadio : public SinglePortPlugin { - uint32_t lastRxID; + uint32_t lastRxID = 0; public: RangeTestPluginRadio() : SinglePortPlugin("RangeTestPluginRadio", PortNum_TEXT_MESSAGE_APP) {} diff --git a/src/plugins/esp32/SerialPlugin.h b/src/plugins/esp32/SerialPlugin.h index 27cd7e91a..ab28c2841 100644 --- a/src/plugins/esp32/SerialPlugin.h +++ b/src/plugins/esp32/SerialPlugin.h @@ -25,7 +25,7 @@ extern SerialPlugin *serialPlugin; */ class SerialPluginRadio : public SinglePortPlugin { - uint32_t lastRxID; + uint32_t lastRxID = 0; public: /* diff --git a/src/plugins/esp32/StoreForwardPlugin.cpp b/src/plugins/esp32/StoreForwardPlugin.cpp index f613fc7c0..f970f86dc 100644 --- a/src/plugins/esp32/StoreForwardPlugin.cpp +++ b/src/plugins/esp32/StoreForwardPlugin.cpp @@ -121,7 +121,7 @@ void StoreForwardPlugin::historySend(uint32_t msAgo, uint32_t to) uint32_t queueSize = storeForwardPlugin->historyQueueCreate(msAgo, to); if (queueSize) { - snprintf(this->routerMessage, 80, "** S&F - Sending %d message(s)", queueSize); + snprintf(this->routerMessage, 80, "** S&F - Sending %u message(s)", queueSize); storeForwardPlugin->sendMessage(to, this->routerMessage); this->busy = true; // runOnce() will pickup the next steps once busy = true. diff --git a/suppressions.txt b/suppressions.txt new file mode 100644 index 000000000..074c9bfdf --- /dev/null +++ b/suppressions.txt @@ -0,0 +1,12 @@ +// cppcheck suppressions +assertWithSideEffect + +// TODO: need to come back to these +duplInheritedMember + +// most likely due to a cppcheck configuration issue (like missing an include) +syntaxError + +// ignore stuff that is not ours +*:.pio/* +*:*/libdeps/*