use realloc() when increasing the size of _params. addParameter() now returns bool

indicating the status of adding the parameter where false indicates it failed to
increase the size of _params and did not add the parameter.
This commit is contained in:
Christopher Liebman
2017-12-09 06:04:07 -08:00
parent 0b6341bea4
commit d66cd47df8
2 changed files with 11 additions and 8 deletions
+9 -6
View File
@@ -75,24 +75,27 @@ WiFiManager::~WiFiManager()
}
}
void WiFiManager::addParameter(WiFiManagerParameter *p) {
bool WiFiManager::addParameter(WiFiManagerParameter *p) {
if(_paramsCount + 1 > _max_params)
{
// rezise the params array
_max_params += WIFI_MANAGER_MAX_PARAMS;
DEBUG_WM(F("Increasing _max_params to:"));
DEBUG_WM(_max_params);
WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
// copy old data
memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*));
free(_params);
_params = new_params;
WiFiManagerParameter** new_params = (WiFiManagerParameter**)realloc(_params, _max_params * sizeof(WiFiManagerParameter*));
if (new_params != NULL) {
_params = new_params;
} else {
DEBUG_WM("ERROR: failed to realloc params, size not increased!");
return false;
}
}
_params[_paramsCount] = p;
_paramsCount++;
DEBUG_WM("Adding parameter");
DEBUG_WM(p->getID());
return true;
}
void WiFiManager::setupConfigPortal() {
+2 -2
View File
@@ -100,8 +100,8 @@ class WiFiManager
void setAPCallback( void (*func)(WiFiManager*) );
//called when settings have been changed and connection was successful
void setSaveConfigCallback( void (*func)(void) );
//adds a custom parameter
void addParameter(WiFiManagerParameter *p);
//adds a custom parameter, returns false on failure
bool addParameter(WiFiManagerParameter *p);
//if this is set, it will exit after config, even if connection is unsuccessful.
void setBreakAfterConfig(boolean shouldBreak);
//if this is set, try WPS setup when starting (this will delay config portal for up to 2 mins)