Skip to content

fix(cpp): Fixes for cpp code scanning alerts #11704

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions cores/esp32/esp32-hal-time.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@
#endif

static void setTimeZone(long offset, int daylight) {
char cst[17] = {0};
char cdt[17] = "DST";
char tz[33] = {0};
char cst[21] = {0};
char cdt[21] = "DST";
char tz[41] = {0};

if (offset % 3600) {
sprintf(cst, "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
snprintf(cst, sizeof(cst), "UTC%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
} else {
sprintf(cst, "UTC%ld", offset / 3600);
snprintf(cst, sizeof(cst), "UTC%ld", offset / 3600);
}
if (daylight != 3600) {
long tz_dst = offset - daylight;
if (tz_dst % 3600) {
sprintf(cdt, "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
snprintf(cdt, sizeof(cdt), "DST%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
} else {
sprintf(cdt, "DST%ld", tz_dst / 3600);
snprintf(cdt, sizeof(cdt), "DST%ld", tz_dst / 3600);
}
}
sprintf(tz, "%s%s", cst, cdt);
snprintf(tz, sizeof(tz), "%s%s", cst, cdt);
setenv("TZ", tz, 1);
tzset();
}
Expand Down
27 changes: 27 additions & 0 deletions libraries/AsyncUDP/src/AsyncUDP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,33 @@ AsyncUDPPacket::AsyncUDPPacket(AsyncUDPPacket &packet) {
pbuf_ref(_pb);
}

AsyncUDPPacket& AsyncUDPPacket::operator=(const AsyncUDPPacket& packet) {
if (this != &packet) {
if (_pb) {
// Free existing pbuf reference
pbuf_free(_pb);
}

// Copy all members
_udp = packet._udp;
_pb = packet._pb;
_if = packet._if;
_data = packet._data;
_len = packet._len;
_index = 0;

memcpy(&_remoteIp, &packet._remoteIp, sizeof(ip_addr_t));
memcpy(&_localIp, &packet._localIp, sizeof(ip_addr_t));
_localPort = packet._localPort;
_remotePort = packet._remotePort;
memcpy(_remoteMac, packet._remoteMac, 6);

// Increment reference count for the new pbuf
pbuf_ref(_pb);
}
return *this;
}

AsyncUDPPacket::AsyncUDPPacket(AsyncUDP *udp, pbuf *pb, const ip_addr_t *raddr, uint16_t rport, struct netif *ntif) {
_udp = udp;
_pb = pb;
Expand Down
3 changes: 3 additions & 0 deletions libraries/AsyncUDP/src/AsyncUDP.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ class AsyncUDPPacket : public Stream {

size_t write(const uint8_t *data, size_t len);
size_t write(uint8_t data);

// Copy assignment operator
AsyncUDPPacket& operator=(const AsyncUDPPacket& packet);
};

class AsyncUDP : public Print {
Expand Down
25 changes: 13 additions & 12 deletions libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <nvs.h>
#include <esp_partition.h>
#include <esp_log.h>
#include <new>

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

Expand Down Expand Up @@ -59,31 +60,31 @@ bool EEPROMClass::begin(size_t size) {
}
if (size < key_size) { // truncate
log_w("truncating EEPROM from %d to %d", key_size, size);
uint8_t *key_data = (uint8_t *)malloc(key_size);
uint8_t *key_data = new(std::nothrow) uint8_t[key_size];
if (!key_data) {
log_e("Not enough memory to truncate EEPROM!");
return false;
}
nvs_get_blob(_handle, _name, key_data, &key_size);
nvs_set_blob(_handle, _name, key_data, size);
nvs_commit(_handle);
free(key_data);
delete[] key_data;
} else if (size > key_size) { // expand or new
size_t expand_size = size - key_size;
uint8_t *expand_key = (uint8_t *)malloc(expand_size);
uint8_t *expand_key = new(std::nothrow) uint8_t[expand_size];
if (!expand_key) {
log_e("Not enough memory to expand EEPROM!");
return false;
}
// check for adequate free space
if (nvs_set_blob(_handle, "expand", expand_key, expand_size)) {
log_e("Not enough space to expand EEPROM from %d to %d", key_size, size);
free(expand_key);
delete[] expand_key;
return false;
}
free(expand_key);
delete[] expand_key;
nvs_erase_key(_handle, "expand");
uint8_t *key_data = (uint8_t *)malloc(size);
uint8_t *key_data = new(std::nothrow) uint8_t[size];
if (!key_data) {
log_e("Not enough memory to expand EEPROM!");
return false;
Expand All @@ -99,15 +100,15 @@ bool EEPROMClass::begin(size_t size) {
}
nvs_commit(_handle);
nvs_set_blob(_handle, _name, key_data, size);
free(key_data);
delete[] key_data;
nvs_commit(_handle);
}

if (_data) {
delete[] _data;
}

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

size_t size = mypart->size;
uint8_t *data = (uint8_t *)malloc(size);
uint8_t *data = new(std::nothrow) uint8_t[size];
if (!data) {
log_e("Not enough memory to convert EEPROM!");
goto exit;
Expand Down Expand Up @@ -255,7 +256,7 @@ uint16_t EEPROMClass::convert(bool clear, const char *EEPROMname, const char *nv
}
}
exit:
free(data);
delete[] data;
return result;
}

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

template<class T> T EEPROMClass::writeAll(int address, const T &value) {
template<class T> size_t EEPROMClass::writeAll(int address, const T &value) {
if (address < 0 || address + sizeof(T) > _size) {
return value;
return 0;
}

memcpy(_data + address, (const uint8_t *)&value, sizeof(T));
Expand Down
2 changes: 1 addition & 1 deletion libraries/EEPROM/src/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class EEPROMClass {
size_t writeString(int address, const char *value);
size_t writeString(int address, String value);
size_t writeBytes(int address, const void *value, size_t len);
template<class T> T writeAll(int address, const T &);
template<class T> size_t writeAll(int address, const T &);

protected:
nvs_handle _handle;
Expand Down
12 changes: 6 additions & 6 deletions libraries/ESP32/examples/FreeRTOS/Mutex/Mutex.ino
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,17 @@ void Task(void *pvParameters) { // This is a task.
#endif
int new_value = random(1000);

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <esp_mac.h> // For the MAC2STR and MACSTR macros

#include <vector>
#include <new> //std::nothrow

/* Definitions */

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

if (current_peer_count < ESPNOW_PEER_COUNT) {
Serial.printf("New peer found: " MACSTR " with priority %d\n", MAC2STR(info->src_addr), priority);
ESP_NOW_Network_Peer *new_peer = new ESP_NOW_Network_Peer(info->src_addr, priority);
ESP_NOW_Network_Peer *new_peer = new (std::nothrow) ESP_NOW_Network_Peer(info->src_addr, priority);
if (new_peer == nullptr || !new_peer->begin()) {
Serial.println("Failed to create or register the new peer");
delete new_peer;
Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP_NOW/src/ESP32_NOW.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ class ESP_NOW_Peer {
friend bool ESP_NOW_Class::removePeer(ESP_NOW_Peer &);
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ESP_NOW)
extern ESP_NOW_Class ESP_NOW;
#endif

#endif
2 changes: 2 additions & 0 deletions libraries/ESP_SR/src/ESP_SR.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class ESP_SR_Class {
esp_err_t _fill(void *out, size_t len, size_t *bytes_read, uint32_t timeout_ms);
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ESP_SR)
extern ESP_SR_Class ESP_SR;
#endif

#endif // CONFIG_IDF_TARGET_ESP32S3
2 changes: 2 additions & 0 deletions libraries/ESPmDNS/src/ESPmDNS.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ class MDNSResponder {
mdns_txt_item_t *_getResultTxt(int idx, int txtIdx);
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_MDNS)
extern MDNSResponder MDNS;
#endif

#endif /* CONFIG_MDNS_MAX_INTERFACES */
#endif //ESP32MDNS_H
2 changes: 2 additions & 0 deletions libraries/Ethernet/src/ETH.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ class ETHClass : public NetworkInterface {
friend class EthernetClass; // to access beginSPI
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_ETH)
extern ETHClass ETH;
#endif

#endif /* _ETH_H_ */
#endif /* CONFIG_ETH_ENABLED */
14 changes: 8 additions & 6 deletions libraries/FFat/examples/FFat_time/FFat_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.print(" DIR : ");
Serial.print(file.name());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour,
tmstruct->tm_min, tmstruct->tm_sec
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour,
tmstruct.tm_min, tmstruct.tm_sec
);
if (levels) {
listDir(fs, file.path(), levels - 1);
Expand All @@ -42,10 +43,11 @@ void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour,
tmstruct->tm_min, tmstruct->tm_sec
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour,
tmstruct.tm_min, tmstruct.tm_sec
);
}
file = root.openNextFile();
Expand Down
2 changes: 2 additions & 0 deletions libraries/FFat/src/FFat.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class F_Fat : public FS {

} // namespace fs

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_FFAT)
extern fs::F_Fat FFat;
#endif

#endif /* _FFAT_H_ */
2 changes: 1 addition & 1 deletion libraries/FS/src/vfs_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ FileImplPtr VFSImpl::open(const char *fpath, const char *mode, const bool create
if ((mode && mode[0] != 'r') && create) {

char *token;
char *folder = (char *)malloc(strlen(fpath));
char *folder = (char *)malloc(strlen(fpath) + 1);

int start_index = 0;
int end_index = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ void setClock() {
struct tm timeinfo;
gmtime_r(&nowSecs, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
char buf[26];
Serial.print(asctime_r(&timeinfo, buf));
}

WiFiMulti WiFiMulti;
Expand Down
10 changes: 6 additions & 4 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1593,8 +1593,9 @@ void HTTPClient::setCookie(String date, String headerValue) {

// overwrite or delete cookie in/from cookie jar
time_t now_local = time(NULL);
time_t now_gmt = mktime(gmtime(&now_local));

struct tm tm_gmt;
gmtime_r(&now_local, &tm_gmt);
time_t now_gmt = mktime(&tm_gmt);
bool found = false;

for (auto c = _cookieJar->begin(); c != _cookieJar->end(); ++c) {
Expand All @@ -1619,8 +1620,9 @@ void HTTPClient::setCookie(String date, String headerValue) {

bool HTTPClient::generateCookieString(String *cookieString) {
time_t now_local = time(NULL);
time_t now_gmt = mktime(gmtime(&now_local));

struct tm tm_gmt;
gmtime_r(&now_local, &tm_gmt);
time_t now_gmt = mktime(&tm_gmt);
*cookieString = "";
bool found = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void setClock() {
struct tm timeinfo;
gmtime_r(&now, &timeinfo);
Serial.print(F("Current time: "));
Serial.print(asctime(&timeinfo));
char buf[26];
Serial.print(asctime_r(&timeinfo, buf));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions libraries/Insights/src/Insights.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ class ESPInsightsClass {
bool event(const char *tag, const char *format, ...);
};

#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_INSIGHTS)
extern ESPInsightsClass Insights;
#endif

extern "C" {
#endif
Expand Down
14 changes: 8 additions & 6 deletions libraries/LittleFS/examples/LITTLEFS_time/LITTLEFS_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.print(" DIR : ");
Serial.print(file.name());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour,
tmstruct->tm_min, tmstruct->tm_sec
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour,
tmstruct.tm_min, tmstruct.tm_sec
);
if (levels) {
listDir(fs, file.path(), levels - 1);
Expand All @@ -54,10 +55,11 @@ void listDir(fs::FS &fs, const char *dirname, uint8_t levels) {
Serial.print(" SIZE: ");
Serial.print(file.size());
time_t t = file.getLastWrite();
struct tm *tmstruct = localtime(&t);
struct tm tmstruct;
localtime_r(&t, &tmstruct);
Serial.printf(
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour,
tmstruct->tm_min, tmstruct->tm_sec
" LAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour,
tmstruct.tm_min, tmstruct.tm_sec
);
}
file = root.openNextFile();
Expand Down
Loading
Loading