Skip to content

shared/netutils: Factor mp_hal_get_mac_ascii out of ports. - #19674

Open
pablogventura wants to merge 1 commit into
micropython:masterfrom
pablogventura:ports-mac-ascii-bounds
Open

pablogventura wants to merge 1 commit into
micropython:masterfrom
pablogventura:ports-mac-ascii-bounds

Conversation

@pablogventura

@pablogventura pablogventura commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #19668.

mp_hal_get_mac_ascii indexed mac[chr_off >> 1] without checking that the requested hex slice stays within the 12-character MAC representation, so chr_off == 12 read mac[6].

The same helper was duplicated in stm32, rp2, mimxrt, renesas-ra, and alif. Move it to shared/netutils/get_mac_ascii.c and add assert(chr_off <= 12 && chr_len <= 12 - chr_off) in that single copy. In-tree callers use (8, 4) and are unchanged.

Testing

  • Relies on port CI builds (C HAL; no Python test)
  • Diff removes the five port copies and wires get_mac_ascii.c into each port's shared/netutils sources

Trade-offs and Alternatives

assert (not a runtime clamp): zero cost in typical release builds with NDEBUG, matches other HAL programmer-error checks. Invalid callers still trap in assert-enabled builds.

shared/netutils rather than a new extmod/ file: this is a portable C HAL helper (no Python module), and these ports already build shared/netutils.

No unix .text delta (port HAL / shared helper only).

Generative AI

I used generative AI tools when creating this PR, but a human has checked the
code and is responsible for the code and the description above.

@github-actions

github-actions Bot commented Aug 31, 2026

Copy link
Copy Markdown

Code size report:

Reference:  stm32/boards: Use LSI-matched RTC prescalers on Arduino boards. [14e9793]
Comparison: shared/netutils: Factor mp_hal_get_mac_ascii out of ports. [merge of cbf4ccd]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
      esp32:    +0 +0.000% ESP32_GENERIC
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The assert looks reasonable, but this looks like we should actually move this utility function somewhere else (maybe a new file in extmod/?) instead of maintaining five identical copies of it.

Move the identical helper from stm32/rp2/mimxrt/renesas-ra/alif into
shared/netutils and assert the hex character slice stays within the
12-character MAC representation.

Fixes micropython#19668.

Signed-off-by: Pablo Ventura <pablogventura@gmail.com>
@pablogventura

Copy link
Copy Markdown
Contributor Author

@projectgus Thanks for the review. Consolidated the five copies into shared/netutils/get_mac_ascii.c (portable HAL helper; these ports already build shared/netutils) and kept the bounds assert in that single implementation.

@pablogventura pablogventura changed the title ports: Avoid OOB in mp_hal_get_mac_ascii. shared/netutils: Factor mp_hal_get_mac_ascii out of ports. Sep 5, 2026
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.55%. Comparing base (14e9793) to head (cbf4ccd).

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #19674      +/-   ##
==========================================
- Coverage   98.56%   98.55%   -0.01%     
==========================================
  Files         182      182              
  Lines       23335    23335              
  Branches        5        5              
==========================================
- Hits        22999    22998       -1     
- Misses        335      336       +1     
  Partials        1        1              
Flag Coverage Δ
unix-coverage-32bit 98.55% <ø> (-0.01%) ⬇️
unix-coverage-64bit 98.52% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.

off by one in mp_hal_get_mac_ascii

2 participants