This is an archive of the discontinued LLVM Phabricator instance.

[llvm][json] Fix UINT64 json parsing
ClosedPublic

Authored by wallace on May 10 2022, 8:58 AM.

Details

Summary

https://reviews.llvm.org/D109347 added support for UINT64 json numeric
types. However, it seems that it didn't properly test uint64_t numbers
larger than the int64_t because the number parsing logic doesn't
have any special handling for these large numbers.

This diffs adds a handler for large numbers, and besides that, fixes the
parsing of signed types by checking for errno ERANGE, which is the
recommended way to check if parsing fails because of out of bounds
errors. Before this diff, strtoll was always returning a number within
the bounds of an int64_t and the bounds check it was doing was completely
superfluous.

As an interesting fact about the old implementation, when calling strtoll
with "18446744073709551615", the largest uint64_t, End was S.end(), even
though it didn't use all digits. Which means that this check can only be
used to identify if the numeric string is malformed or not.

This patch also adds additional tests for extreme cases.

Diff Detail

Event Timeline

wallace created this revision.May 10 2022, 8:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2022, 8:58 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
wallace requested review of this revision.May 10 2022, 8:58 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 10 2022, 8:58 AM
wallace updated this revision to Diff 428398.May 10 2022, 8:59 AM

add additional test

sounds reasonable to me

llvm/lib/Support/JSON.cpp
917

what is this?

llvm/unittests/Support/JSONTest.cpp
388

please add a full-stop

398

please add a full-stop

416

please add a full-stop

428

please add a full-stop

wallace updated this revision to Diff 428992.May 12 2022, 10:17 AM

address comments

wallace marked 5 inline comments as done.May 12 2022, 10:20 AM

LGTM, but I don't own the LLVM JSON parser, so someone else should give the final ok!

dblaikie accepted this revision.May 16 2022, 4:24 PM

Sure, sounds OK.

This revision is now accepted and ready to land.May 16 2022, 4:24 PM
This revision was landed with ongoing or failed builds.May 17 2022, 9:11 AM
This revision was automatically updated to reflect the committed changes.