drivers: gpio: esp32: fix BIT overflow for pins >= 32#107124
Merged
nashif merged 2 commits intozephyrproject-rtos:mainfrom Apr 15, 2026
Merged
drivers: gpio: esp32: fix BIT overflow for pins >= 32#107124nashif merged 2 commits intozephyrproject-rtos:mainfrom
nashif merged 2 commits intozephyrproject-rtos:mainfrom
Conversation
gpio_pin_is_valid() and gpio_pin_is_output_capable() use BIT() which expands to (1UL << n). On 32-bit Xtensa targets, unsigned long is 32 bits, so BIT(n) for n >= 32 is undefined behavior. This causes gpio1 pins (GPIO32+) to always fail validation with -EINVAL, breaking any peripheral connected to GPIO32-GPIO48 on ESP32-S3 (and similar ESP32 variants with gpio1). Fix by using BIT64() which correctly handles pin numbers >= 32 since SOC_GPIO_VALID_GPIO_MASK is already a 64-bit value. Signed-off-by: Matin Lotfaliei <matinlotfali@gmail.com>
Contributor
|
@matinlotfali please, would you also fix the Thanks! |
Same issue as the GPIO driver: esp32_pin_is_valid() and esp32_pin_is_output_capable() use BIT() which overflows on 32-bit Xtensa for pin numbers >= 32. Fix by using BIT64() to match the 64-bit SOC_GPIO_VALID_GPIO_MASK. Signed-off-by: Matin Lotfaliei <matinlotfali@gmail.com>
Contributor
Author
|
@sylvioalves done |
sylvioalves
approved these changes
Apr 10, 2026
|
wmrsouza
approved these changes
Apr 10, 2026
lucien-nxp
approved these changes
Apr 13, 2026
marekmatej
approved these changes
Apr 13, 2026
uLipe
approved these changes
Apr 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



gpio_pin_is_valid()andgpio_pin_is_output_capable()useBIT()whichexpands to
(1UL << n). On 32-bit Xtensa targets,unsigned longis32 bits, so
BIT(n)forn >= 32is undefined behavior.This causes gpio1 pins (GPIO32+) to always fail validation with
-EINVAL, breaking any peripheral connected to GPIO32-GPIO48 onESP32-S3 (and similar ESP32 variants with gpio1).
Fix by using
BIT64()which correctly handles pin numbers >= 32since
SOC_GPIO_VALID_GPIO_MASKis already a 64-bit value.Note that correct devicetree addressing (
&gpio1 Ninstead of&gpio0 (N+32))does not work around this bug.
gpio_esp32_config()converts theport-relative pin to an absolute GPIO number (
io_pin = pin + 32for gpio1)before passing it to
gpio_pin_is_valid(io_pin), so theBIT()overflowstill occurs regardless of how the pin is addressed in the devicetree.
The bug may appear intermittent because the result of
1UL << NforN >= 32is undefined — at runtime on Xtensa hardware the shift amount wraps
(
N & 31, producing a non-zero value that passes the check), but when GCCconstant-folds the expression at compile time with optimizations enabled,
it can produce 0, causing the validation to fail.
Related: #65244