Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Uncatchable exception + SIGABRT when serialising unmapped enum value #595

Open
2 tasks done
balgillo opened this issue Feb 14, 2025 · 1 comment
Open
2 tasks done

Comments

@balgillo
Copy link

balgillo commented Feb 14, 2025

When serialising an enum value not covered by JSONCONS_ENUM_NAME_TRAITS, I expect an exception to be thrown that the application can catch and then carry on. That works with a bare enum value, but not if the enum value is inside a struct. In that case, the program crashes with SIGABRT signal:

terminate called after throwing an instance of 'jsoncons::conv_error'
  what():  Not an enum: Unable to convert into the provided type
Aborted (core dumped)

This program reproduces the issue:

#include <iostream>

#include <jsoncons/json.hpp>

enum class MyEnum { A, B };

struct MyStruct {
    MyEnum x;
};

int main() {
    // Prints "Exception: Not an enum: Unable to convert into the provided type" and continues program
    try {
        jsoncons::encode_json(MyEnum::B, std::cout);
    } catch (const jsoncons::json_exception &e) {
        std::cout << "Exception: " << e.what() << std::endl;
    }

    /*
     * SIGABRT, exception is not caught.  Prints:
     * terminate called after throwing an instance of 'jsoncons::conv_error'
     *   what():  Not an enum: Unable to convert into the provided type
     * Aborted (core dumped)
     */
    try {
        jsoncons::encode_json(MyStruct {MyEnum::B}, std::cout);
    } catch (const jsoncons::conv_error &e) {
        std::cout << "Exception: " << e.what() << std::endl;
    } catch (const jsoncons::json_exception &e) {
        std::cout << "Exception: " << e.what() << std::endl;
    } catch (...) {
        std::cout << "Something else caught" << std::endl;
    }
    return 0;
}

JSONCONS_ALL_MEMBER_TRAITS(MyStruct, x);

JSONCONS_ENUM_NAME_TRAITS(MyEnum, (A, "A"));  // B is missing
  • Compiler: g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
  • Architecture: x64
  • Operating system: Ubuntu 24.04

What jsoncons library version?

  • Latest release 1.2.0
  • Other release 0.171.0
@balgillo balgillo added the Bug label Feb 14, 2025
danielaparker added a commit that referenced this issue Feb 14, 2025
danielaparker added a commit that referenced this issue Feb 14, 2025
danielaparker added a commit that referenced this issue Feb 14, 2025
danielaparker added a commit that referenced this issue Feb 14, 2025
@danielaparker
Copy link
Owner

The issue has been identified as resulting from an inappropriate noexcept specifier on a key_value constructor. This is fixed on master.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants