From 5bceb689604df2a54f65ec7899c37ad274d80906 Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Thu, 29 Aug 2019 15:44:27 +0900 Subject: [PATCH] Fixed invalid memory access for empty entries --- src/AutoConnectCredential.cpp | 36 +++++++++++++++++++++++------------ src/AutoConnectCredential.h | 1 + 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/AutoConnectCredential.cpp b/src/AutoConnectCredential.cpp index 8e335ab..d7202fe 100644 --- a/src/AutoConnectCredential.cpp +++ b/src/AutoConnectCredential.cpp @@ -340,14 +340,8 @@ AutoConnectCredential::~AutoConnectCredential() { * @retval true The entry successfully delete. * false Could not deleted. */ -bool AutoConnectCredential::del(const char* ssid) { - decltype(_credit)::iterator it = _credit.find(String(ssid)); - if (it != _credit.end()) { - _credit.erase(it); - _entries = _credit.size(); - return true; - } - return false; +inline bool AutoConnectCredential::del(const char* ssid) { + return _del(ssid, true); } /** @@ -421,9 +415,7 @@ bool AutoConnectCredential::_add(const station_config_t* config) { if (ssid.length() > 0) { // Remove a same entry to insert a new one. - decltype(_credit)::iterator it = _credit.find(ssid); - if (it != _credit.end()) - _credit.erase(it); + _del(ssid.c_str(), false); // Insert AC_CREDTBODY_t credtBody; @@ -482,7 +474,8 @@ size_t AutoConnectCredential::_commit(void) { memcpy(&credtPool[dp], credtBody.bssid, sizeof(station_config_t::bssid)); dp += sizeof(station_config_t::bssid); } - credtPool[dp] = '\0'; // Terminates a container + if (_credit.size() > 0) + credtPool[dp] = '\0'; // Terminates a container // Write back to the nvs if (_pref->begin(AC_CREDENTIAL_NVSNAME, false)) { sz = _pref->putBytes(AC_CREDENTIAL_NVSKEY, credtPool, psz); @@ -501,6 +494,25 @@ size_t AutoConnectCredential::_commit(void) { return sz; } +/** + * Actualy delete the credential entry for the specified SSID from Preferences. + * @param ssid A SSID character string to be deleted. + * @param commit If false, delete only a credential entry without updating Preferences. + * @retval true The entry successfully delete. + * false Could not deleted. + */ +bool AutoConnectCredential::_del(const char* ssid, const bool commit) { + decltype(_credit)::iterator it = _credit.find(String(ssid)); + if (it != _credit.end()) { + _credit.erase(it); + _entries = _credit.size(); + if (commit) + _commit(); + return true; + } + return false; +} + /** * Import the credentials bulk data as Preferences from NVS. * In ESP32, AutoConnect stores credentials in NVS from v1.0.0. diff --git a/src/AutoConnectCredential.h b/src/AutoConnectCredential.h index 3d38cc5..a2cf895 100644 --- a/src/AutoConnectCredential.h +++ b/src/AutoConnectCredential.h @@ -137,6 +137,7 @@ class AutoConnectCredential : public AutoConnectCredentialBase { bool _add(const station_config_t* config); /**< Add an entry */ size_t _commit(void); /**< Write back to the nvs */ + bool _del(const char* ssid, const bool commit); /**< Deletes an entry */ uint8_t _import(void); /**< Import from the nvs */ void _obtain(AC_CREDT_t::iterator const& it, station_config_t* config); /**< Obtain an entry from iterator */