micropython / micropython/micropython

Incorrect loop conditional

Open
#6,066 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

py-core
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

I came across a problem while experimenting with MicroPython (v1.9.4). The program crashed while trying to execute this while loop:

        while (--d >= dig) {
            a = (a << DIG_SIZE) | *d;
            *d = a / base;
            a %= base;
        }

The pre-decrement (i.e. --) operation underflows the d pointer which caused the d >= dig condition to succeed (even though it shouldn't). Therefore, the line

*d = a / base

was mistakenly executed using an out-of-bounds pointer d.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in py/mpz.c at the loop around line 1705 and trace the bounds of d through the pre-decrement and comparison. Reproduce the MicroPython v1.9.4 crash described in the issue, then verify that the loop no longer dereferences an out-of-bounds pointer while preserving the intended calculation.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.