Skip to content

Commit

Permalink
Fix Uuid comparison for PROGMEM usage (#2567)
Browse files Browse the repository at this point in the history
even memcmp_P goes pop sometimes.
To be safe, compare explicitly as 4 words.
  • Loading branch information
mikee47 authored Oct 17, 2022
1 parent cacef01 commit 86aec1f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
11 changes: 11 additions & 0 deletions Sming/Core/Data/Uuid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ uint32_t os_random();
void os_get_random(void* buf, size_t n);
}

bool Uuid::operator==(const Uuid& other) const
{
// Ensure these are stricly compared as a set of words to avoid PROGMEM issues
struct S {
uint32_t a, b, c, d;
};
auto& s1 = reinterpret_cast<const S&>(*this);
auto& s2 = reinterpret_cast<const S&>(other);
return s1.a == s2.a && s1.b == s2.b && s1.c == s2.c && s1.d == s2.d;
}

bool Uuid::generate(MacAddress mac)
{
uint8_t version = 1; // DCE version
Expand Down
10 changes: 3 additions & 7 deletions Sming/Core/Data/Uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ struct Uuid {
*/
static constexpr size_t stringSize = 36;

Uuid()
constexpr Uuid()
{
}

Expand Down Expand Up @@ -69,14 +69,10 @@ struct Uuid {

explicit operator bool() const
{
Uuid Null{};
return memcmp(this, &Null, sizeof(Null)) != 0;
return *this != Uuid{};
}

bool operator==(const Uuid& other) const
{
return memcmp(this, &other, sizeof(Uuid)) == 0;
}
bool operator==(const Uuid& other) const;

bool operator!=(const Uuid& other) const
{
Expand Down

0 comments on commit 86aec1f

Please sign in to comment.