diff --git a/src/target/cortexa.c b/src/target/cortexa.c index fa231a6..613b28a 100644 --- a/src/target/cortexa.c +++ b/src/target/cortexa.c @@ -243,7 +243,11 @@ static_assert(ARRAY_SIZE(cortex_a_spr_types) == ARRAY_SIZE(cortex_a_spr_names), // Returns the amount of characters written to the buffer. static size_t create_tdesc_cortex_a(char *buffer, size_t max_len) { - size_t total = 0; + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not be able to fail -- and if it does, + // then there's nothing we can do about it, so we'll repatedly cast this variable to a size_t + // when calculating printsz (see below). + int total = 0; // We can't just repeatedly pass max_len to snprintf, because we keep changing the start // of buffer (effectively changing its size), so we have to repeatedly compute the size @@ -271,9 +275,9 @@ static size_t create_tdesc_cortex_a(char *buffer, size_t max_len) for (uint8_t i = 0; i <= 12; ++i) { if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; - total += snprintf(buffer + total, printsz, "", i); + total += snprintf(buffer + total, printsz, "", i); } // The special purpose registers are a slightly more complicated. @@ -285,7 +289,7 @@ static size_t create_tdesc_cortex_a(char *buffer, size_t max_len) gdb_reg_type_e type = cortex_a_spr_types[i]; if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, "", cortex_a_spr_names[i], @@ -294,7 +298,7 @@ static size_t create_tdesc_cortex_a(char *buffer, size_t max_len) } if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; // Now onto the floating point registers. // The first register is unique; the rest all follow the same format. @@ -307,20 +311,24 @@ static size_t create_tdesc_cortex_a(char *buffer, size_t max_len) // Now onto the simple ones. for (uint8_t i = 0; i <= 15; ++i) { if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, - "", + "", i ); } if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, ""); - return total; + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not ever be able to fail -- and if it + // does, then there's nothing we can do about it, so we'll just discard the signedness + // of total when we return it. + return (size_t) total; } diff --git a/src/target/cortexm.c b/src/target/cortexm.c index fda1699..3181f2a 100644 --- a/src/target/cortexm.c +++ b/src/target/cortexm.c @@ -265,7 +265,11 @@ static_assert(ARRAY_SIZE(cortex_m_spr_bitsizes) == ARRAY_SIZE(cortex_m_spr_names */ static size_t create_tdesc_cortex_m(char *buffer, size_t max_len) { - size_t total = 0; + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not be able to fail -- and if it does, + // then there's nothing we can do about it, so we'll repatedly cast this variable to a size_t + // when calculating printsz (see below). + int total = 0; // We can't just repeatedly pass max_len to snprintf, because we keep changing the start // of buffer (effectively changing its size), so we have to repeatedly compute the size @@ -292,9 +296,9 @@ static size_t create_tdesc_cortex_m(char *buffer, size_t max_len) for (uint8_t i = 0; i <= 12; ++i) { if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; - total += snprintf(buffer + total, printsz, "", i); + total += snprintf(buffer + total, printsz, "", i); } // Now for sp, lr, pc, xpsr, msp, psp, primask, basepri, faultmask, and control. @@ -304,12 +308,12 @@ static size_t create_tdesc_cortex_m(char *buffer, size_t max_len) for (size_t i = 0; i < ARRAY_SIZE(cortex_m_spr_names); ++i) { if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; gdb_reg_type_e type = cortex_m_spr_types[i]; gdb_reg_save_restore_e save_restore = cortex_m_spr_save_restores[i]; - total += snprintf(buffer + total, printsz, "", + total += snprintf(buffer + total, printsz, "", cortex_m_spr_names[i], cortex_m_spr_bitsizes[i], gdb_reg_save_restore_strings[save_restore], @@ -318,11 +322,15 @@ static size_t create_tdesc_cortex_m(char *buffer, size_t max_len) } if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, ""); - return total; + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not ever be able to fail -- and if it + // does, then there's nothing we can do about it, so we'll just discard the signedness + // of total when we return it. + return (size_t) total; } // Creates the target description XML string for a Cortex-MF. Like snprintf(), this function @@ -387,9 +395,18 @@ static size_t create_tdesc_cortex_m(char *buffer, size_t max_len) */ static size_t create_tdesc_cortex_mf(char *buffer, size_t max_len) { + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not be able to fail -- and if it does, + // then there's not really anything we can do about it, so we repatedly cast this variable + // to a size_t when calculating printsz (see below). Likewise, create_tdesc_cortex_m() + // has static inputs and shouldn't ever return a value large enough for casting it to a + // signed int to change its value, and if it does, then again there's something wrong that + // we can't really do anything about. + int total = 0; + // The first part of the target description for the Cortex-MF is identical to the Cortex-M // target description. - size_t total = create_tdesc_cortex_m(buffer, max_len); + total = (int) create_tdesc_cortex_m(buffer, max_len); // We can't just repeatedly pass max_len to snprintf, because we keep changing the start // of buffer (effectively changing its size), so we have to repeatedly compute the size @@ -403,7 +420,7 @@ static size_t create_tdesc_cortex_mf(char *buffer, size_t max_len) // Minor hack: subtract the target closing tag, since we have a bit more to add. total -= strlen(""); - printsz = max_len - total; + printsz = max_len - (size_t) total; } @@ -416,17 +433,21 @@ static size_t create_tdesc_cortex_mf(char *buffer, size_t max_len) for (uint8_t i = 0; i <= 15; ++i) { if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, "", i); } if (max_len != 0) - printsz = max_len - total; + printsz = max_len - (size_t) total; total += snprintf(buffer + total, printsz, ""); - return total; + // Minor hack: technically snprintf returns an int for possibility of error, but in this case + // these functions are given static input that should not ever be able to fail -- and if it + // does, then there's nothing we can do about it, so we'll just discard the signedness + // of total when we return it. + return (size_t) total; }