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

Allow preventing implicit converions to basic_json #4324

Closed
wants to merge 2 commits into from

Conversation

iFreilicht
Copy link

@iFreilicht iFreilicht commented Mar 27, 2024

Previously, JSON_USE_IMPLICIT_CONVERSIONS only prevented implicit conversions from basic_json to other types.

With this change, implicit conversions to basic_json are also prevented by that macro.

Fixes #2226.

I tested this change also in iFreilicht/json-questions@c0b2f53, the original repro repo from #2226 (comment).

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 files single_include/nlohmann/json.hpp and single_include/nlohmann/json_fwd.hpp. The whole process is described here.

Previously, `JSON_USE_IMPLICIT_CONVERSIONS` only prevented implicit
conversions _from_ `basic_json` to other types.

With this change, implicit conversions _to_ `basic_json` are also
prevented by that macro.
@iFreilicht iFreilicht requested a review from nlohmann as a code owner March 27, 2024 16:57
iFreilicht added a commit to iFreilicht/json-questions that referenced this pull request Mar 27, 2024
@coveralls
Copy link

Coverage Status

coverage: 100.0%. remained the same
when pulling 7c7f7a8 on iFreilicht:develop
into 199dea1 on nlohmann:develop.

@schaumb
Copy link

schaumb commented Mar 27, 2024

As I thought, this will not work easily because a lot of code depends on implicit construction.

@iFreilicht
Copy link
Author

Ah yes, it seems that the CI is running more tests that I didn't run locally. I'll have a look at that.

@gregmarr
Copy link
Contributor

Could we use a new define JSON_USE_IMPLICIT_CONSTRUCTORS for this that defaults to true so that users can opt in when they're ready?

@nlohmann
Copy link
Owner

Good idea.

@iFreilicht
Copy link
Author

So, after playing whack-a-mole with the tests for 2 hours, I think just making the constructor explicit is not enough, it breaks an incredible amount of features, including safe ones (see the latest WIP commit).

A lot of functions already rely on the implicit conversion, so it seems necessary to add overloads for CompatibleTypes to many functions that then convert internally. I'm not sure that's much better than the current state of things.

Maybe there's a smarter way to prevent nested temporary values from being implicitly converted? Maybe if they're required to be moved instead of allowing const references?

Copy link
Contributor

@gregmarr gregmarr left a comment

Choose a reason for hiding this comment

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

Yeah, that is a lot of casting needed. Not very attractive.

@@ -491,6 +491,20 @@ add_custom_target(ci_test_noimplicitconversions
COMMENT "Compile and test with implicit conversions switched off"
)

###############################################################################
# Disable implicit converstructors.
Copy link
Contributor

Choose a reason for hiding this comment

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

constructors


??? example

Disabling constructors is particularly useful to avoid implicit allocations that can lead to invalid memory access:
Copy link
Contributor

Choose a reason for hiding this comment

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

Disabling implicit constructors

@iFreilicht
Copy link
Author

I will not continuing work on this. If someone else is interested in picking it up, feel free to do so.

@iFreilicht iFreilicht closed this Apr 8, 2024
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.

std::tuple dangling reference - implicit conversion
5 participants