jaraco / jaraco/inflect

number_to_words leaks IndexError instead of NumOutOfRangeError for huge inputs

Open Beginner friendly
#242 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
127
PR merge metrics
No merged PRs in 30d

Description

Hi,

Found via an adversarial test pass on `inflect/__init__.py` against the main branch (version `7.5.1.dev33+g262a247d2`).

`engine().number_to_words(n)` is documented to raise `NumOutOfRangeError` for out-of-range inputs (the exception is imported and advertised). For numbers requiring magnitude index >= 12 (roughly 10^36 and above), instead of that documented exception, an uncaught `IndexError: list index out of range` leaks from `tenfn` at `inflect/__init__.py:3699`.

## Minimal repro

```python
import inflect
p = inflect.engine()
p.number_to_words(10**40)
# Expected: raises NumOutOfRangeError
# Actual: IndexError: list index out of range
# from tenfn -> mill[mindex] (inflect/__init__.py:3699)
```

Same failure mode at `10**36`, `10**40`, and `10**100` (all parametrized in my probe; all three exhibit the same IndexError).

## Why it matters

Library users who wrap calls in `try: ... except NumOutOfRangeError` (which is the documented contract) will not catch `IndexError`. Their code will crash unexpectedly when given a number above the supported range.

## Suggested fix

Either expand the `mill` table to cover larger magnitudes (probably impractical past a point), or guard `mill[mindex]` in `tenfn` with a magnitude-bounds check that raises `NumOutOfRangeError` when the magnitude would exceed `len(mill) - 1`.

Happy to send a PR if helpful. Thanks for the library.

Contributor guide

No contributing guide indexed for this repository

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 inflect/__init__.py at tenfn around line 3699, where mill[mindex] raises IndexError for the documented out-of-range inputs. Reproduce with 10**36, 10**40, and 10**100, then verify that each raises NumOutOfRangeError instead of IndexError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.