Fixed invalid memory access for empty entries

pull/123/head
Hieromon Ikasamo 6 years ago
parent 2afb178faf
commit 5bceb68960
  1. 36
      src/AutoConnectCredential.cpp
  2. 1
      src/AutoConnectCredential.h

@ -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.

@ -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 */

Loading…
Cancel
Save