Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix a lot of nuisances reported by cppcheck #4872

Merged
merged 2 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SafeFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
class SafeFile : public Print
{
public:
SafeFile(char const *filepath, bool fullAtomic = false);
explicit SafeFile(char const *filepath, bool fullAtomic = false);

virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buffer, size_t size);
Expand Down
2 changes: 1 addition & 1 deletion src/gps/GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ int GPS::getACK(uint8_t *buffer, uint16_t size, uint8_t requestedClass, uint8_t

bool GPS::setup()
{
int msglen = 0;

if (!didSerialInit) {
int msglen = 0;
if (tx_gpio && gnssModel == GNSS_MODEL_UNKNOWN) {

// if GPS_BAUDRATE is specified in variant (i.e. not 9600), skip to the specified rate.
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2201,7 +2201,7 @@ void Screen::setFrames(FrameFocus focus)

case FOCUS_PRESERVE:
// If we can identify which type of frame "originalPosition" was, can move directly to it in the new frameset
FramesetInfo &oldFsi = this->framesetInfo;
const FramesetInfo &oldFsi = this->framesetInfo;
if (originalPosition == oldFsi.positions.log)
ui->switchToFrame(fsi.positions.log);
else if (originalPosition == oldFsi.positions.settings)
Expand Down
4 changes: 2 additions & 2 deletions src/mesh/CryptoEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ bool CryptoEngine::setDHKey(uint32_t nodeNum)
void CryptoEngine::hash(uint8_t *bytes, size_t numBytes)
{
SHA256 hash;
size_t posn, len;
size_t posn;
uint8_t size = numBytes;
uint8_t inc = 16;
hash.reset();
for (posn = 0; posn < size; posn += inc) {
len = size - posn;
size_t len = size - posn;
if (len > inc)
len = inc;
hash.update(bytes + posn, len);
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/ProtobufModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ template <class T> class ProtobufModule : protected SinglePortModule
T *decoded = NULL;
if (mp.which_payload_variant == meshtastic_MeshPacket_decoded_tag && mp.decoded.portnum == ourPortNum) {
memset(&scratch, 0, sizeof(scratch));
auto &p = mp.decoded;
const meshtastic_Data &p = mp.decoded;
if (pb_decode_from_bytes(p.payload.bytes, p.payload.size, fields, &scratch)) {
decoded = &scratch;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/mesh/aes-ccm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ static void aes_ccm_encr(size_t L, const uint8_t *in, size_t len, uint8_t *out,
*out++ ^= *in++;
}
}
static void aes_ccm_encr_auth(size_t M, uint8_t *x, uint8_t *a, uint8_t *auth)
static void aes_ccm_encr_auth(size_t M, const uint8_t *x, uint8_t *a, uint8_t *auth)
{
size_t i;
uint8_t tmp[AES_BLOCK_SIZE];
Expand Down
4 changes: 2 additions & 2 deletions src/mesh/compression/unishox2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ int32_t readUTF8(const char *in, int len, int l, int *utf8len)
/// This is also used for Unicode strings \n
/// This is a crude implementation that is not optimized. Assuming only short strings \n
/// are encoded, this is not much of an issue.
int matchOccurance(const char *in, int len, int l, char *out, int olen, int *ol, uint8_t *state, const uint8_t usx_hcodes[],
int matchOccurance(const char *in, int len, int l, char *out, int olen, int *ol, const uint8_t *state, const uint8_t usx_hcodes[],
const uint8_t usx_hcode_lens[])
{
int j, k;
Expand Down Expand Up @@ -383,7 +383,7 @@ int matchOccurance(const char *in, int len, int l, char *out, int olen, int *ol,
/// This is also used for Unicode strings \n
/// This is a crude implementation that is not optimized. Assuming only short strings \n
/// are encoded, this is not much of an issue.
int matchLine(const char *in, int len, int l, char *out, int olen, int *ol, struct us_lnk_lst *prev_lines, uint8_t *state,
int matchLine(const char *in, int len, int l, char *out, int olen, int *ol, struct us_lnk_lst *prev_lines, const uint8_t *state,
const uint8_t usx_hcodes[], const uint8_t usx_hcode_lens[])
{
int last_ol = *ol;
Expand Down
3 changes: 1 addition & 2 deletions src/meshUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ char *strnstr(const char *s, const char *find, size_t slen)

void printBytes(const char *label, const uint8_t *p, size_t numbytes)
{
char *messageBuffer;
int labelSize = strlen(label);
if (labelSize < 100 && numbytes < 64) {
messageBuffer = new char[labelSize + (numbytes * 3) + 2];
char *messageBuffer = new char[labelSize + (numbytes * 3) + 2];
strncpy(messageBuffer, label, labelSize);
for (size_t i = 0; i < numbytes; i++)
snprintf(messageBuffer + labelSize + i * 3, 4, " %02x", p[i]);
Expand Down
4 changes: 2 additions & 2 deletions src/modules/AdminModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,7 @@ bool AdminModule::checkPassKey(meshtastic_AdminMessage *res)
memcmp(res->session_passkey.bytes, session_passkey, 8) == 0);
}

bool AdminModule::messageIsResponse(meshtastic_AdminMessage *r)
bool AdminModule::messageIsResponse(const meshtastic_AdminMessage *r)
{
if (r->which_payload_variant == meshtastic_AdminMessage_get_channel_response_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_owner_response_tag ||
Expand All @@ -1043,7 +1043,7 @@ bool AdminModule::messageIsResponse(meshtastic_AdminMessage *r)
return false;
}

bool AdminModule::messageIsRequest(meshtastic_AdminMessage *r)
bool AdminModule::messageIsRequest(const meshtastic_AdminMessage *r)
{
if (r->which_payload_variant == meshtastic_AdminMessage_get_channel_request_tag ||
r->which_payload_variant == meshtastic_AdminMessage_get_owner_request_tag ||
Expand Down
4 changes: 2 additions & 2 deletions src/modules/AdminModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class AdminModule : public ProtobufModule<meshtastic_AdminMessage>, public Obser
void setPassKey(meshtastic_AdminMessage *res);
bool checkPassKey(meshtastic_AdminMessage *res);

bool messageIsResponse(meshtastic_AdminMessage *r);
bool messageIsRequest(meshtastic_AdminMessage *r);
bool messageIsResponse(const meshtastic_AdminMessage *r);
bool messageIsRequest(const meshtastic_AdminMessage *r);
};

extern AdminModule *adminModule;
Expand Down
8 changes: 4 additions & 4 deletions src/modules/CannedMessageModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,16 @@ int CannedMessageModule::splitConfiguredMessages()
int messageIndex = 0;
int i = 0;

String messages = cannedMessageModuleConfig.messages;
String canned_messages = cannedMessageModuleConfig.messages;

#if defined(T_WATCH_S3) || defined(RAK14014)
String separator = messages.length() ? "|" : "";
String separator = canned_messages.length() ? "|" : "";

messages = "[---- Free Text ----]" + separator + messages;
canned_messages = "[---- Free Text ----]" + separator + canned_messages;
#endif

// collect all the message parts
strncpy(this->messageStore, messages.c_str(), sizeof(this->messageStore));
strncpy(this->messageStore, canned_messages.c_str(), sizeof(this->messageStore));

// The first message points to the beginning of the store.
this->messages[messageIndex++] = this->messageStore;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/EnvironmentTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void EnvironmentTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiSt
uint32_t agoSecs = GetTimeSinceMeshPacket(lastMeasurementPacket);
const char *lastSender = getSenderShortName(*lastMeasurementPacket);

auto &p = lastMeasurementPacket->decoded;
const meshtastic_Data &p = lastMeasurementPacket->decoded;
if (!pb_decode_from_bytes(p.payload.bytes, p.payload.size, &meshtastic_Telemetry_msg, &lastMeasurement)) {
display->drawString(x, y, "Measurement Error");
LOG_ERROR("Unable to decode last packet");
Expand Down
2 changes: 1 addition & 1 deletion src/modules/Telemetry/PowerTelemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void PowerTelemetryModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *s
uint32_t agoSecs = GetTimeyWimeySinceMeshPacket(lastMeasurementPacket);
const char *lastSender = getSenderShortName(*lastMeasurementPacket);

auto &p = lastMeasurementPacket->decoded;
const meshtastic_Data &p = lastMeasurementPacket->decoded;
if (!pb_decode_from_bytes(p.payload.bytes, p.payload.size, &meshtastic_Telemetry_msg, &lastMeasurement)) {
display->setFont(FONT_SMALL);
display->drawString(x, y += _fontHeight(FONT_MEDIUM), "Measurement Error");
Expand Down
2 changes: 1 addition & 1 deletion src/modules/TraceRouteModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool TraceRouteModule::handleReceivedProtobuf(const meshtastic_MeshPacket &mp, m

void TraceRouteModule::alterReceivedProtobuf(meshtastic_MeshPacket &p, meshtastic_RouteDiscovery *r)
{
auto &incoming = p.decoded;
const meshtastic_Data &incoming = p.decoded;

// Insert unknown hops if necessary
insertUnknownHops(p, r, !incoming.request_id);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/WaypointModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ void WaypointModule::drawFrame(OLEDDisplay *display, OLEDDisplayUiState *state,
display->fillRect(0 + x, 0 + y, x + display->getWidth(), y + FONT_HEIGHT_SMALL);

// Decode the waypoint
meshtastic_MeshPacket &mp = devicestate.rx_waypoint;
const meshtastic_MeshPacket &mp = devicestate.rx_waypoint;
meshtastic_Waypoint wp;
memset(&wp, 0, sizeof(wp));
if (!pb_decode_from_bytes(mp.decoded.payload.bytes, mp.decoded.payload.size, &meshtastic_Waypoint_msg, &wp)) {
Expand Down
4 changes: 2 additions & 2 deletions src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
// PKI messages get accepted even if we can't decrypt
if (router && p->which_payload_variant == meshtastic_MeshPacket_encrypted_tag &&
strcmp(e.channel_id, "PKI") == 0) {
meshtastic_NodeInfoLite *tx = nodeDB->getMeshNode(getFrom(p));
meshtastic_NodeInfoLite *rx = nodeDB->getMeshNode(p->to);
const meshtastic_NodeInfoLite *tx = nodeDB->getMeshNode(getFrom(p));
const meshtastic_NodeInfoLite *rx = nodeDB->getMeshNode(p->to);
// Only accept PKI messages to us, or if we have both the sender and receiver in our nodeDB, as then it's
// likely they discovered each other via a channel we have downlink enabled for
if (p->to == nodeDB->getNodeNum() || (tx && tx->has_user && rx && rx->has_user))
Expand Down
3 changes: 1 addition & 2 deletions src/platform/nrf52/BLEDfuScure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ extern "C" void bootloader_util_app_start(uint32_t start_addr);

static uint16_t crc16(const uint8_t *data_p, uint8_t length)
{
uint8_t x;
uint16_t crc = 0xFFFF;

while (length--) {
x = crc >> 8 ^ *data_p++;
uint8_t x = crc >> 8 ^ *data_p++;
x ^= x >> 4;
crc = (crc << 8) ^ ((uint16_t)(x << 12)) ^ ((uint16_t)(x << 5)) ^ ((uint16_t)x);
}
Expand Down
16 changes: 8 additions & 8 deletions src/serialization/JSONValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
#define FREE_ARRAY(x) \
{ \
JSONArray::iterator iter; \
for (iter = x.begin(); iter != x.end(); iter++) { \
for (iter = x.begin(); iter != x.end(); ++iter) { \
delete *iter; \
} \
}
#define FREE_OBJECT(x) \
{ \
JSONObject::iterator iter; \
for (iter = x.begin(); iter != x.end(); iter++) { \
for (iter = x.begin(); iter != x.end(); ++iter) { \
delete (*iter).second; \
} \
}
Expand Down Expand Up @@ -430,7 +430,7 @@ JSONValue::JSONValue(const JSONValue &m_source)
JSONArray source_array = *m_source.array_value;
JSONArray::iterator iter;
array_value = new JSONArray();
for (iter = source_array.begin(); iter != source_array.end(); iter++)
for (iter = source_array.begin(); iter != source_array.end(); ++iter)
array_value->push_back(new JSONValue(**iter));
break;
}
Expand All @@ -439,7 +439,7 @@ JSONValue::JSONValue(const JSONValue &m_source)
JSONObject source_object = *m_source.object_value;
object_value = new JSONObject();
JSONObject::iterator iter;
for (iter = source_object.begin(); iter != source_object.end(); iter++) {
for (iter = source_object.begin(); iter != source_object.end(); ++iter) {
std::string name = (*iter).first;
(*object_value)[name] = new JSONValue(*((*iter).second));
}
Expand All @@ -462,12 +462,12 @@ JSONValue::~JSONValue()
{
if (type == JSONType_Array) {
JSONArray::iterator iter;
for (iter = array_value->begin(); iter != array_value->end(); iter++)
for (iter = array_value->begin(); iter != array_value->end(); ++iter)
delete *iter;
delete array_value;
} else if (type == JSONType_Object) {
JSONObject::iterator iter;
for (iter = object_value->begin(); iter != object_value->end(); iter++) {
for (iter = object_value->begin(); iter != object_value->end(); ++iter) {
delete (*iter).second;
}
delete object_value;
Expand Down Expand Up @@ -722,7 +722,7 @@ std::vector<std::string> JSONValue::ObjectKeys() const
while (iter != object_value->end()) {
keys.push_back(iter->first);

iter++;
++iter;
}
}

Expand Down Expand Up @@ -865,7 +865,7 @@ std::string JSONValue::StringifyString(const std::string &str)
str_out += chr;
}

iter++;
++iter;
}

str_out += "\"";
Expand Down
Loading