From 8ec31dda71f867566a09c04704050626f243e629 Mon Sep 17 00:00:00 2001 From: agessaman Date: Sat, 3 Oct 2026 15:52:21 -0700 Subject: [PATCH] fix(ota): skip the bare compat search literal when scanning an image Every image also holds "ota-compat:" with no value (the search string); finding it first made every channel switch look untagged. --- src/helpers/OtaChannel.h | 4 +++- test/test_ota_channel/test_ota_channel.cpp | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/helpers/OtaChannel.h b/src/helpers/OtaChannel.h index 974cc4ba..4c3b81b5 100644 --- a/src/helpers/OtaChannel.h +++ b/src/helpers/OtaChannel.h @@ -116,11 +116,13 @@ static inline bool ota_compat_parse(const char* s, OtaCompat* out) { } // Find the tag in an image chunk; returns the NUL-terminated value text, or nullptr when -// the chunk holds no complete tag. +// the chunk holds no complete tag. Skips the bare OTA_COMPAT_TAG search literal, which +// every image also contains. static inline const char* ota_compat_find(const uint8_t* buf, size_t len) { const size_t tag_len = sizeof(OTA_COMPAT_TAG) - 1; for (size_t i = 0; i + tag_len < len; i++) { if (memcmp(buf + i, OTA_COMPAT_TAG, tag_len) != 0) continue; + if (buf[i + tag_len] < '0' || buf[i + tag_len] > '9') continue; if (memchr(buf + i + tag_len, 0, len - i - tag_len)) return (const char*)buf + i + tag_len; } return nullptr; diff --git a/test/test_ota_channel/test_ota_channel.cpp b/test/test_ota_channel/test_ota_channel.cpp index 75ce673d..ce6d0251 100644 --- a/test/test_ota_channel/test_ota_channel.cpp +++ b/test/test_ota_channel/test_ota_channel.cpp @@ -62,6 +62,10 @@ TEST(OtaCompat, FindsCompleteTagOnly) { EXPECT_STREQ(v, "2+eth"); const char cut[] = "junk ota-compat:2+e"; // value runs past the chunk end EXPECT_EQ(ota_compat_find((const uint8_t*)cut, sizeof(cut) - 1), nullptr); + const char literal_first[] = "ota-compat:\0code\0ota-compat:1\0"; // search literal precedes the tag + v = ota_compat_find((const uint8_t*)literal_first, sizeof(literal_first)); + ASSERT_NE(v, nullptr); + EXPECT_STREQ(v, "1"); const char none[] = "ota-compat"; EXPECT_EQ(ota_compat_find((const uint8_t*)none, sizeof(none)), nullptr); }