Skip to content

Commit 2c49239

Browse files
committed
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 has been submitted upstream: Mbed-TLS/mbedtls#10318 Signed-off-by: Keith Packard <[email protected]>
1 parent 85440ef commit 2c49239

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

library/common.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ static inline void mbedtls_xor(unsigned char *r,
208208
return;
209209
}
210210
#endif
211+
#if defined(MBEDTLS_COMPILER_IS_GCC) && __has_builtin(__builtin_constant_p)
212+
if (__builtin_constant_p(n) && n % 16 == 0)
213+
return;
214+
#endif
211215
#elif defined(MBEDTLS_ARCH_IS_X64) || defined(MBEDTLS_ARCH_IS_ARM64)
212216
/* This codepath probably only makes sense on architectures with 64-bit registers */
213217
for (; (i + 8) <= n; i += 8) {
@@ -219,6 +223,10 @@ static inline void mbedtls_xor(unsigned char *r,
219223
return;
220224
}
221225
#endif
226+
#if defined(MBEDTLS_COMPILER_IS_GCC) && __has_builtin(__builtin_constant_p)
227+
if (__builtin_constant_p(n) && n % 8 == 0)
228+
return;
229+
#endif
222230
#else
223231
for (; (i + 4) <= n; i += 4) {
224232
uint32_t x = mbedtls_get_unaligned_uint32(a + i) ^ mbedtls_get_unaligned_uint32(b + i);
@@ -229,6 +237,10 @@ static inline void mbedtls_xor(unsigned char *r,
229237
return;
230238
}
231239
#endif
240+
#if defined(MBEDTLS_COMPILER_IS_GCC) && __has_builtin(__builtin_constant_p)
241+
if (__builtin_constant_p(n) && n % 4 == 0)
242+
return;
243+
#endif
232244
#endif
233245
#endif
234246
for (; i < n; i++) {

0 commit comments

Comments
 (0)