Skip to content

fix: raise ValidationError for out-of-range int→float in convert - #1129

Closed
Solaris-star wants to merge 1 commit into
msgspec:mainfrom
Solaris-star:fix/1122-convert-int-float-overflow
Closed

fix: raise ValidationError for out-of-range int→float in convert#1129
Solaris-star wants to merge 1 commit into
msgspec:mainfrom
Solaris-star:fix/1122-convert-int-float-overflow

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

msgspec.convert(big_int, float) for integers outside the finite double range leaked a SystemError (<built-in function convert> returned a result with an exception set) instead of raising msgspec.ValidationError.

Root cause: convert_int called PyLong_AsDouble(obj) and passed the result into ms_decode_float without checking PyErr_Occurred(). When the C conversion overflows, Python sets OverflowError but the unchecked path still returns a value, and the interpreter surfaces that as SystemError.

json.decode already reports the same case as ValidationError: Number out of range.

Fix

In convert_int's MS_TYPE_FLOAT branch:

  1. Capture PyLong_AsDouble(obj)
  2. If x == -1.0 && PyErr_Occurred(), clear the exception and return ms_error_with_path("Number out of range%U", path)
  3. Otherwise decode the float as before

Test plan

  • pytest tests/unit/test_convert.py::TestFloat -q8 passed
  • Manual: convert(10**400, float) and nested dict[str, float] raise ValidationError: Number out of range
  • Manual: in-range convert(5, float) == 5.0 still works
  • Matches json.decode(str(10**400).encode(), type=float) error class/message

Fixes #1122

PyLong_AsDouble sets OverflowError for ints outside the finite double
range. convert_int previously passed that value straight into
ms_decode_float without checking PyErr_Occurred, which leaked a
SystemError to callers. Surface the same "Number out of range"
ValidationError that json.decode already uses.

Fixes #1122
@provinzkraut

Copy link
Copy Markdown
Member

There is already a PR attached to this: #1128.

If you have suggestions for a different way to fix it, please attach them to that PR.

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.

convert leaks SystemError on an out-of-range intfloat, where json.decode cleanly raises ValidationError

2 participants