Skip to content

Commit 2994b29

Browse files
keith-packardtomi-font
authored andcommitted
Avoid invalid gcc 14.3 warning about array bounds in mbedtls_xor
The combination of the multi-byte loop with the single byte loop confuses GCC 14.3's array bounds checker. When the loop size is constant, check to see if it is a multiple of the multi-byte size and bail early. As this will be evaluated at compile time, there should be no run-time cost. This change uses the __builtin_constant_p compile-time operation. To check if that is supported, the change uses the existing MBEDTLS_HAS_BUILTIN macro. That macro was defined later in library/common.h than is needed for this change, so it was moved up to join some other macros that looked similar. Upstream PR: Mbed-TLS/mbedtls#10318 Signed-off-by: Keith Packard <[email protected]>
1 parent 7e3aee6 commit 2994b29

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

library/common.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ extern void (*mbedtls_test_hook_test_fail)(const char *test, int line, const cha
9999
* fall back to the unsafe implementation. */
100100
#define ARRAY_LENGTH(array) ARRAY_LENGTH_UNSAFE(array)
101101
#endif
102+
103+
#if defined(__has_builtin)
104+
#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
105+
#else
106+
#define MBEDTLS_HAS_BUILTIN(x) 0
107+
#endif
108+
102109
/** Allow library to access its structs' private members.
103110
*
104111
* Although structs defined in header files are publicly available,
@@ -208,6 +215,11 @@ static inline void mbedtls_xor(unsigned char *r,
208215
return;
209216
}
210217
#endif
218+
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
219+
if (__builtin_constant_p(n) && n % 16 == 0) {
220+
return;
221+
}
222+
#endif
211223
#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
212224
/* This codepath probably only makes sense on architectures with 64-bit registers */
213225
for (; (i + 8) <= n; i += 8) {
@@ -219,6 +231,11 @@ static inline void mbedtls_xor(unsigned char *r,
219231
return;
220232
}
221233
#endif
234+
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
235+
if (__builtin_constant_p(n) && n % 8 == 0) {
236+
return;
237+
}
238+
#endif
222239
#else
223240
for (; (i + 4) <= n; i += 4) {
224241
uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
@@ -229,6 +246,11 @@ static inline void mbedtls_xor(unsigned char *r,
229246
return;
230247
}
231248
#endif
249+
#if defined(MBEDTLS_COMPILER_IS_GCC) && MBEDTLS_HAS_BUILTIN(__builtin_constant_p)
250+
if (__builtin_constant_p(n) && n % 4 == 0) {
251+
return;
252+
}
253+
#endif
232254
#endif
233255
#endif
234256
for (; i < n; i++) {
@@ -367,12 +389,6 @@ static inline void mbedtls_xor_no_simd(unsigned char *r,
367389
struct ISO_C_does_not_allow_extra_semicolon_outside_of_a_function
368390
#endif
369391

370-
#if defined(__has_builtin)
371-
#define MBEDTLS_HAS_BUILTIN(x) __has_builtin(x)
372-
#else
373-
#define MBEDTLS_HAS_BUILTIN(x) 0
374-
#endif
375-
376392
/* Define compiler branch hints */
377393
#if MBEDTLS_HAS_BUILTIN(__builtin_expect)
378394
#define MBEDTLS_LIKELY(x) __builtin_expect(!!(x), 1)

0 commit comments

Comments
 (0)