digitalio, pwmio, keypad, bitbangio: subclasses and arguments taken o… - #11381
Merged
tannewt merged 1 commit intoSep 15, 2026
Merged
Conversation
Author
|
Testing and diagnostic script. |
Member
I don't believe anyone does this. Sometimes we allow it but it is spotty. I wouldn't add it here because it bloats the code size on the samd1. |
tannewt
requested changes
Sep 14, 2026
tannewt
left a comment
Member
There was a problem hiding this comment.
No digitalinout subclass support is needed. Folks shouldn't do this.
digitalio's drive_mode took anything that was not OPEN_DRAIN to mean PUSH_PULL, so switch_to_output(drive_mode=42) configured a push-pull output instead of raising. pwmio.PWMOut put duty_cycle into a uint16_t and validated it afterwards, so 65536 became 0 and -1 became 65535. The setter next to it already checked first. keypad and keypad_demux passed their counts to mp_arg_validate_int_range as the maximum, which is inclusive, so one index past the last was accepted: on a two by three matrix key_number_to_row_column(6) returned (2, 0). bitbangio range-checked neither the I2C frequency nor the SPI baudrate, and both are divisors when the bit delay is worked out. bitbangio.I2C read a bus error as an acknowledgement: write_byte returns -1 for a bus error, 0 for a NAK and 1 for an ACK, and the callers tested !write_byte(), which is true only for the NAK.
peterbay
force-pushed
the
pin-apis-subclasses-and-arguments
branch
from
September 15, 2026 04:30
63d84f7 to
d48973e
Compare
Author
|
Dropped it. |
tannewt
added this pull request to the merge queue
Sep 15, 2026
tannewt
removed this pull request from the merge queue due to the queue being cleared
Sep 15, 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.
Code written by Claude Code, guided and corrected by @peterbay.
The problem
Five defects in the pin and bit-banged bus bindings: three arguments narrowed or compared before being checked, one accepted without being checked at all, and a bus error read as an acknowledgement.
The changes
drive_modeaccepted anything. Anything that was notOPEN_DRAINsilently becamePUSH_PULL, soswitch_to_output(drive_mode=42)andpin.drive_mode = 42configured a push-pull output rather than raising.PWMOutnarrowedduty_cyclebefore validating it. The constructor put it into auint16_tand checked afterwards, soduty_cycle=65536silently became 0 and-1became 65535. The setter next door already validated the value first.keypad's range checks admitted one past the last index.key_count,row_countandcolumn_countwere passed tomp_arg_validate_int_rangeas the maximum, and it is inclusive at both ends. On a two by three matrix,key_number_to_row_column(6)returned(2, 0)androw_column_to_key_number(2, 0)returned 6.bitbangiodid not range-check its rate. Neither the I2C frequency nor the SPI baudrate went through any validation, and both are divisors when the module works out its bit delay. The first version of this also added a maximum of 500000; that was wrong and only the lower bound remains.bitbangio.I2Cread a bus error as an acknowledgement.write_bytereturns -1 for a bus error, 0 for a NAK and 1 for an ACK, and both the data loop ini2c_writeand the address byte ini2c_readtested!write_byte(...), which is true only for the NAK.Testing
Seeed XIAO nRF52840 Sense, on two builds differing only by these changes. Nothing is wired to the pins.
switch_to_output(drive_mode=42)PUSH_PULLValueError: Invalid drive_modepin.drive_mode = 42PUSH_PULLValueError: Invalid drive_modePWMOut(pin, duty_cycle=65536)ValueError: duty_cycle must be 0-65535PWMOut(pin, duty_cycle=-1)ValueErrorpwm.duty_cycle = 65536ValueError, unchanged — the setter always checkedValueErrorkey_number_to_row_column(6)on a 2x3 matrix(2, 0)ValueError: key_number must be 0-5row_column_to_key_number(2, 0)with two rowsValueError: row must be 0-1SPI.configure(baudrate=0)ValueError: baudrate must be >= 1SPI.configure(baudrate=1000000)I2C(scl, sda, frequency=0)ValueError: frequency must be >= 1The bus-error row is not covered: it needs a bus fault to produce, which nothing here can arrange.
No new translatable strings.