dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.12k stars 574 forks source link

Fixing the Segmentation Fault in libgpiod V1 #2321

Closed raffaeler closed 3 weeks ago

raffaeler commented 3 weeks ago

Fixes #2313

This PR fixes the Segmentation Fault occuring when a wrong gpioChip is passed to the libgpiod driver. In a Raspberry PI this is typically 0 whichi is the default but on other boards there may be the need to specify a different number or even more than one (different I/Os are mapped on different chips).

The problem was related to the SafeHandle being non-null but invalid, therefore any following PInvoke to the native libgpiod library resulted in the fault.

After the change, the application calling the driver with a wrong gpioChip receive this exception message which can be handled in a catch statement.

System.IO.IOException: Unable to find a chip, error code: 2

The libgpiod V2 manages the gpioChip differently and it should already ok.

Microsoft Reviewers: Open in CodeFlow