Skip to content

Fix crash parsing non-finite numbers (inf/nan) in namelists - #45

Open
jacquelinegarrahan wants to merge 1 commit into
lume-science:mainfrom
jacquelinegarrahan:bugfix/namelist-nonfinite-numbers
Open

Fix crash parsing non-finite numbers (inf/nan) in namelists#45
jacquelinegarrahan wants to merge 1 commit into
lume-science:mainfrom
jacquelinegarrahan:bugfix/namelist-nonfinite-numbers

Conversation

@jacquelinegarrahan

Copy link
Copy Markdown
Contributor

Fix crash parsing non-finite numbers in namelists

Branch: bugfix/namelist-nonfinite-numbersmain

Bug

lume/parsers/numbers.py::try_int converts a float to int unconditionally:

def try_int(x):
    if x == int(x):   # int(inf) -> OverflowError; int(nan) -> ValueError
        return int(x)
    else:
        return x

number() routes every float-like token through try_int(float(z)), so any
token that float() accepts but int() cannot convert crashes the parser:

Input Before After
"inf" OverflowError inf
"-inf" OverflowError -inf
"nan" ValueError nan
"1e400" OverflowError inf

Both single-value (parse_unrolled_namelist line 123) and vector (line 127)
paths call number(), so a namelist file containing e.g. b = Inf — which
appears in real Fortran/Astra/Impact numeric output — crashed the whole parse.
The numbers.py/namelist.py parsers have no test coverage, so this slipped
through.

Fix

Guard the int() conversion in try_int and return the value unchanged when it
is not a finite integer. Integral floats (1.0 -> 1) and strings are unaffected.

Verification

pytest -q                       # 121 passed
number('inf')  -> inf           # (was OverflowError)
number('nan')  -> nan           # (was ValueError)
number('1e400')-> inf           # (was OverflowError)
number('1.5')  -> 1.5           # unchanged
number('1')    -> 1             # unchanged

🤖 Generated with Claude Code

try_int() called int(x) unconditionally, which raises OverflowError on
infinities and ValueError on NaN. number('inf'), number('nan'), and
overflowing exponents like '1e400' therefore crashed the namelist
parser on valid Fortran/Astra/Impact numeric output. Guard the int()
conversion and return the float unchanged when it is not integral.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant