Represent invalid UUIDs as UUIDs with length zero

Summary:
During the previous attempt to generalize the UUID class, it was
suggested that we represent invalid UUIDs as length zero (previously, we
used an all-zero UUID for that). This meant that some valid build-ids
could not be represented (it's possible however unlikely that a checksum of
some file would be zero) and complicated adding support for variable
length build-ids (should a 16-byte empty UUID compare equal to a 20-byte
empty UUID?).

This patch resolves these issues by introducing a canonical
representation for an invalid UUID. The slight complication here is that
some clients (MachO) actually use the all-zero notation to mean "no UUID
has been set". To keep this use case working (while making it very
explicit about which construction semantices are wanted), replaced the
UUID constructors and the SetBytes functions with named factory methods.
- "fromData" creates a UUID from the given data, and it treats all bytes
  equally.
- "fromOptionalData" first checks the data contents - if all bytes are
  zero, it treats this as an invalid/empty UUID.

Reviewers: clayborg, sas, lemo, davide, espindola

Subscribers: emaste, lldb-commits, arichardson

Differential Revision: https://reviews.llvm.org/D48479

llvm-svn: 335612
This commit is contained in:
Pavel Labath
2018-06-26 15:12:20 +00:00
parent 9f199ebec0
commit 2f93fd1f50
12 changed files with 111 additions and 144 deletions

View File

@@ -21,29 +21,12 @@
using namespace lldb_private;
UUID::UUID() { Clear(); }
UUID::UUID(llvm::ArrayRef<uint8_t> bytes) {
if (bytes.size() != 20 && bytes.size() != 16)
bytes = {};
UUID::UUID(const UUID &rhs) {
SetBytes(rhs.m_uuid, rhs.m_num_uuid_bytes);
}
UUID::UUID(const void *uuid_bytes, uint32_t num_uuid_bytes) {
SetBytes(uuid_bytes, num_uuid_bytes);
}
const UUID &UUID::operator=(const UUID &rhs) {
if (this != &rhs) {
m_num_uuid_bytes = rhs.m_num_uuid_bytes;
::memcpy(m_uuid, rhs.m_uuid, sizeof(m_uuid));
}
return *this;
}
UUID::~UUID() {}
void UUID::Clear() {
m_num_uuid_bytes = 16;
::memset(m_uuid, 0, sizeof(m_uuid));
m_num_uuid_bytes = bytes.size();
std::memcpy(m_uuid, bytes.data(), bytes.size());
}
std::string UUID::GetAsString(const char *separator) const {
@@ -74,38 +57,6 @@ void UUID::Dump(Stream *s) const {
s->PutCString(GetAsString().c_str());
}
bool UUID::SetBytes(const void *uuid_bytes, uint32_t num_uuid_bytes) {
if (uuid_bytes) {
switch (num_uuid_bytes) {
case 20:
m_num_uuid_bytes = 20;
break;
case 16:
m_num_uuid_bytes = 16;
m_uuid[16] = m_uuid[17] = m_uuid[18] = m_uuid[19] = 0;
break;
default:
// Unsupported UUID byte size
m_num_uuid_bytes = 0;
break;
}
if (m_num_uuid_bytes > 0) {
::memcpy(m_uuid, uuid_bytes, m_num_uuid_bytes);
return true;
}
}
::memset(m_uuid, 0, sizeof(m_uuid));
return false;
}
bool UUID::IsValid() const {
return m_uuid[0] || m_uuid[1] || m_uuid[2] || m_uuid[3] || m_uuid[4] ||
m_uuid[5] || m_uuid[6] || m_uuid[7] || m_uuid[8] || m_uuid[9] ||
m_uuid[10] || m_uuid[11] || m_uuid[12] || m_uuid[13] || m_uuid[14] ||
m_uuid[15] || m_uuid[16] || m_uuid[17] || m_uuid[18] || m_uuid[19];
}
static inline int xdigit_to_int(char ch) {
ch = tolower(ch);
if (ch >= 'a' && ch <= 'f')
@@ -155,14 +106,15 @@ size_t UUID::SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes) {
// Skip leading whitespace characters
p = p.ltrim();
ValueType bytes;
uint32_t bytes_decoded = 0;
llvm::StringRef rest =
UUID::DecodeUUIDBytesFromString(p, m_uuid, bytes_decoded, num_uuid_bytes);
UUID::DecodeUUIDBytesFromString(p, bytes, bytes_decoded, num_uuid_bytes);
// If we successfully decoded a UUID, return the amount of characters that
// were consumed
if (bytes_decoded == num_uuid_bytes) {
m_num_uuid_bytes = num_uuid_bytes;
*this = fromData(bytes, bytes_decoded);
return str.size() - rest.size();
}