Skip to content

Commit 0c22163

Browse files
committed
Fix unaligned reads during metadata search
1 parent 5ca10f3 commit 0c22163

File tree

1 file changed

+37
-30
lines changed

1 file changed

+37
-30
lines changed

wled00/wled_metadata.cpp

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,40 +80,47 @@ const __FlashStringHelper* brandString = FPSTR(brandString_s);
8080
* @return true if structure was found and extracted, false otherwise
8181
*/
8282
bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_metadata_t* extractedDesc) {
83-
if (!binaryData || !extractedDesc || dataSize < sizeof(wled_metadata_t)) {
84-
return false;
85-
}
83+
if (!binaryData || !extractedDesc || dataSize < sizeof(wled_metadata_t)) {
84+
return false;
85+
}
8686

87-
for (size_t offset = 0; offset <= dataSize - sizeof(wled_metadata_t); offset++) {
88-
const wled_metadata_t* custom_desc = (const wled_metadata_t*)(binaryData + offset);
87+
for (size_t offset = 0; offset <= dataSize - sizeof(wled_metadata_t); offset++) {
88+
if ((binaryData[offset]) == static_cast<char>(WLED_CUSTOM_DESC_MAGIC)) {
89+
// First byte matched; check next in an alignment-safe way
90+
uint32_t data_magic;
91+
memcpy(&data_magic, binaryData + offset, sizeof(data_magic));
92+
93+
// Check for magic number
94+
if (data_magic == WLED_CUSTOM_DESC_MAGIC) {
95+
wled_metadata_t candidate;
96+
memcpy(&candidate, binaryData + offset, sizeof(candidate));
97+
98+
// Found potential match, validate version
99+
if (candidate.desc_version != WLED_CUSTOM_DESC_VERSION) {
100+
DEBUG_PRINTF_P(PSTR("Found WLED structure at offset %u but version mismatch: %u\n"),
101+
offset, candidate.desc_version);
102+
continue;
103+
}
89104

90-
// Check for magic number
91-
if (custom_desc->magic == WLED_CUSTOM_DESC_MAGIC) {
92-
// Found potential match, validate version
93-
if (custom_desc->desc_version != WLED_CUSTOM_DESC_VERSION) {
94-
DEBUG_PRINTF_P(PSTR("Found WLED structure at offset %u but version mismatch: %u\n"),
95-
offset, custom_desc->desc_version);
96-
continue;
97-
}
98-
99-
// Validate hash using runtime function
100-
uint32_t expected_hash = djb2_hash_runtime(custom_desc->release_name);
101-
if (custom_desc->hash != expected_hash) {
102-
DEBUG_PRINTF_P(PSTR("Found WLED structure at offset %u but hash mismatch\n"), offset);
103-
continue;
104-
}
105-
106-
// Valid structure found - copy entire structure
107-
memcpy(extractedDesc, custom_desc, sizeof(wled_metadata_t));
108-
109-
DEBUG_PRINTF_P(PSTR("Extracted WLED structure at offset %u: '%s'\n"),
110-
offset, extractedDesc->release_name);
111-
return true;
105+
// Validate hash using runtime function
106+
uint32_t expected_hash = djb2_hash_runtime(candidate.release_name);
107+
if (candidate.hash != expected_hash) {
108+
DEBUG_PRINTF_P(PSTR("Found WLED structure at offset %u but hash mismatch\n"), offset);
109+
continue;
112110
}
111+
112+
// Valid structure found - copy entire structure
113+
*extractedDesc = candidate;
114+
115+
DEBUG_PRINTF_P(PSTR("Extracted WLED structure at offset %u: '%s'\n"),
116+
offset, extractedDesc->release_name);
117+
return true;
118+
}
113119
}
114-
115-
DEBUG_PRINTLN(F("No WLED custom description found in binary"));
116-
return false;
120+
}
121+
122+
DEBUG_PRINTLN(F("No WLED custom description found in binary"));
123+
return false;
117124
}
118125

119126

0 commit comments

Comments
 (0)