stm32f4xx-hal icon indicating copy to clipboard operation
stm32f4xx-hal copied to clipboard

Pin state violation in public API.

Open YruamaLairba opened this issue 3 years ago • 3 comments

Hi, i discovered several Pins trait allowing to violate the Pin type state system from outside the crate. It concern i2c, i2s, serial and spi module. The way to do this violation is actually simple.

Pseudo example

use stm32f4xx_hal::serial::Pins;

let pins = (gpioa.pa9, gpioa.pa10); // tuple of Input pins
pins.set_alt_mode(); // type state violation, set pin in alternate mode without changing the type.

A simple fix consist to put those trait in a private module to prevent using their methods outside the crate. The trait SetAlternate must also documented, since the dangerous pin state violation ability comes from here.

YruamaLairba avatar May 01 '22 19:05 YruamaLairba

SetAlternate is in private alt mode

burrbull avatar May 02 '22 03:05 burrbull

SetAlternate is in private alt mode

In my example, the set_alt_mode() method comes from the Pins trait which is public https://docs.rs/stm32f4xx-hal/0.13.1/stm32f4xx_hal/serial/trait.Pins.html

YruamaLairba avatar May 05 '22 12:05 YruamaLairba

Yes, this is really not good.

burrbull avatar May 05 '22 12:05 burrbull