From 4acfa9984fb7b8d2cba2a054351f9d1b194dd33d Mon Sep 17 00:00:00 2001 From: Daniele Lacamera Date: Fri, 31 Jul 2026 15:24:43 +0200 Subject: [PATCH] sp_int: remove tautological err check in sp_todecimal Premise: sp_int.c:18887 `if (err == MP_OKAY) {` opens the block that dominates the claimed condition. The last thing that can set err is ALLOC_SP_INT_SIZE at :18883, above that guard. Claim: sp_int.c:18909, a second `if (err == MP_OKAY)` nested directly inside the first. Proof: control reaches :18909 only through the taken branch of :18887, so err == MP_OKAY there. Between the two, err is neither assigned nor passed by address: the span holds character stores into str and `(void)sp_div_d(t, 10, t, &d)`, whose return value is explicitly discarded. The inner test is therefore a tautology and its false branch is unreachable. Scope: the only preprocessor construct in the span is the WOLFSSL_SP_INT_NEGATIVE sign-character block (:18888-18895); neither arm writes err. Evidence: llvm-cov MC/DC records this decision as never taking its false branch; it is one of the two structural residuals noted for sp_int.c in the sp-math baseline. The remaining err checks in the function are live: ALLOC_SP_INT_SIZE writes err (MP_VAL when the size exceeds SP_INT_DIGITS, MP_MEM in the malloc form) in both its small-stack and static-stack expansions. Compiler cross-check: gcc -O2 emits byte-identical code for this file before and after this commit -- the optimiser had already folded the removed condition, independently confirming it was dead. --- wolfcrypt/src/sp_int.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index 4400d6f845..c4d9712fef 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -18906,13 +18906,11 @@ int sp_todecimal(const sp_int* a, char* str) /* Terminate string. */ str[i] = '\0'; - if (err == MP_OKAY) { - /* Reverse string to big endian. */ - for (j = 0; j <= (i - 1) / 2; j++) { - int c = (unsigned char)str[j]; - str[j] = str[i - 1 - j]; - str[i - 1 - j] = (char)c; - } + /* Reverse string to big endian. */ + for (j = 0; j <= (i - 1) / 2; j++) { + int c = (unsigned char)str[j]; + str[j] = str[i - 1 - j]; + str[i - 1 - j] = (char)c; } }