Skip to content

Commit 2d0b267

Browse files
committed
fix(cpp): Fixes for cpp code scanning alerts
1 parent f6b1910 commit 2d0b267

File tree

40 files changed

+215
-71
lines changed

40 files changed

+215
-71
lines changed

cores/esp32/esp32-hal-time.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,24 @@
2222
#endif
2323

2424
static void setTimeZone(long offset, int daylight) {
25-
char cst[17] = {0};
26-
char cdt[17] = "DST";
27-
char tz[33] = {0};
25+
char cst[21] = {0};
26+
char cdt[21] = "DST";
27+
char tz[41] = {0};
2828

2929
if (offset % 3600) {
30-
sprintf(cst, "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
30+
snprintf(cst, sizeof(cst), "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
3131
} else {
32-
sprintf(cst, "UTC%ld", offset / 3600);
32+
snprintf(cst, sizeof(cst), "UTC%ld", offset / 3600);
3333
}
3434
if (daylight != 3600) {
3535
long tz_dst = offset - daylight;
3636
if (tz_dst % 3600) {
37-
sprintf(cdt, "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
37+
snprintf(cdt, sizeof(cdt), "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
3838
} else {
39-
sprintf(cdt, "DST%ld", tz_dst / 3600);
39+
snprintf(cdt, sizeof(cdt), "DST%ld", tz_dst / 3600);
4040
}
4141
}
42-
sprintf(tz, "%s%s", cst, cdt);
42+
snprintf(tz, sizeof(tz), "%s%s", cst, cdt);
4343
setenv("TZ", tz, 1);
4444
tzset();
4545
}

libraries/AsyncUDP/src/AsyncUDP.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,33 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDPPacket &packet) {
317317
pbuf_ref(_pb);
318318
}
319319

320+
AsyncUDPPacket& AsyncUDPPacket::operator=(const AsyncUDPPacket& packet) {
321+
if (this != &packet) {
322+
if (_pb) {
323+
// Free existing pbuf reference
324+
pbuf_free(_pb);
325+
}
326+
327+
// Copy all members
328+
_udp = packet._udp;
329+
_pb = packet._pb;
330+
_if = packet._if;
331+
_data = packet._data;
332+
_len = packet._len;
333+
_index = 0;
334+
335+
memcpy(&_remoteIp, &packet._remoteIp, sizeof(ip_addr_t));
336+
memcpy(&_localIp, &packet._localIp, sizeof(ip_addr_t));
337+
_localPort = packet._localPort;
338+
_remotePort = packet._remotePort;
339+
memcpy(_remoteMac, packet._remoteMac, 6);
340+
341+
// Increment reference count for the new pbuf
342+
pbuf_ref(_pb);
343+
}
344+
return *this;
345+
}
346+
320347
AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr, uint16_t rport, struct netif *ntif) {
321348
_udp = udp;
322349
_pb = pb;

libraries/AsyncUDP/src/AsyncUDP.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ class AsyncUDPPacket : public Stream {
100100

101101
size_t write(const uint8_t *data, size_t len);
102102
size_t write(uint8_t data);
103+
104+
// Copy assignment operator
105+
AsyncUDPPacket& operator=(const AsyncUDPPacket& packet);
103106
};
104107

105108
class AsyncUDP : public Print {

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <nvs.h>
2828
#include <esp_partition.h>
2929
#include <esp_log.h>
30+
#include <new>
3031

3132
EEPROMClass::EEPROMClass(void) : _handle(0), _data(0), _size(0), _dirty(false), _name("eeprom") {}
3233

@@ -59,31 +60,31 @@ bool EEPROMClass::begin(size_t size) {
5960
}
6061
if (size < key_size) { // truncate
6162
log_w("truncating EEPROM from %d to %d", key_size, size);
62-
uint8_t *key_data = (uint8_t *)malloc(key_size);
63+
uint8_t *key_data = new(std::nothrow) uint8_t[key_size];
6364
if (!key_data) {
6465
log_e("Not enough memory to truncate EEPROM!");
6566
return false;
6667
}
6768
nvs_get_blob(_handle, _name, key_data, &key_size);
6869
nvs_set_blob(_handle, _name, key_data, size);
6970
nvs_commit(_handle);
70-
free(key_data);
71+
delete[] key_data;
7172
} else if (size > key_size) { // expand or new
7273
size_t expand_size = size - key_size;
73-
uint8_t *expand_key = (uint8_t *)malloc(expand_size);
74+
uint8_t *expand_key = new(std::nothrow) uint8_t[expand_size];
7475
if (!expand_key) {
7576
log_e("Not enough memory to expand EEPROM!");
7677
return false;
7778
}
7879
// check for adequate free space
7980
if (nvs_set_blob(_handle, "expand", expand_key, expand_size)) {
8081
log_e("Not enough space to expand EEPROM from %d to %d", key_size, size);
81-
free(expand_key);
82+
delete[] expand_key;
8283
return false;
8384
}
84-
free(expand_key);
85+
delete[] expand_key;
8586
nvs_erase_key(_handle, "expand");
86-
uint8_t *key_data = (uint8_t *)malloc(size);
87+
uint8_t *key_data = new(std::nothrow) uint8_t[size];
8788
if (!key_data) {
8889
log_e("Not enough memory to expand EEPROM!");
8990
return false;
@@ -99,15 +100,15 @@ bool EEPROMClass::begin(size_t size) {
99100
}
100101
nvs_commit(_handle);
101102
nvs_set_blob(_handle, _name, key_data, size);
102-
free(key_data);
103+
delete[] key_data;
103104
nvs_commit(_handle);
104105
}
105106

106107
if (_data) {
107108
delete[] _data;
108109
}
109110

110-
_data = (uint8_t *)malloc(size);
111+
_data = new(std::nothrow) uint8_t[size];
111112
if (!_data) {
112113
log_e("Not enough memory for %d bytes in EEPROM", size);
113114
return false;
@@ -212,7 +213,7 @@ uint16_t EEPROMClass::convert(bool clear, const char *EEPROMname, const char *nv
212213
}
213214

214215
size_t size = mypart->size;
215-
uint8_t *data = (uint8_t *)malloc(size);
216+
uint8_t *data = new(std::nothrow) uint8_t[size];
216217
if (!data) {
217218
log_e("Not enough memory to convert EEPROM!");
218219
goto exit;
@@ -255,7 +256,7 @@ uint16_t EEPROMClass::convert(bool clear, const char *EEPROMname, const char *nv
255256
}
256257
}
257258
exit:
258-
free(data);
259+
delete[] data;
259260
return result;
260261
}
261262

@@ -509,9 +510,9 @@ size_t EEPROMClass::writeBytes(int address, const void *value, size_t len) {
509510
return len;
510511
}
511512

512-
template<class T> T EEPROMClass::writeAll(int address, const T &value) {
513+
template<class T> size_t EEPROMClass::writeAll(int address, const T &value) {
513514
if (address < 0 || address + sizeof(T) > _size) {
514-
return value;
515+
return 0;
515516
}
516517

517518
memcpy(_data + address, (const uint8_t *)&value, sizeof(T));

libraries/EEPROM/src/EEPROM.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class EEPROMClass {
105105
size_t writeString(int address, const char *value);
106106
size_t writeString(int address, String value);
107107
size_t writeBytes(int address, const void *value, size_t len);
108-
template<class T> T writeAll(int address, const T &);
108+
template<class T> size_t writeAll(int address, const T &);
109109

110110
protected:
111111
nvs_handle _handle;

libraries/ESP32/examples/FreeRTOS/Mutex/Mutex.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ void Task(void *pvParameters) { // This is a task.
7171
#endif
7272
int new_value = random(1000);
7373

74-
char str0[32];
75-
sprintf(str0, " %d <- %d |", shared_variable, new_value);
76-
char str1[32];
77-
sprintf(str1, " | %d <- %d", shared_variable, new_value);
74+
char str0[35]; // Maximum possible length of the string
75+
snprintf(str0, sizeof(str0), " %d <- %d |", shared_variable, new_value);
76+
char str1[46]; // Maximum possible length of the string
77+
snprintf(str1, sizeof(str1), " | %d <- %d", shared_variable, new_value);
7878
Serial.printf("%s\n", task_num ? str0 : str1);
7979

8080
shared_variable = new_value;
8181
delay(random(100)); // wait random time of max 100 ms - simulating some computation
8282

83-
sprintf(str0, " R: %d |", shared_variable);
84-
sprintf(str1, " | R: %d", shared_variable);
83+
snprintf(str0, sizeof(str0), " R: %d |", shared_variable);
84+
snprintf(str1, sizeof(str1), " | R: %d", shared_variable);
8585
Serial.printf("%s\n", task_num ? str0 : str1);
8686
//Serial.printf("Task %d after write: reading %d\n", task_num, shared_variable);
8787

libraries/ESP_NOW/examples/ESP_NOW_Network/ESP_NOW_Network.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
3333

3434
#include <vector>
35+
#include <new> //std::nothrow
3536

3637
/* Definitions */
3738

@@ -235,7 +236,7 @@ void register_new_peer(const esp_now_recv_info_t *info, const uint8_t *data, int
235236

236237
if (current_peer_count < ESPNOW_PEER_COUNT) {
237238
Serial.printf("New peer found: " MACSTR " with priority %d\n", MAC2STR(info->src_addr), priority);
238-
ESP_NOW_Network_Peer *new_peer = new ESP_NOW_Network_Peer(info->src_addr, priority);
239+
ESP_NOW_Network_Peer *new_peer = new (std::nothrow) ESP_NOW_Network_Peer(info->src_addr, priority);
239240
if (new_peer == nullptr || !new_peer->begin()) {
240241
Serial.println("Failed to create or register the new peer");
241242
delete new_peer;

libraries/ESP_NOW/src/ESP32_NOW.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class ESP_NOW_Peer {
8787
friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer &);
8888
};
8989

90+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ESP_NOW)
9091
extern ESP_NOW_Class ESP_NOW;
92+
#endif
9193

9294
#endif

libraries/ESP_SR/src/ESP_SR.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class ESP_SR_Class {
3333
esp_err_t _fill(void *out, size_t len, size_t *bytes_read, uint32_t timeout_ms);
3434
};
3535

36+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ESP_SR)
3637
extern ESP_SR_Class ESP_SR;
38+
#endif
3739

3840
#endif // CONFIG_IDF_TARGET_ESP32S3

libraries/ESPmDNS/src/ESPmDNS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ class MDNSResponder {
127127
mdns_txt_item_t *_getResultTxt(int idx, int txtIdx);
128128
};
129129

130+
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
130131
extern MDNSResponder MDNS;
132+
#endif
131133

132134
#endif /* CONFIG_MDNS_MAX_INTERFACES */
133135
#endif //ESP32MDNS_H

0 commit comments

Comments
 (0)