Hello,
I have some problems with NVS, when trying to restore more values from NVS then some of them returns error :
Error (ESP_ERR_NVS_INVALID_LENGTH) reading pe_0 size 15!
size is value of size_t required_size; variable.
NVS statistics
Count: UsedEntries = (74), FreeEntries = (2194), AllEntries = (2268)
variable used:
char smtps_recipients[4][128];
command for store:
sprintf(key,"pe_0");
nvs_write_str(key,smtps_recipients[0]);
command for restore:
sprintf(key,"pe_0");
nvs_read_str(key,smtps_recipients[0]);
when I dumped NVS partition I can see that all values are stored properly. So what is problem with loading?
ESP_ERR_NVS_INVALID_LENGTH
Re: ESP_ERR_NVS_INVALID_LENGTH
Ho do you implement nvs_read_str function?
-
- Posts: 7
- Joined: Tue Jan 08, 2019 10:31 am
Re: ESP_ERR_NVS_INVALID_LENGTH
Code: Select all
uint32_t nvs_read_str(const char* key, char * value)
{
esp_err_t err;
size_t required_size;
// Open
nvs_handle my_handle;
err = nvs_open("storage", NVS_READWRITE, &my_handle);
if (err != ESP_OK) {
printf("Error (%s) opening NVS handle!\n", esp_err_to_name(err));
}
else {
// Read
*value = 0; // value will default to 0xFF, if not set yet in NVS
err = nvs_get_str(my_handle, key, value,&required_size);
switch (err) {
case ESP_OK:
break;
case ESP_ERR_NVS_NOT_FOUND:
printf("The value %s is not initialized yet!\n",key);
break;
default :
printf("Error (%s) reading %s size %u!\n", esp_err_to_name(err),key,required_size);
break;
}
// Close
nvs_close(my_handle);
}
return (uint32_t)err;
}
Re: ESP_ERR_NVS_INVALID_LENGTH
No, you need to initialize "required_size" variable with the amount of space available in the destination buffer. Currently the variable is uninitialized, and contains garbage.
If you want to know what is the size required to store the result, you can first call nvs_get_str with NULL instead of the destination buffer. Then the size argument will receive the size of the string stored in NVS. See "print_what_saved" function of the following example: https://github.com/espressif/esp-idf/bl ... ple_main.c
If you want to know what is the size required to store the result, you can first call nvs_get_str with NULL instead of the destination buffer. Then the size argument will receive the size of the string stored in NVS. See "print_what_saved" function of the following example: https://github.com/espressif/esp-idf/bl ... ple_main.c
-
- Posts: 7
- Joined: Tue Jan 08, 2019 10:31 am
Re: ESP_ERR_NVS_INVALID_LENGTH
Thank you I will try that.
What is purpose of this approach? Why I need to declare how large is target buffer to read function?
I understand that for malloc is necessary to know how large data are, so nvs_get with NULL parameter is logical. But when I know how large is stored value and I have static size of buffer then I don't understand why is necesary to provide buffer size.
What is purpose of this approach? Why I need to declare how large is target buffer to read function?
I understand that for malloc is necessary to know how large data are, so nvs_get with NULL parameter is logical. But when I know how large is stored value and I have static size of buffer then I don't understand why is necesary to provide buffer size.
Re: ESP_ERR_NVS_INVALID_LENGTH
It's mostly because in C language, writing into a destination buffer without knowing how large it is is unsafe. You may know the buffer size today, but then it is not uncommon for some parameters in the application to change so that the size of the destination buffer becomes insufficient. Without explicitly passing the size of the buffer around, this goes unchecked, and you may be left with a hard to debug memory corruption. Same logic is applied when certain C library string functions (sprintf, strcpy, strcat) are recognized as unsafe, and alternative functions which take destination buffer size as an argument are proposed instead (snprintf, strncpy, strncat).
-
- Posts: 7
- Joined: Tue Jan 08, 2019 10:31 am
Re: ESP_ERR_NVS_INVALID_LENGTH
Excellent, thank you very much for help. I will implement changes and return back with result to finish issue.
-
- Posts: 7
- Joined: Tue Jan 08, 2019 10:31 am
Re: ESP_ERR_NVS_INVALID_LENGTH
After modification accoridng to your instructions everything works perfectly, thank you once again.
Who is online
Users browsing this forum: No registered users and 89 guests