Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use unsigned indizies for array index in json pointer #2203

Conversation

t-b
Copy link
Contributor

@t-b t-b commented Jun 20, 2020

The current code uses std::stoi to convert the input string to an int
array_index. This limits the maximum addressable array size to ~2GB on most
platforms.

But all callers immediately convert the result of array_index to
BasicJsonType::size_type.

So let's parse it as unsigned long long, which allows us to have as big arrays
as available memory. And also makes the call sites nicer to read.

Forgotten in dcd3a6c (move the catch of std::invalid_argument into
array_index(), 2020-03-23).
@t-b t-b requested a review from nlohmann as a code owner June 20, 2020 14:26
@coveralls
Copy link

coveralls commented Jun 20, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling ecbb275 on t-b:use-unsigned-indizies-for-array-index-in-json-pointer into 29ad217 on nlohmann:develop.

@t-b t-b force-pushed the use-unsigned-indizies-for-array-index-in-json-pointer branch from 6580e3e to 54a3988 Compare June 20, 2020 15:58
@t-b t-b force-pushed the use-unsigned-indizies-for-array-index-in-json-pointer branch 2 times, most recently from fed4474 to 7807a43 Compare June 21, 2020 19:34
@t-b
Copy link
Contributor Author

t-b commented Jun 22, 2020

@nlohmann The coverage decreased because that code only triggers when sizeof(size_type) < sizeof(unsigned long long) (aka 32bit) and it looks like coverage is calculated for 64-bit.

@nlohmann
Copy link
Owner

I see. I tried to build a 32 bit binary, but the CI seems not to support this.

Could you add a comment before the JSON_THROW line that this code is only triggered in 32 bit mode, best with a link to this PR. Then add //LCOV_EXCL_LINE at the end of the JSON_THROW line.

…en parsing

The current code uses std::stoi to convert the input string to an int
array_index. This limits the maximum addressable array size to ~2GB on
most platforms.

But all callers immediately convert the result of array_index to
BasicJsonType::size_type.

So let's parse it as unsigned long long, which allows us to have as
big arrays as available memory. And also makes the call sites nicer to
read.

One complication arises on platforms where size_type is smaller than
unsigned long long. We need to bail out on these if the parsed array
index does not fit into size_type.
@t-b t-b force-pushed the use-unsigned-indizies-for-array-index-in-json-pointer branch from 7807a43 to ecbb275 Compare June 22, 2020 11:44
@t-b
Copy link
Contributor Author

t-b commented Jun 22, 2020

@nlohmann Done.

Copy link
Owner

@nlohmann nlohmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@nlohmann nlohmann self-assigned this Jun 22, 2020
@nlohmann nlohmann added this to the Release 3.8.1 milestone Jun 23, 2020
@nlohmann nlohmann merged commit 4bfe4ad into nlohmann:develop Jun 23, 2020
@nlohmann
Copy link
Owner

Thanks a lot!

@nlohmann
Copy link
Owner


🔖 Release item

This issue/PR will be part of the next release of the library. This template helps preparing the release notes.

Type

  • ✨ New Feature
  • 🐛 Bug Fix
  • ⚡️ Improvement
  • 🔨 Further Change
  • 🔥 Deprecated function

Description


@t-b t-b deleted the use-unsigned-indizies-for-array-index-in-json-pointer branch June 23, 2020 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants