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

Fix C26451 warnnings in to_chars.hpp #1967

Merged
merged 1 commit into from
Apr 17, 2020

Conversation

dota17
Copy link
Contributor

@dota17 dota17 commented Feb 29, 2020

Refer to #1911

Warning C26451 Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value. Cast the value to the wider type before calling operator '-' to avoid overflow (io.2).
.\nlohmann\detail\conversions\to_chars.hpp 993
(also line 997, 1007, 1009, 1017, 1021, 1036 and 1038

In to_chars.hpp, there were asserts before operator -.
This PR will disable warning C26451.


Pull request checklist

Read the Contribution Guidelines for detailed information.

  • Changes are described in the pull request, or an existing issue is referenced.
  • The test suite compiles and runs without error.
  • Code coverage is 100%. Test cases can be added by editing the test suite.
  • The source code is amalgamated; that is, after making changes to the sources in the include/nlohmann directory, run make amalgamate to create the single-header file single_include/nlohmann/json.hpp. The whole process is described here.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Specifically, I am aware of compilation problems with Microsoft Visual Studio (there even is an issue label for these kind of bugs). I understand that even in 2016, complete C++11 support isn't there yet. But please also understand that I do not want to drop features or uglify the code just to make Microsoft's sub-standard compiler happy. The past has shown that there are ways to express the functionality such that the code compiles with the most recent MSVC - unfortunately, this is not the main objective of the project.
  • Please refrain from proposing changes that would break JSON conformance. If you propose a conformant extension of JSON to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 100.0% when pulling 096714f on dota17:dota17-disable into 973c52d on nlohmann:develop.

@nlohmann
Copy link
Owner

nlohmann commented Mar 20, 2020

Is there no way to fix the warning?

@dota17
Copy link
Contributor Author

dota17 commented Mar 23, 2020

Honestly: Can't we just use the original code and suppress the warning from the msvc analyzer? Those casts add absolutely nothing in terms of correctness, readability or safety to the code.

What MSVC complains about is that computation is done in 32 bits first and then the result is widened to 64bits (a.k.a. std::size_t). Such a warning makes sense in circumstances where people expect the result of a computation might need more than 32 bits and thus assign the result to a 64 bit variable, but forget that the computation itself (and resulting overflow) still happens in 32 bits.

From #1969 (comment), i agree with @MBalszun .
There were asserts before operator - so that we can makesure that the computation itself (and resulting overflow) still happens in 32 bits.

But if you dont want this, i will take a new version out.

@dota17 dota17 changed the title disable C26451 for to_chars.hpp Fix C26451 warnnings in to_chars.hpp Mar 23, 2020
@MBalszun
Copy link
Contributor

Assuming this warning needs to be tackled at all and not suppressed, wouldn't it be easiest to just turn k and n into variables of std::size_t or std::ptrdiff_t if they need to be signed instead of casting them to those types all over the code?

@dota17
Copy link
Contributor Author

dota17 commented Mar 23, 2020

Assuming this warning needs to be tackled at all and not suppressed, wouldn't it be easiest to just turn k and n into variables of std::size_t or std::ptrdiff_t if they need to be signed instead of casting them to those types all over the code?

Good idea.
I will try and test.

No, it is not ok for

return append_exponent(buf, n - 1);

inline char* append_exponent(char* buf, int e)

which raises ...possible loss of data.

It is wried if we change append_exponent(buf, n - 1) to append_exponent(buf, len + decimal_exponent - 1). But if nobody against this, it will be ok.

@dota17
Copy link
Contributor Author

dota17 commented Mar 24, 2020

There are some errors in CI. And i tried to fix it.
However, the errors in #2004, #2002 are same as this issue, especially #2004 just add a yml file and don't change any code.

@dota17 dota17 closed this Apr 13, 2020
@dota17 dota17 reopened this Apr 13, 2020
@dota17
Copy link
Contributor Author

dota17 commented Apr 17, 2020

Ready to go.

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 linked an issue Apr 17, 2020 that may be closed by this pull request
@nlohmann nlohmann self-assigned this Apr 17, 2020
@nlohmann nlohmann added this to the Release 3.8.0 milestone Apr 17, 2020
@nlohmann nlohmann merged commit 69ac336 into nlohmann:develop Apr 17, 2020
@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


@nlohmann
Copy link
Owner

Thanks!

@nlohmann nlohmann mentioned this pull request Apr 19, 2020
@dota17 dota17 deleted the dota17-disable branch April 20, 2020 08:51
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.

Compile warnings on MSVC 14.2
4 participants