ld testsuite: Fix integer overflow in init-mixed.c
Alan Modra commits a fix to an ld testsuite file, renaming a variable and using smaller values to avoid integer overflows.
This discussion resolves a potential integer overflow issue in an ld testsuite file. While the original patch proposed using int32_t, the maintainer opted for a different fix: renaming the count variable to order and using small values (0-6) to prevent overflow, even on targets with 16-bit integers. This change accounts for prior commits that enabled testing on such targets.
- proposer
Submitted a patch to fix an integer overflow in a testsuite file by using `int32_t` to ensure correct behavior across different `int` sizes.
“The code assumes that int is 32 bits wide (see e.g. line 97), which is not necessarily true.”
- maintainer
Accepted a different fix by renaming the variable to `order` and using small values to avoid overflow on 16-bit `int` targets, aligning with past commits.
“Fair enough, but I'm going to commit the following instead. The variable isn't a counter so rename it, and use small values that won't overflow a target with 16-bit int.”
Technical Tradeoffs
- Choosing between using a wider integer type (`int32_t`) to directly accommodate larger values and resizing the variable and its test values to be safe across different `int` widths.
In Details
ld/testsuite/ld-elf/init-mixed.c tests the interaction of sections like .init_array and .fini_array across different object files. The fix addresses an integer overflow in a variable used to check the initialization/finalization order, ensuring correctness on platforms where int might be smaller than 32 bits, a scenario enabled by a prior commit.
- ld
- The GNU linker, responsible for combining object files into executables or libraries.
- testsuite
- A collection of tests designed to verify the correctness of the software.
- integer overflow
- A condition that occurs when an arithmetic operation produces a result that exceeds the maximum representable value for the given integer type.
- .init_array and .fini_array
- ELF sections used to store pointers to C++ global constructors and destructors, respectively, executed before and after main().