Repository: mesos Updated Branches: refs/heads/master 67825e8e6 -> e3a825a5c
Added validation logic for UUID's. This change adds validation logic to `UUID::fromBytes()` for uuids. The modified return type of this function is now `Try<UUID>`. Review: https://reviews.apache.org/r/48613/ Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/4912a714 Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/4912a714 Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/4912a714 Branch: refs/heads/master Commit: 4912a714f18f77db50a92c7871303bd7daaf3cc6 Parents: 67825e8 Author: Deshna Jain <deshna...@gmail.com> Authored: Mon Jun 20 15:46:56 2016 -0700 Committer: Vinod Kone <vinodk...@gmail.com> Committed: Mon Jun 20 15:46:56 2016 -0700 ---------------------------------------------------------------------- 3rdparty/stout/include/stout/uuid.hpp | 15 ++++++++++++++- 3rdparty/stout/tests/uuid_tests.cpp | 12 +++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/4912a714/3rdparty/stout/include/stout/uuid.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/uuid.hpp b/3rdparty/stout/include/stout/uuid.hpp index cde3bdb..a57896c 100644 --- a/3rdparty/stout/include/stout/uuid.hpp +++ b/3rdparty/stout/include/stout/uuid.hpp @@ -22,7 +22,9 @@ #include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_io.hpp> +#include <stout/error.hpp> #include <stout/thread_local.hpp> +#include <stout/try.hpp> #ifdef __WINDOWS__ #include <stout/windows.hpp> @@ -49,10 +51,21 @@ public: return UUID((*generator)()); } - static UUID fromBytes(const std::string& s) + static Try<UUID> fromBytes(const std::string& s) { + const std::string error = "Not a valid UUID"; + + if (s.size() != UUID::static_size()) { + return Error(error); + } + boost::uuids::uuid uuid; memcpy(&uuid, s.data(), s.size()); + + if (uuid.version() == UUID::version_unknown) { + return Error(error); + } + return UUID(uuid); } http://git-wip-us.apache.org/repos/asf/mesos/blob/4912a714/3rdparty/stout/tests/uuid_tests.cpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/tests/uuid_tests.cpp b/3rdparty/stout/tests/uuid_tests.cpp index 50295ad..b061b82 100644 --- a/3rdparty/stout/tests/uuid_tests.cpp +++ b/3rdparty/stout/tests/uuid_tests.cpp @@ -16,6 +16,8 @@ #include <gmock/gmock.h> +#include <stout/check.hpp> +#include <stout/gtest.hpp> #include <stout/uuid.hpp> using std::string; @@ -24,7 +26,7 @@ using std::string; TEST(UUIDTest, Test) { UUID uuid1 = UUID::random(); - UUID uuid2 = UUID::fromBytes(uuid1.toBytes()); + UUID uuid2 = UUID::fromBytes(uuid1.toBytes()).get(); UUID uuid3 = uuid2; EXPECT_EQ(uuid1, uuid2); @@ -47,3 +49,11 @@ TEST(UUIDTest, Test) EXPECT_EQ(string2, string3); EXPECT_EQ(string1, string3); } + + +TEST(UUIDTest, MalformedUUID) +{ + EXPECT_SOME(UUID::fromBytes(UUID::random().toBytes())); + EXPECT_ERROR(UUID::fromBytes("malformed-uuid")); + EXPECT_ERROR(UUID::fromBytes("invalidstringmsg")); +}