getString with char * not working.
justbendev opened this issue ยท 4 comments
Hello ๐
When porting my current project from ESP32 to ESP8266 all my getString calls didn't work as expected.
Basically when using the char * overload to copy the string into my pointer, 0 bytes are copied to memory.
I will investigate the issue when i have time and send a pull request.
for the time being you can use return value of the String overload and store it that way (Less efficient) if someone is facing a similar issue.
Example code to reproduce the bug
#include <Arduino.h>
#include <ArduinoJson.h>
#include <Preferences.h>
Preferences prefs;
char json[] = "{\"p\":\"bTpsL545cVQr7m2AEnQ8GQkrdm1lqH96\"}";
void setup() {
while(!Serial){
Serial.begin(115200);
};
prefs.begin("example");
if (prefs.getBool("factory",true)){
Serial.println("--- FACTORY MODE ---");
StaticJsonDocument <500> doc;
deserializeJson(doc,json);
prefs.putString("test","demo");
prefs.putString("pass",(char *) doc["p"].as<char *>());
prefs.putBool("factory",false);
prefs.end();
ESP.restart();
}
Serial.println("--- INITIALIZE ---");
char PASSWORD[32];
prefs.getString("pass",PASSWORD,32);
Serial.println(PASSWORD);
}
void loop() {
}
Have a nice day ๐
The problem here appears to be the size of the buffer you are reading into.
char PASSWORD[32];
prefs.getString("pass",PASSWORD,32);
The actual data is 32 bytes, BUT the library needs an extra byte for a null terminator.
When enabling the NVS_LOG
, I actually got a warning about this:
[prefs] WARN: not enough space in buffer: 31 < 32
Increasing the buffer helps
This was a design decision, getString
terminates string.
If you don't need string termination, please use getBytes
.
A small test was added: cbd5dd5
Hello
Yeah i ended up figuring that out not long after opening the issue ๐ , i just forgot to update it and close it.
Thank you,
and have a great day.