Skip to content

zephyrCommon: fix issues with invalid pin numbers#178

Merged
soburi merged 1 commit into
zephyrproject-rtos:nextfrom
soburi:invalid_pin_check
May 31, 2026
Merged

zephyrCommon: fix issues with invalid pin numbers#178
soburi merged 1 commit into
zephyrproject-rtos:nextfrom
soburi:invalid_pin_check

Conversation

@soburi

@soburi soburi commented Apr 19, 2026

Copy link
Copy Markdown
Member

Passing -1 to digital I/O functions may happen when libraries do not
properly sanitize their inputs, leading to undefined behavior. This
commit adds a check to ensure that the pin number is valid before
accessing the arduino_pins array.

Note:
The code related to CONFIG_PINCTRL_DYNAMIC will also be included,
as CONFIG is disabled in this repository. so not affected.

@soburi soburi changed the title Invalid pin check zephyrCommon: fix issues with invalid pin numbers Apr 19, 2026
@soburi soburi marked this pull request as ready for review April 20, 2026 00:10
@soburi soburi requested a review from DhruvaG2000 as a code owner April 20, 2026 00:10
Copilot AI review requested due to automatic review settings April 20, 2026 00:10
@soburi soburi marked this pull request as draft April 20, 2026 00:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the Zephyr-based Arduino core against invalid digital pin numbers (e.g., -1) and extends peripheral lifecycle handling to better support dynamic pinctrl/device deinit scenarios, alongside several analog/timing API additions.

Changes:

  • Add early-return guards to prevent out-of-bounds access of the arduino_pins array across multiple GPIO/interrupt/timing APIs.
  • Introduce optional peripheral re-init / de-init hooks (CONFIG-gated) for I2C/SPI/UART and add a pin-to-peripheral reinit helper.
  • Add/extend analog + timing APIs (DAC support, read/write resolution APIs, cycle/time conversion macros, and inline delay helpers).

Reviewed changes

Copilot reviewed 11 out of 11 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
libraries/Wire/Wire.cpp Adds CONFIG-gated device init/deinit calls in begin()/end()
libraries/SPI/SPI.cpp Adds CONFIG-gated device init/deinit calls in begin()/end()
cores/arduino/zephyrSerial.h Adds CONFIG-gated UART deinit in end()
cores/arduino/zephyrSerial.cpp Adds CONFIG-gated UART init in begin()
cores/arduino/zephyrInternal.h Exposes CONFIG-gated peripheral reinit helper prototype
cores/arduino/zephyrCommon.cpp Adds invalid-pin guards, tone concurrency changes, analog resolution/DAC support, and peripheral reinit logic
cores/arduino/time_macros.h New timing/cycle conversion macros
cores/arduino/overloads.h New overload/resolution getter declarations
cores/arduino/inlines.h New inline implementations for delay APIs
cores/arduino/Arduino.h Adds DAC declarations, resolution APIs, extra includes, and namespace exposure
Kconfig Removes Rust core option; tweaks ARDUINO_MAX_TONES help text

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cores/arduino/zephyrCommon.cpp Outdated
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/Arduino.h
Comment thread cores/arduino/Arduino.h
Comment thread cores/arduino/overloads.h
Comment thread cores/arduino/inlines.h
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrCommon.cpp Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cores/arduino/Arduino.h
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrInternal.h Outdated
Comment thread cores/arduino/inlines.h
@soburi soburi force-pushed the invalid_pin_check branch 3 times, most recently from a7ff7cb to ea2881d Compare April 25, 2026 05:36
@soburi soburi force-pushed the invalid_pin_check branch 4 times, most recently from 36d9450 to 7ee4612 Compare May 3, 2026 22:15
@soburi soburi marked this pull request as ready for review May 3, 2026 22:28
@soburi soburi force-pushed the invalid_pin_check branch 2 times, most recently from 078ef3e to 1a9bcba Compare May 31, 2026 06:57
Passing -1 to digital I/O functions may happen when libraries do not
properly sanitize their inputs, leading to undefined behavior. This
commit adds a check to ensure that the pin number is valid before
accessing the arduino_pins array.

Signed-off-by: Luca Burelli <l.burelli@arduino.cc>
Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
@soburi soburi force-pushed the invalid_pin_check branch from 1a9bcba to e6531a0 Compare May 31, 2026 07:05
@soburi soburi merged commit 9521617 into zephyrproject-rtos:next May 31, 2026
3 checks passed
@soburi soburi deleted the invalid_pin_check branch May 31, 2026 10:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants