Page 1 of 1

ESP_ERR_NVS_INVALID_LENGTH

Posted: Tue Jan 08, 2019 10:35 am
by GeorgesLab
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?

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Tue Jan 08, 2019 11:43 am
by ESP_igrr
Ho do you implement nvs_read_str function?

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Tue Jan 08, 2019 12:13 pm
by GeorgesLab

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;
}
I'm little confused by definition of ESP_ERR_NVS_INVALID_LENGTH error definition. If I understand it right it is raised when target variable is different type or shorter in case of string than saved value in NVS, right?

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Tue Jan 08, 2019 2:09 pm
by ESP_igrr
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

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Tue Jan 08, 2019 5:57 pm
by GeorgesLab
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.

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Wed Jan 09, 2019 3:44 am
by ESP_igrr
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).

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Wed Jan 09, 2019 7:44 am
by GeorgesLab
Excellent, thank you very much for help. I will implement changes and return back with result to finish issue.

Re: ESP_ERR_NVS_INVALID_LENGTH

Posted: Wed Jan 09, 2019 10:24 am
by GeorgesLab
After modification accoridng to your instructions everything works perfectly, thank you once again.