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

I was forced to report an assertion error when copying an array of strings #3419

Closed
skychips opened this issue Apr 5, 2022 · 5 comments
Closed
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead

Comments

@skychips
Copy link

skychips commented Apr 5, 2022

Description

Goes to structures, and string arrays, in the C way, throwing exceptions.

According to the normal mode of understanding, a string is also an array, and we can force to specify, whether he is a string, or an array of strings.
Or, whether we want to use the array approach to strings or the string object approach to strings.

The design of these structures requires the use of 'char array' or else a second copy of the code would have to be wrapped for C to call, since C does not exist for std::string.

So I think the 'char []' array of strings in structs is legal.

Reproduction steps

Paste the Minimal code example code into the console for debugging, and it will directly trigger the assertion issue

Expected vs. actual results

I hope that the string array 'char' can be copied correctly

Minimal code example

namespace ns
{
    using json = typename::nlohmann::basic_json<nlohmann::ordered_map>;
    // a simple struct to model a person
    struct person
    {
        char name[256];
        int age;
    };

    void to_json(json& j, const person& p)
    {
        j = json{ {"name", p.name}, {"age", p.age} };
    }

    void from_json(const json& j, person& p)
    {
        j.at("name").get_to(p.name);
        j.at("age").get_to(p.age);
    }
}
int main() {
    ns::person p{ "Ned Flanders", 60 };

    ns::json j = p;

    std::cout << j << std::endl;

    auto p2 = j.get<ns::person>();
}

Error messages

/// @brief access specified array element with bounds checking
    /// @sa https://json.nlohmann.me/api/basic_json/at/
    const_reference at(size_type idx) const
    {
        // at only works for arrays
        if (JSON_HEDLEY_LIKELY(is_array()))
        {
            JSON_TRY
            {
                return m_value.array->at(idx);
            }
            JSON_CATCH (std::out_of_range&)
            {
                // create better exception explanation
                JSON_THROW(out_of_range::create(401, "array index " + std::to_string(idx) + " is out of range", *this));
            }
        }
        else
        {
            JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name()), *this));
        }
    }


### Compiler and operating system

VS2022 MSVC 17.1.0

### Library version

JSON for Modern C++ version 3.10.5

### Validation

- [X] The bug also occurs if the latest version from the [`develop`](https://github.com/nlohmann/json/tree/develop) branch is used.
- [X] I can successfully [compile and run the unit tests](https://github.com/nlohmann/json#execute-unit-tests).
@nlohmann
Copy link
Owner

nlohmann commented Apr 5, 2022

@nlohmann nlohmann removed the kind: bug label Apr 5, 2022
@skychips
Copy link
Author

skychips commented Apr 5, 2022

See https://json.nlohmann.me/home/faq/#brace-initialization-yields-arrays

Is there a perfect way to circumvent this problem?

Please let me know, thank you.

@nlohmann
Copy link
Owner

nlohmann commented Apr 5, 2022

Don't use brace initialization for the library.

@skychips
Copy link
Author

skychips commented Apr 5, 2022

Don't use brace initialization for the library.

Ok, thanks, I ran into a different problem.

The exception that is thrown, I can't receive the string information when I receive it.

My guess: most likely the std::string string is freed

@skychips
Copy link
Author

skychips commented Apr 5, 2022

The reason: turning on the _HAS_EXCEPTIONS macro definition will cause this phenomenon

@skychips skychips closed this as completed Apr 5, 2022
@nlohmann nlohmann added the solution: duplicate the issue is a duplicate; refer to the linked issue instead label Jul 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution: duplicate the issue is a duplicate; refer to the linked issue instead
Projects
None yet
Development

No branches or pull requests

2 participants