fix(mqtt): render the mbedtls diagnostic magnitude correctly

ESP-IDF stores the mbedTLS stack error as a positive magnitude (it captures
-ret), so negating it before printing produced "mbedtls:-0xFFFF8100" instead
of "mbedtls:-0x7F00" for the record-buffer allocation failure. %04X is a
minimum width, so nothing masked it.

Normalisation moves into MQTTReplyFormat.h as mbedtlsErrorMagnitude() rather
than staying inline in the bridge: inline is why this survived, since the
existing test passes the already-correct magnitude straight into replyAppendf
and never exercised the caller. It accepts either sign so a later SDK storing
the real negative code still renders, and widens to int64_t before negating
because negating INT32_MIN is undefined behaviour.

MQTTReplyFormat.h also gains the stdint.h it was always missing: it compiled
only because MQTTBridge.cpp pulls stdint in via other headers, and the host
test includes the header standalone.
This commit is contained in:
agessaman committed 2026-08-18 13:54:55 -07:00
1 parent cf14c0bb18
commit 25d035f80a
3 files changed
+34 -2

No files matched your search

@@ -88,6 +88,27 @@ TEST(ReplyAppendf, NoWritePastBufferAcrossOverflowingChain) {
EXPECT_EQ(c.buf()[c.logical - 1], '\0'); // still NUL-terminated
}
// The caller, not the helper, was where the mbedtls: line went wrong: production
// negated a value ESP-IDF already stores as a magnitude, so it rendered
// "mbedtls:-0xFFFF8100" instead of "-0x7F00". The chain test above never caught it
// because it passes the correct magnitude straight in.
TEST(MbedtlsErrorMagnitude, NormalisesEitherSignForTheNegativeHexConvention) {
EXPECT_EQ(mbedtlsErrorMagnitude(0x7F00), 0x7F00u); // ESP-IDF 4.4 stores +magnitude
EXPECT_EQ(mbedtlsErrorMagnitude(-0x7F00), 0x7F00u); // a later SDK may store the real code
EXPECT_EQ(mbedtlsErrorMagnitude(0), 0u);
char buf[32];
int pos = 0;
replyAppendf(buf, sizeof(buf), &pos, ", mbedtls:-0x%04X",
(unsigned)mbedtlsErrorMagnitude(0x7F00));
EXPECT_STREQ(buf, ", mbedtls:-0x7F00");
}
// Negating INT32_MIN is undefined behaviour, which is why the helper widens first.
TEST(MbedtlsErrorMagnitude, HandlesInt32MinWithoutOverflow) {
EXPECT_EQ(mbedtlsErrorMagnitude(INT32_MIN), 2147483648u);
}
TEST(ReplyAppendf, ExactFitBoundary) {
char buf[11]; // room for exactly "0123456789" + NUL
int pos = 0;