Skip to content

Uncatchable exception + SIGABRT when serialising unmapped enum value #595

@balgillo

Description

@balgillo

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions