philbowles/h4plugins

PersistentStorage bug

HamzaHajeir opened this issue · 1 comments

Hi

I've faced and fixed a bug in H4P_PersistentStorage.cpp

Which was at these lines :

    for(auto const& i:items){
        vector<string> nv=split(i,"=");
            psRam[nv[0]]=nv[1];
    }

What's the bug ?

This minimal code always crashes after reboot :

#include<H4Plugins.h>

H4_USE_PLUGINS(115200,20,false);
H4P_PersistentStorage h4ps;

void h4setup()
{
    h4ps.setstring("hello","Hi");   //non-empty
    h4ps.setstring("hello","");     //Empty value.

    h4.once(1000,//call below function once after 1000 ms
        [](){
            ESP.restart(); // Check continuous crashing
        }
    );
}

Analysis :

When your last saved value to SPIFFS is EMPTY string i.e : h4ps.setstring("hello","");
nv1 (in H4P_PersistentStorage.cpp) becomes nv::end(); not a value of string, then it crashes.

Suggested solution (worked for me) :
Add a size check of nv before proceeding :

    for(auto const& i:items){
        vector<string> nv=split(i,"=");
        if (nv.size() == 2)
            psRam[nv[0]]=nv[1];
    }

The best ;)

fixed for imminent v1.0.1