-
Notifications
You must be signed in to change notification settings - Fork 7.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
nvs_storage implementation uses asserts and crashes system instead of returning an error (IDFGH-6629) #8271
Comments
Hi @mastrogippo, thanks for pointing out this issue. That specific assert indeed looks incorrect, it should have been a check returning an error, instead.
Generally we try to report errors instead of asserting/aborting on what could be user input, in this case we made a mistake. There are also asserts in NVS which check the internal consistency of the NVS state. The motivation for those asserts is that if one of those fails, then it means the NVS implementation is incorrect or logically inconsistent. Our standard approach for this case is to fail at the earliest point possible to detect the error, and assert does that. However over time we found that occasionally users report assertion failures in NVS where NVS asserts on each start-up (i.e. call to nvs_flash_init) and is never able to recover. Usually in these cases we also see that NVS partition is somehow corrupted. So realistically we should try to give the application a chance to recover from such situation in production, even if in debug mode and during testing we would prefer to assert. There is a task open in our internal issue tracker to replace some/most NVS assertions with recoverable error checks, but it's not completed yet. You are welcome to send a PR for fixing the specific assert which you found! However please note that we would still need to add tests to the NVS test suite to cover this situation, so it's not going to be an automatic merge. Likely we'll have to amend you commit and add the tests. |
I found that there is another issue open which seems related to the same problem, #7837. |
I think the temporary workaround is to first call size_t required_size;
nvs_get_str(my_handle, "server_name", NULL, &required_size);
char* server_name = malloc(required_size);
nvs_get_str(my_handle, "server_name", server_name, &required_size); (this is about nvs_get_str, but nvs_get_blob would work in a similar way) |
Thanks @igrr , since you're already working on it and it's a more widespread issue than my single assert, I'll just implement the workaround for the time being. |
Reopen until the main fix gets merged |
Hello. Environment:Target: ESP32 Debug LogsBacktrace:0x4008b3eb:0x3fff2410 0x4008c227:0x3fff2440 0x4008c22f:0x3fff2460 0x4008fcc9:0x3fff2480 0x4008bdfd:0x3fff24a0 0x4010c8d9:0x3fff24e0 0x40110926:0x3fff2510 0x40194141:0x3fff2530 0x401114fe:0x3fff2550 0x4010fbbd:0x3fff25b0 0x4010ff86:0x3fff2600 0x401100a5:0x3fff2680 0x40110799:0x3fff26f0 0x4010f509:0x3fff2710 0x4010f8d1:0x3fff2750 0x400d74ee:0x3fff2770 0x400df6e0:0x3fff2830 Files:0x4008b3eb: spi_flash_disable_interrupts_caches_and_other_cpu at C:/Users/guilhermepiedade/esp-idf-v5.0.6/esp-idf/components/spi_flash/cache_utils.c:173 (discriminator 1) The last two files refer to my application. This problem happens randomically. |
Environment
Problem Description
NVS blob read function uses asserts in its implementation; this causes a CPU reset in case of error.
In my specific case, the problematic line is:
esp-idf/components/nvs_flash/src/nvs_storage.cpp
Line 460 in 1cb31e5
But there are asserts sprinkled all around the code.
In my use case, I'm reading a blob using:
If len is wrong (e.g. bigger than it's stored value), the CPU crashes
Are those asserts there for a specific reason? Would you merge a fix that returns an error if I spend the time to write it, or should I just implement a workaround for my own code?
Expected Behavior
I would expect a returned error (ESP_ERR_NVS_INVALID_LENGTH) from "get_blob" that I can check and act upon (in my specific case, I would delete the relevant config section and rewrite it with the correct size).
Actual Behavior
System crashes due to assert
Steps to reproduce
Code to reproduce this issue
Debug Logs
The text was updated successfully, but these errors were encountered: