diff --git a/src/SomfyController.ino b/src/SomfyController.ino index 4b5411d..29af53e 100644 --- a/src/SomfyController.ino +++ b/src/SomfyController.ino @@ -86,7 +86,11 @@ void setup() { #endif // The backtrace only decodes against the firmware.elf of the build that // crashed, so say whether that is the one running now. - const char *crashedSha = (const char *)summary.app_elf_sha256; + // app_elf_sha256 isn't always terminated, so copy it before comparing or + // printing it. + char crashedSha[APP_ELF_SHA256_SZ]; + static_assert(sizeof(crashedSha) <= sizeof(summary.app_elf_sha256), "copy would read past app_elf_sha256"); + SETCHARPROP(crashedSha, (const char *)summary.app_elf_sha256, sizeof(crashedSha)); char runningSha[APP_ELF_SHA256_SZ] = {0}; getRunningElfSha(runningSha, sizeof(runningSha)); if(strcmp(crashedSha, runningSha) == 0) diff --git a/test/test_utils/test_utils.cpp b/test/test_utils/test_utils.cpp index ebde1d5..5d17801 100644 --- a/test/test_utils/test_utils.cpp +++ b/test/test_utils/test_utils.cpp @@ -79,6 +79,15 @@ static void test_setcharprop_truncates_long_value() { SETCHARPROP(prop, "abcdefgh", sizeof(prop)); TEST_ASSERT_EQUAL_STRING("abcd", prop); } +static void test_setcharprop_unterminated_source() { + // A coredump's app_elf_sha256 can be full of hex digits with no terminator. + // The copy must stop inside the array and come out as a comparable string. + const char crashed[17] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','9'}; + char sha[sizeof(crashed)]; + SETCHARPROP(sha, crashed, sizeof(sha)); + TEST_ASSERT_EQUAL_STRING("0123456789abcdef", sha); + TEST_ASSERT_EQUAL_INT(0, strcmp(sha, "0123456789abcdef")); +} /********************************************************************* * sortArray / sortArrayReverse @@ -202,6 +211,7 @@ int main(int argc, char **argv) { RUN_TEST(test_toBoolean_unknown_uses_default); RUN_TEST(test_setcharprop_copies_short_value); RUN_TEST(test_setcharprop_truncates_long_value); + RUN_TEST(test_setcharprop_unterminated_source); RUN_TEST(test_sortArray_ascending); RUN_TEST(test_sortArrayReverse_descending); RUN_TEST(test_sortArray_custom_comparator);