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

Serializing std::variant causes stack overflow #1292

Closed
untrioctium opened this issue Oct 12, 2018 · 5 comments
Closed

Serializing std::variant causes stack overflow #1292

untrioctium opened this issue Oct 12, 2018 · 5 comments
Assignees
Labels
release item: 🐛 bug fix solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Milestone

Comments

@untrioctium
Copy link

untrioctium commented Oct 12, 2018

This may be similar to #1261, but version 3.3.0, on my tested compilers, will stack overflow trying to serialize a variant instead of yelling at compile time that it doesn't know how to do it.

With this code:

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

int main() {
	std::variant<int, float, std::string> t = 3.14159f;
	std::cout << nlohmann::json(t).dump() << std::endl;
}

MSVC 19.14.26430 will produce the cryptic and lengthy error C4717:

'nlohmann::detail::json_ref<nlohmann::basic_json<std::map,std::vector,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer> >::json_ref<nlohmann::basic_json<std::map,std::vector,std::basic_string<char,std::char_traits<char>,std::allocator<char> >,bool,__int64,unsigned __int64,double,std::allocator,nlohmann::adl_serializer> ><std::variant<int,float,std::basic_string<char,std::char_traits<char>,std::allocator<char> > > &>': recursive on all control paths, function will cause runtime stack overflow

And crash with a stack overflow when run. GCC 7.3.0 and Clang 6.0.0 will not produce a compile error, but will segfault. An earlier version of json I was working with, 3.1.2, instead complains that it's not sure how to serialize a std::variant on all previously tested compilers.

json.hpp:1545:5: error: static assertion failed: could not find to_json() method in T's namespace
     static_assert(sizeof(BasicJsonType) == 0,

I cannot even begin to claim to know what the resolution of this problem might even vaguely look like, but I hope this is helpful information. Hopefully I'm not just doing something horribly wrong.

@theodelrieu
Copy link
Contributor

Well... oops.

I'll make a fix asap

@nlohmann
Copy link
Owner

@untrioctium Could you please check whether #1294 fixes this issue?

@nlohmann nlohmann added the solution: proposed fix a fix for the issue has been proposed and waits for confirmation label Oct 12, 2018
@untrioctium
Copy link
Author

untrioctium commented Oct 12, 2018

@nlohmann Yes, on all previously tested compilers with that test code, I now get "static assertion failed: could not find to_json() method in T's namespace" using the fixed version. Thanks for the quick resolution @theodelrieu

@nlohmann
Copy link
Owner

Thanks for the quick check, @untrioctium ! And of course, @theodelrieu, thanks for the quick fix!

@zhongcy
Copy link

zhongcy commented May 30, 2019

@nlohmann
Hi, variant is not support in Release 3.6.1? Have any Variant examples?
I test the above example is not success in Release 3.6.1.

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

No branches or pull requests

4 participants