fix(tests): avoid unsigned underflow in DH group loop - #11050
Conversation
groups[] carries a trailing 0 sentinel so the array is never empty when
no HAVE_FFDHE_* is defined, and the loop bound subtracts it back off.
In that configuration the count is 1, so the bound folds to
size_t i < 1 - 1
i.e. an unsigned value compared against 0. GCC diagnoses this as
-Wtype-limits, and builds using -Werror (which configure enables for any
VCS checkout) fail:
tests/api/test_dh.c:569:19: error: comparison of unsigned expression
< 0 is always false [-Werror=type-limits]
Compare i + 1 against the element count instead. This never underflows
and is arithmetically identical for every array size; verified the
iteration count is unchanged for 0, 1, 2 and 3 configured groups.
Note the loop body is already dead in that configuration, so a build
with no FFDHE group enabled runs no DH group exchange at all.
There was a problem hiding this comment.
Pull request overview
This PR fixes a compile-time warning/error in the DH API test when no HAVE_FFDHE_* groups are enabled, by rewriting the loop bound to avoid an unsigned underflow that can trigger -Wtype-limits under -Werror.
Changes:
- Update the
groups[]iteration condition to usei + 1 < element_countinstead ofi < element_count - 1, preventing underflow when the array contains only the sentinel.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@danielinux — assigning you since this touches the loop from 2e1d38d ("tests: MC/DC decision coverage for dh.c and dsa.c"). The fix itself is mechanical, but there's a coverage question that's yours to call. The build break is just the unsigned underflow: with no The part worth your judgement: in that same configuration the loop body never executes, so Options, in your call:
I kept this PR to the one-line build fix rather than pre-empting that decision. Happy to fold in whichever you prefer. For context on urgency: this has been failing |
Fixes jenkins issue
#558— https://jenkins-supervisor.wolfssl.com/#/open-issues/558(root-cause task
#556: https://jenkins-supervisor.wolfssl.com/#/open-issues/556)Problem
test_wc_DhGenerateKeyPair_and_Agreebuilds agroups[]array from the enabledHAVE_FFDHE_*macros, with a trailing0sentinel so the array is never empty, and then subtracts that sentinel back off in the loop bound:When no
HAVE_FFDHE_*is defined the count is 1, so the bound constant-folds tosize_t i < 0— an unsigned value compared against zero. GCC flags it under-Wtype-limits, and since configure enables-Werrorfor any VCS checkout (m4/ax_harden_compiler_flags.m4), the build fails outright:This is not hypothetical — it has been breaking a nightly small-footprint build for 11 consecutive runs. The configuration that trips it enables
dhandrsawith all curves disabled, soHAVE_FFDHE_2048is never defined (configure.acgates it behind TLS 1.3 or supported-curves).Fix
Compare
i + 1against the element count rather thaniagainstcount - 1. The subtraction can no longer underflow.Verification
Arithmetically identical for every array size. Iteration counts measured before and after on gcc 9.4.0:
groups[]{0}{g,0}{g,g,0}{g,g,g,0}The pre-patch form fails to compile at size 1 under
-Werror -Wtype-limits; the patched form compiles clean and behaves identically everywhere else.Worth noting separately
The loop body is already dead in the no-FFDHE configuration — that build runs no DH group exchange at all, and passes. This PR only fixes the build break; whether such a configuration should skip the test loudly is a separate coverage question I'm happy to follow up on.