BitBoxSwiss / bitbox02-firmware

Firmware code of the BitBox02 hardware wallet
https://bitbox.swiss/bitbox02
Apache License 2.0
217 stars 81 forks source link

ui/button: copy text into the button data struct #1065

Closed benma closed 1 year ago

benma commented 1 year ago

Before the text that was passed to button_create was simply passed as a pointer to the data struct, making it the caller's responsibility to keep the string alive for as long as the component is alive.

The comments in ui.rs were incorrect:

    bitbox02_sys::trinary_choice_create(
        crate::util::str_to_cstr_vec(message).unwrap().as_ptr(), // copied in C
        crate::util::str_to_cstr_vec(label_left).unwrap().as_ptr(), // copied in C
        crate::util::str_to_cstr_vec(label_middle).unwrap().as_ptr(), // copied in C
        crate::util::str_to_cstr_vec(label_right).unwrap().as_ptr(), // copied in C
        ...

Since the three labels are buttons, the text was not copied and the pointer becomes dangling.

I checked all instances and found that all button label strings are short and fit in 20 bytes. A lower number here is better to save binary space. Can increase it in the future if needed.