Skip to content

to/from_json Failing To Convert String #2238

@cliffburdick

Description

@cliffburdick

Trying to convert a simple JSON type into a class using the instructions provided in the README.

What is the issue you have?

When the from_json code is called, it seems to successfully run the function, but segfaults in a string copy assignment/operator.

Please describe the steps to reproduce the issue.

Here is the class and the code to convert:


class ExampleJsonConvert
{
    public: 
        std::string a;
        int b;
        float c;
};

void from_json(const nlohmann::json& j, ExampleJsonConvert& p) {
    j.at("a").get_to(p.a);
    j.at("b").get_to(p.b);
    j.at("c").get_to(p.c);
}   

The callback is invoked with the following template:

template <typename T>
int GenericJsonConverter(std::string json_str, void *output)
{
    try {
        T *conv = reinterpret_cast<T*>(output);
        nlohmann::json json_obj = nlohmann::json::parse(json_str);
        *conv = json_obj;
    }
    catch (const nlohmann::json::parse_error &e) {
        printf("Failed to parse payload from PubSub message: %s, exception:%s", json_str.c_str(), e.what());   
        return -1;         
    }     
    catch (const nlohmann::detail::type_error &e) {
        printf("Failed to parse type from PubSub message: %s, exception: %s", json_str.c_str(), e.what());
        return -1;              
    }       

    return 0;
}

GenericJsonConverter<ExampleJsonConvert>(j["payload"].dump(), mybuffer);

The JSON being passed in is as follows:

{
   "hdr" : {"module": "_ANY_", "core": 0, "name": "none"},
   "payload" : {"a": "test", "b":1, "c":2.0}
}

When hitting the callback, I put a print statement after the j.at("c").get_to(p.c); line, and it succeeds, but the stack trace shows:

#0  0x00007fffe0597b6a in ?? ()
#1  0x0000555556553a9b in std::char_traits<char>::copy (__n=4, __s2=<optimized out>, __s1=<optimized out>) at /usr/include/c++/7/bits/char_traits.h:350
#2  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy (__n=4, __s=<optimized out>, __d=<optimized out>) at /usr/include/c++/7/bits/basic_string.h:340
#3  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign (this=this@entry=0x1996f2510, __str=...) at /usr/include/c++/7/bits/basic_string.tcc:272
#4  0x00005555567c1afb in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::assign (__str=..., this=0x1996f2510) at /usr/include/c++/7/bits/basic_string.h:1347
#5  std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator= (__str=..., this=0x1996f2510) at /usr/include/c++/7/bits/basic_string.h:766
#6  TestBed::ExampleJsonConvert::operator= (this=0x1996f2510) at ./src/RPC/interfaces/json_converters/CommonConverters.h:8
#7  TestBed::GenericJsonConverter<TestBed::ExampleJsonConvert> (json_str=..., output=0x1996f2510) at ./src/RPC/interfaces/json_converters/ConverterUtilities.h:16

Can you provide a small but working code example?

See above

What is the expected behavior?

No crash

And what is the actual behavior instead?

Crash

Which compiler and operating system are you using?

  • Compiler: ___
  • Operating system: ___

Which version of the library did you use?

  • [ X ] latest release version 3.8.0
    Ubuntu 18.04
    gcc 7.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions