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

Segmentation fault on G++ when trying to assign json string literal to custom json type. #972

Closed
lesf0 opened this issue Feb 14, 2018 · 11 comments
Assignees
Labels
confirmed solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@lesf0
Copy link

lesf0 commented Feb 14, 2018

Bug Report

This is the minimal code demonstrating the problem.

#include <iostream>
#include "json.hpp"
#include "fifo_map.hpp"

template<class K, class V, class dummy_compare, class A>
using my_workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using my_json = nlohmann::basic_json<my_workaround_fifo_map>;

int main()
{
	my_json foo = R"([1, 2, 3])"_json;
	
	std::cout << foo << std::endl;
}
$ g++ --version
g++ (GCC) 7.3.0
$ uname -a
Linux daddy 4.14.15-1-ARCH #1 SMP PREEMPT Tue Jan 23 21:49:25 UTC 2018 x86_64 GNU/Linux
@nlohmann
Copy link
Owner

Out of curiosity: does it work without fifo_map?

@lesf0
Copy link
Author

lesf0 commented Feb 14, 2018

Do you mean nlohmann::json instead of my_json in the code above? Yes, it works just fine.

@nlohmann
Copy link
Owner

I'll check tonight.

@gregmarr
Copy link
Contributor

Do you have a stack trace? Does it happen with just the assignment line, or does it require the cout?

@lesf0
Copy link
Author

lesf0 commented Feb 14, 2018

cout here is just to suppress unused variable warning, the program actually fails on assignment. I can run this programin debugger tomorrow and attach its output here if it's required.

@gregmarr
Copy link
Contributor

Just thought that if you had it handy, we might be able to see something before Niels gets to look at it tonight.

@nlohmann
Copy link
Owner

When I call my_json foo = my_json::parse("[1, 2, 3]"); instead of my_json foo = R"([1, 2, 3])"_json;, the error disappears. The problems seems to be the definition of _json which does not return basic_json, but json.

stacktrace.txt

The error can be reduced to an assignment from json to the user-defined version:

#include "json.hpp"
#include "fifo_map.hpp"

template<class K, class V, class dummy_compare, class A>
using my_workaround_fifo_map = nlohmann::fifo_map<K, V, nlohmann::fifo_map_compare<K>, A>;
using my_json = nlohmann::basic_json<my_workaround_fifo_map>;

int main()
{
    nlohmann::json x = 1;
    my_json y = x;
}

@nlohmann
Copy link
Owner

I think the problem is that the copy constructor/assignment operator/whatever thinks assigning a json object to nlohmann::basic_json<my_workaround_fifo_map> is a good idea. I think this should be avoided in the first place.

@nlohmann
Copy link
Owner

In #986 there is a fix for a related issue. Maybe it also helps here.

@nlohmann nlohmann mentioned this issue Mar 6, 2018
4 tasks
@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

With PR #986, the original code example compiles and runs without issues. Output is [1,2,3].

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Mar 6, 2018
@nlohmann nlohmann self-assigned this Mar 6, 2018
@nlohmann nlohmann added this to the Release 3.1.2 milestone Mar 6, 2018
@nlohmann
Copy link
Owner

nlohmann commented Mar 6, 2018

Fixed with #986.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

3 participants