diff --git a/components/core/src/clp/ffi/ir_stream/Serializer.cpp b/components/core/src/clp/ffi/ir_stream/Serializer.cpp index 379330373..09199e32d 100644 --- a/components/core/src/clp/ffi/ir_stream/Serializer.cpp +++ b/components/core/src/clp/ffi/ir_stream/Serializer.cpp @@ -550,6 +550,9 @@ auto Serializer::create( cVariableEncodingMethodsVersion ); if (optional_user_defined_metadata.has_value()) { + if (false == optional_user_defined_metadata.value().is_object()) { + return std::errc::protocol_not_supported; + } metadata.emplace( string{cProtocol::Metadata::UserDefinedMetadataKey}, std::move(optional_user_defined_metadata.value()) diff --git a/components/core/src/clp/ffi/ir_stream/Serializer.hpp b/components/core/src/clp/ffi/ir_stream/Serializer.hpp index 3906c4d6e..8fc40a77c 100644 --- a/components/core/src/clp/ffi/ir_stream/Serializer.hpp +++ b/components/core/src/clp/ffi/ir_stream/Serializer.hpp @@ -41,9 +41,11 @@ class Serializer { // Factory functions /** * Creates an IR serializer and serializes the stream's preamble. - * @param optional_user_defined_metadata Stream-level user-defined metadata. + * @param optional_user_defined_metadata Stream-level user-defined metadata, given as a JSON + * object. * @return A result containing the serializer or an error code indicating the failure: - * - std::errc::protocol_error on failure to serialize the preamble. + * - std::errc::protocol_error if failed to serialize the preamble. + * - std::errc::protocol_not_supported if the given user-defined metadata is not a JSON object. */ [[nodiscard]] static auto create( std::optional optional_user_defined_metadata = std::nullopt diff --git a/components/core/tests/test-ir_encoding_methods.cpp b/components/core/tests/test-ir_encoding_methods.cpp index 97786c92f..361f96c67 100644 --- a/components/core/tests/test-ir_encoding_methods.cpp +++ b/components/core/tests/test-ir_encoding_methods.cpp @@ -1494,3 +1494,23 @@ TEMPLATE_TEST_CASE( }; REQUIRE(assert_invalid_serialization(array_with_invalid_submap)); } + +// NOLINTNEXTLINE(readability-function-cognitive-complexity) +TEMPLATE_TEST_CASE( + "ffi_ir_stream_Serializer_serialize_invalid_user_defined_metadata", + "[clp][ffi][ir_stream][Serializer]", + four_byte_encoded_variable_t, + eight_byte_encoded_variable_t +) { + auto invalid_user_defined_metadata = GENERATE( + nlohmann::json(std::string{"str"}), + nlohmann::json(int{0}), + nlohmann::json(double{0.0}), + nlohmann::json(true), + nlohmann::json(nullptr), + nlohmann::json(vector{0, 1, 2}) + ); + auto const serializer_result{Serializer::create(invalid_user_defined_metadata)}; + REQUIRE(serializer_result.has_error()); + REQUIRE((std::errc::protocol_not_supported == serializer_result.error())); +}