1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-30 03:26:13 +01:00

remove possible RAM corruption and improve findUniqThrottle

This commit is contained in:
Harald Barth 2022-10-18 22:20:38 +02:00
parent 2a51fa6f5d
commit f792e07d40

View File

@ -77,28 +77,38 @@ static uint8_t xstrcmp(const char *s1, const char *s2) {
void WiThrottle::findUniqThrottle(int id, char *u) { void WiThrottle::findUniqThrottle(int id, char *u) {
WiThrottle *wtmyid = NULL; WiThrottle *wtmyid = NULL;
WiThrottle *wtmyuniq = NULL; WiThrottle *wtmyuniq = NULL;
u[16] = '\0';
// search 1, look for clientid match
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle){ for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle){
//DIAG(F("looking at %d as %s"),wt->clientid, wt->uniq); if (wt->clientid == id) {
if (wtmyid == NULL && wt->clientid == id)
wtmyid = wt; wtmyid = wt;
if (wtmyuniq == NULL && xstrcmp(u, wt->uniq) == 0) if (xstrcmp(u, wt->uniq) == 0) // should be most common case
wtmyuniq = wt; return;
break;
}
} }
// search 2, look for string match
for (WiThrottle* wt=firstThrottle; wt!=NULL ; wt=wt->nextThrottle){
if (xstrcmp(u, wt->uniq) == 0) {
wtmyuniq = wt;
break;
}
}
// analyse result of the two for loops:
if (wtmyid == NULL) { // should not happen if (wtmyid == NULL) { // should not happen
DIAG(F("Did not find my own wiThrottle handle")); DIAG(F("Did not find my own wiThrottle handle"));
return; return;
} }
if (wtmyid == wtmyuniq) { // all well, just return; // wtmyuniq == wtmyid has already returned in for loop 1
return;
}
if (wtmyuniq == NULL) { // register uniq in the found id if (wtmyuniq == NULL) { // register uniq in the found id
strncpy(wtmyid->uniq, u, 16); strncpy(wtmyid->uniq, u, 16);
wtmyid->uniq[16] = '\0'; wtmyid->uniq[16] = '\0';
if (Diag::WITHROTTLE) DIAG(F("Client %d registered as %s"),wtmyid->clientid, wtmyid->uniq); if (Diag::WITHROTTLE) DIAG(F("Client %d registered as %s"),wtmyid->clientid, wtmyid->uniq);
return; return;
} }
// do the copy (all other options above) // if we get here wtmyid and wtmyuniq point on objects but differnet ones
// so we need to do the copy (all other options covered above)
for(int n=0; n < MAX_MY_LOCO; n++) for(int n=0; n < MAX_MY_LOCO; n++)
wtmyid->myLocos[n] = wtmyuniq->myLocos[n]; wtmyid->myLocos[n] = wtmyuniq->myLocos[n];
wtmyid->heartBeatEnable = wtmyuniq->heartBeatEnable; wtmyid->heartBeatEnable = wtmyuniq->heartBeatEnable;