diff --git a/CMakeLists.txt b/CMakeLists.txt index 25f5cd644..324d111dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2497,6 +2497,13 @@ if (BUILD_TESTS) PRIVATE libfastfetch ) + add_executable(fastfetch-test-edid + tests/edid.c + ) + target_link_libraries(fastfetch-test-edid + PRIVATE libfastfetch + ) + enable_testing() add_test(NAME test-cache COMMAND fastfetch-test-cache) add_test(NAME test-logo COMMAND fastfetch-test-logo) @@ -2518,6 +2525,9 @@ if (BUILD_TESTS) add_test(NAME test-endian COMMAND fastfetch-test-endian) add_test(NAME test-endian-bigendian COMMAND fastfetch-test-endian-bigendian) add_test(NAME test-gpu-pci COMMAND fastfetch-test-gpu-pci) + add_test(NAME test-edid COMMAND fastfetch-test-edid) + # A regression in the EDID parser can turn the scan into an infinite loop. + set_tests_properties(test-edid PROPERTIES TIMEOUT 30) endif() ################## diff --git a/src/common/impl/edidHelper.c b/src/common/impl/edidHelper.c index 4b8f4f84d..b1a973e36 100644 --- a/src/common/impl/edidHelper.c +++ b/src/common/impl/edidHelper.c @@ -122,10 +122,12 @@ bool ffEdidGetSerial(const uint8_t edid[128], FFstrbuf* result) { } bool ffEdidGetHdrCompatible(const uint8_t* edid, uint32_t length) { - if (length <= 128) { + if (length < 256) { return false; } - for (const uint8_t* cta = &edid[128]; cta < &edid[length]; cta += 128) { + // Only complete 128-byte extension blocks may be inspected. + for (uint32_t base = 128; base <= length - 128; base += 128) { + const uint8_t* cta = edid + base; // https://en.wikipedia.org/wiki/Extended_Display_Identification_Data#CTA_EDID_Timing_Extension_Block if (cta[0] != 0x02 /* CTA EDID */) { continue; @@ -137,7 +139,9 @@ bool ffEdidGetHdrCompatible(const uint8_t* edid, uint32_t length) { if (offset <= 4) { continue; } - for (uint8_t i = 4; i < offset;) { + // Byte 2 is an offset inside this block; byte 127 is the checksum. + const uint32_t end = offset > 127 ? 127 : offset; + for (uint32_t i = 4; i + 1 < end;) { uint8_t blkLen = cta[i] & 0x1f; if (blkLen > 0) { uint8_t blkTag = (cta[i] & 0xe0) >> 5; @@ -148,7 +152,7 @@ bool ffEdidGetHdrCompatible(const uint8_t* edid, uint32_t length) { } } } - i += (uint8_t) (blkLen + 1); + i += (uint32_t) blkLen + 1; } } return false; diff --git a/tests/edid.c b/tests/edid.c new file mode 100644 index 000000000..75d3c5904 --- /dev/null +++ b/tests/edid.c @@ -0,0 +1,173 @@ +#include "common/edidHelper.h" +#include "common/textModifier.h" + +#include +#include +#include +#include +#include + +static void verify(bool expression, const char* expressionStr, int lineNo) { + if (expression) { + return; + } + + fprintf(stderr, FASTFETCH_TEXT_MODIFIER_ERROR "[%d] %s\n" FASTFETCH_TEXT_MODIFIER_RESET, lineNo, expressionStr); + exit(1); +} + +#define VERIFY(expression) verify((expression), #expression, __LINE__) + +// Build a zeroed EDID with a valid base block. The buffer is allocated with the +// exact EDID length, so an ASan build also catches reads past the end of it. +static uint8_t* newEdid(uint32_t length) { + uint8_t* edid = (uint8_t*) calloc(length, 1); + VERIFY(edid != NULL); + + static const uint8_t header[] = { 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00 }; + memcpy(edid, header, sizeof(header)); + edid[126] = (uint8_t) (length / 128 - 1); + uint8_t sum = 0; + for (uint32_t i = 0; i < 127; ++i) { + sum += edid[i]; + } + edid[127] = (uint8_t) (0u - sum); + return edid; +} + +static uint8_t* addCta(uint8_t* edid, uint32_t base, uint8_t revision, uint8_t dtdOffset) { + uint8_t* cta = edid + base; + cta[0] = 0x02; + cta[1] = revision; + cta[2] = dtdOffset; + return cta; +} + +static void testNoExtension(void) { + uint8_t* edid = newEdid(128); + VERIFY(!ffEdidGetHdrCompatible(edid, 128)); + free(edid); +} + +static void testPartialBlocks(void) { + // A trailing block shorter than 128 bytes must be ignored entirely; even + // reading its tag / revision / DTD offset used to be out of bounds. + const uint32_t lengths[] = { 129, 130, 200, 255, 257, 300 }; + for (uint32_t i = 0; i < sizeof(lengths) / sizeof(lengths[0]); ++i) { + uint32_t length = lengths[i]; + uint8_t* edid = newEdid(length); + uint8_t* cta = edid + 128; + cta[0] = 0x02; + if (length >= 130) { + cta[1] = 0x03; + } + if (length >= 131) { + cta[2] = 0xFF; + } + VERIFY(!ffEdidGetHdrCompatible(edid, length)); + free(edid); + } +} + +static void testHdrDetection(void) { + uint8_t* edid = newEdid(256); + uint8_t* cta = addCta(edid, 128, 3, 7); + cta[4] = 0xE2; // extended tag, length 2 + cta[5] = 0x06; // HDR SMDB + cta[6] = 0x00; + VERIFY(ffEdidGetHdrCompatible(edid, 256)); + + cta[5] = 0x07; // HDR DMDB + VERIFY(ffEdidGetHdrCompatible(edid, 256)); + + cta[5] = 0x01; // unrelated extended tag + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + + cta[5] = 0x06; + cta[1] = 0x02; // data block collection requires CTA revision 3 + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + + cta[1] = 0x03; + cta[0] = 0x70; // not a CTA extension + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + free(edid); +} + +static void testMultipleExtensions(void) { + uint8_t* edid = newEdid(384); + edid[128] = 0x70; // some other extension type + uint8_t* cta = addCta(edid, 256, 3, 7); + cta[4] = 0xE2; + cta[5] = 0x06; + VERIFY(ffEdidGetHdrCompatible(edid, 384)); + free(edid); +} + +static void testEmptyDataBlockCollection(void) { + // CTA-861 version 3: d == 0 means neither DTDs nor data blocks are present, + // and d == 4 means the data block collection is empty. + uint8_t* edid = newEdid(256); + uint8_t* cta = addCta(edid, 128, 3, 0); + cta[4] = 0xE2; + cta[5] = 0x06; + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + + cta[2] = 4; + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + free(edid); +} + +static void testOutOfRangeOffset(void) { + // An out-of-range DTD offset must be clamped to the CTA block. A valid HDR + // block inside that block is still detected. + uint8_t* edid = newEdid(256); + uint8_t* cta = addCta(edid, 128, 3, 0xFF); + cta[4] = 0xE2; + cta[5] = 0x06; + VERIFY(ffEdidGetHdrCompatible(edid, 256)); + + // ... but bytes past the block must not be used as data blocks. These two + // bytes live in the next extension block, 44 bytes into it. + uint8_t* large = newEdid(512); + addCta(large, 128, 3, 0xFF); + large[300] = 0xE2; + large[301] = 0x06; + VERIFY(!ffEdidGetHdrCompatible(large, 512)); + free(large); + free(edid); +} + +static void testExtendedTagReadBounded(void) { + // A block header at i == d - 1 has no room for its extended tag inside the + // collection; byte d is the first DTD byte and must not be read as a tag. + uint8_t* edid = newEdid(256); + uint8_t* cta = addCta(edid, 128, 3, 10); + cta[9] = 0xE2; + cta[10] = 0x06; + VERIFY(!ffEdidGetHdrCompatible(edid, 256)); + free(edid); +} + +static void testNoCounterWrap(void) { + // The old loop counter was uint8_t: at i == 254 a block length of 1 wrapped + // it back to 0, making the scan loop forever. + uint8_t* edid = newEdid(512); + addCta(edid, 128, 3, 0xFF); + edid[382] = 0x01; // cta[254] of the first extension + VERIFY(!ffEdidGetHdrCompatible(edid, 512)); + free(edid); +} + +int main(void) { + testNoExtension(); + testPartialBlocks(); + testHdrDetection(); + testMultipleExtensions(); + testEmptyDataBlockCollection(); + testOutOfRangeOffset(); + testExtendedTagReadBounded(); + testNoCounterWrap(); + + puts("\033[32mAll tests passed!" FASTFETCH_TEXT_MODIFIER_RESET); + return 0; +}