esp-rs / esp-idf-svc

Type-Safe Rust Wrappers for various ESP-IDF services (WiFi, Network, Httpd, Logging, etc.)
https://docs.esp-rs.org/esp-idf-svc/
Apache License 2.0
333 stars 183 forks source link

EspEncryptedNVS cannot find partition by name #384

Closed MartinBroers closed 8 months ago

MartinBroers commented 8 months ago

When opening an encrypted partition via src/nvs.rs, it tries to find the partition by name. However, the code seems not to work. I have created a minimal viable sample in https://github.com/MartinBroers/rust_c_ffi (don't mind the naming there; it is all about the code). The way the Option<&str> is parsed into an CString object somewhere goes awry, maybe because references are dropped in the meantime?

Run the code in the linked repo by running cargo run and the expected output is found it, But it prints garbage in the puts call as well. Can imagine the strcmp call also fails.... The rust code converts a &str to CString which is used in calling 2 separate C functions, just as esp-idf-svc does when calling esp_partition_find_first.

NvsEncrypted::init called via new takes an key_partition: Option<&str>, and converts this into a CString or core::ptr::null when the option is None. The code works when the key_partition name is not provided, but that hinges on only having a single key partition, which may not cover all use cases.