Files
scylladb/utils/uuid.cc
Kefu Chai 6b249dd301 utils: UUID: throw marshal_exception when fail to parse uuid
* throw marshal_exception if not the whole string is parsed, we
  should error out if the parsed string contains gabage at the end.
  before this change, we silent accept uuid like
  "ce84997b-6ea2-4468-9f02-8a65abf4wxyz", and parses it as
  "ce84997b-6ea2-4468-9f02-8a65abf4". this is not correct.
* throw marshal_exception if stoull() throws,
  `stoull()` throws if it fails to parse a string to an unsigned long
  long, we should translate the exception to `marshal_exception`, so
  we can handle these exception in a consistent manner.

test is updated accordingly.

Signed-off-by: Kefu Chai <kefu.chai@scylladb.com>

Closes #13069
2023-03-06 12:59:41 +02:00

66 lines
1.8 KiB
C++

/*
* Copyright (C) 2015-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include "UUID.hh"
#include <seastar/net/byteorder.hh>
#include <random>
#include <boost/iterator/function_input_iterator.hpp>
#include <boost/algorithm/string.hpp>
#include <string>
#include <seastar/core/sstring.hh>
#include "utils/serialization.hh"
#include "marshal_exception.hh"
namespace utils {
UUID
make_random_uuid() noexcept {
static thread_local std::mt19937_64 engine(std::random_device().operator()());
static thread_local std::uniform_int_distribution<uint64_t> dist;
uint64_t msb, lsb;
msb = dist(engine);
lsb = dist(engine);
msb &= ~uint64_t(0x0f << 12);
msb |= 0x4 << 12; // version 4
lsb &= ~(uint64_t(0x3) << 62);
lsb |= uint64_t(0x2) << 62; // IETF variant
return UUID(msb, lsb);
}
std::ostream& operator<<(std::ostream& out, const UUID& uuid) {
return out << uuid.to_sstring();
}
UUID::UUID(sstring_view uuid) {
sstring uuid_string(uuid.begin(), uuid.end());
boost::erase_all(uuid_string, "-");
auto size = uuid_string.size() / 2;
if (size != 16) {
throw marshal_exception(format("UUID string size mismatch: '{}'", uuid));
}
sstring most = sstring(uuid_string.begin(), uuid_string.begin() + size);
sstring least = sstring(uuid_string.begin() + size, uuid_string.end());
int base = 16;
try {
std::size_t pos = 0;
this->most_sig_bits = std::stoull(most, &pos, base);
if (pos != most.size()) {
throw std::invalid_argument("");
}
this->least_sig_bits = std::stoull(least, &pos, base);
if (pos != least.size()) {
throw std::invalid_argument("");
}
} catch (const std::logic_error&) {
throw marshal_exception(format("invalid UUID: '{}'", uuid));
}
}
}