diff --git a/CHANGES.md b/CHANGES.md index c780a8b..646004e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,12 @@ # redsea changelog +## 1.0.1 + +* Fixes: + * Fix a crash (uncaught json exception) when attempting to serialize a string that's + invalid UTF-8, e.g. if long PS gets corrupted + * Fix build when the installed version of nlohmann-json is too old (#113) + ## 1.0 (2024-06-27) * New features: diff --git a/meson.build b/meson.build index b985ae5..57e8dcc 100644 --- a/meson.build +++ b/meson.build @@ -1,5 +1,5 @@ project('redsea', 'cpp', default_options : ['warning_level=3', 'buildtype=release', 'optimization=3'], - version: '1.0') + version: '1.0.1') # Store version number to be compiled in conf = configuration_data() diff --git a/src/groups.cc b/src/groups.cc index 9548be0..8502b33 100644 --- a/src/groups.cc +++ b/src/groups.cc @@ -312,7 +312,18 @@ void Station::updateAndPrint(const Group& group, std::ostream& stream) { } } - stream << json_ << std::endl << std::flush; + try { + // nlohmann::operator<< throws if a string contains non-UTF8 data. + // It's better to throw while writing to a stringstream; otherwise + // incomplete JSON objects could get printed. + std::stringstream output_proxy_stream; + output_proxy_stream << json_; + stream << output_proxy_stream.str() << std::endl << std::flush; + } catch (const std::exception& e) { + nlohmann::ordered_json json_from_exception; + json_from_exception["debug"] = std::string(e.what()); + stream << json_from_exception << std::endl << std::flush; + } } uint16_t Station::getPI() const { diff --git a/test/unit.cc b/test/unit.cc index 5a6954d..4a8df04 100644 --- a/test/unit.cc +++ b/test/unit.cc @@ -449,3 +449,15 @@ TEST_CASE("Error detection and correction") { CHECK(groups.back().get(redsea::BLOCK1) == 0x0000); // "----" } } + +TEST_CASE("Invalid data") { + redsea::Options options; + + SECTION("Invalid UTF-8 is handled cleanly") { + REQUIRE_NOTHROW(hex2json({ + 0xE24D'4401'D02F'1942, + 0xE24D'E400'E24D'0000, + 0xE24D'F400'E20D'FC20 + }, options, 0xE24D)); + } +}