From ec239933dbc510b8cf6b1466a5d49433a522e7ac Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Wed, 25 Apr 2018 10:43:26 +0900 Subject: [PATCH] Fixed #671. Added STR type check to define_map. --- erb/v1/cpp03_define_map.hpp.erb | 1 + .../v1/adaptor/detail/cpp03_define_map.hpp | 16 ++++++++++++ .../v1/adaptor/detail/cpp11_define_map.hpp | 1 + test/user_class.cpp | 25 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/erb/v1/cpp03_define_map.hpp.erb b/erb/v1/cpp03_define_map.hpp.erb index 1440406e..a7b01fa2 100644 --- a/erb/v1/cpp03_define_map.hpp.erb +++ b/erb/v1/cpp03_define_map.hpp.erb @@ -61,6 +61,7 @@ struct define_map, A<%=j%><%}%>> { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( diff --git a/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp b/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp index 69f347e0..7869b98e 100644 --- a/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp03_define_map.hpp @@ -62,6 +62,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -114,6 +115,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -180,6 +182,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -260,6 +263,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -354,6 +358,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -462,6 +467,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -584,6 +590,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -720,6 +727,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -870,6 +878,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1034,6 +1043,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1212,6 +1222,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1404,6 +1415,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1610,6 +1622,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -1830,6 +1843,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -2064,6 +2078,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( @@ -2312,6 +2327,7 @@ struct define_map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.insert( std::map::value_type( std::string( diff --git a/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp b/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp index 835e1d88..d1ab7015 100644 --- a/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp +++ b/include/msgpack/v1/adaptor/detail/cpp11_define_map.hpp @@ -72,6 +72,7 @@ struct define_map { if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); } std::map kvmap; for (uint32_t i = 0; i < o.via.map.size; ++i) { + if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); } kvmap.emplace( std::string( o.via.map.ptr[i].key.via.str.ptr, diff --git a/test/user_class.cpp b/test/user_class.cpp index e2ac9fc1..37bc09b2 100644 --- a/test/user_class.cpp +++ b/test/user_class.cpp @@ -601,3 +601,28 @@ TEST(MSGPACK_NVP, combination) EXPECT_EQ(d2.c, 3); EXPECT_EQ(d2.d, "ABC"); } + +struct invalid_key { + int val; + MSGPACK_DEFINE_MAP(val); +}; + +TEST(MSGPACK_USER_DEFINED, test_invalid_key_type) +{ + msgpack::sbuffer sbuf; + msgpack::packer pk(sbuf); + pk.pack_map(1); + pk.pack_int(42); + pk.pack_int(43); + msgpack::object_handle oh = msgpack::unpack(sbuf.data(), sbuf.size()); + try { + oh.get().as(); + EXPECT_TRUE(false); + } + catch (msgpack::type_error const&) { + EXPECT_TRUE(true); + } + catch (...) { + EXPECT_TRUE(false); + } +}