From bf0cb3c43fbecded3ea0f8046a24e914ef1820e1 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Fri, 5 Apr 2024 22:34:24 +0200 Subject: [PATCH] Codechange: let lengthof fail when anything that isn't a C-style array is passed (cherry picked from commit 97bea563d73ed493ddbe0b72309421f9f1c0aad2) --- src/stdafx.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/stdafx.h b/src/stdafx.h index 5c61c482e0..071875baa6 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -332,6 +332,9 @@ static_assert(SIZE_MAX >= UINT32_MAX); #define M_PI 3.14159265358979323846 #endif /* M_PI_2 */ +template +char (&ArraySizeHelper(T (&array)[N]))[N]; + /** * Return the length of an fixed size array. * Unlike sizeof this function returns the number of elements @@ -340,7 +343,7 @@ static_assert(SIZE_MAX >= UINT32_MAX); * @param x The pointer to the first element of the array * @return The number of elements */ -#define lengthof(x) (sizeof(x) / sizeof(x[0])) +#define lengthof(array) (sizeof(ArraySizeHelper(array))) /** * Get the end element of an fixed size array.