From 7f96069954dd3c903256a2ca8ae26ece0a274c51 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 3 Apr 2025 08:59:39 +0100 Subject: [PATCH] Use _Countof to implement lenof, where available. Up-to-date trunk clang has introduced a built-in operator called _Countof, which is like the 'lenof' macro in this code (returns the number of elements in a statically-declared array object) but with the safety advantage that it provokes a compile error if you accidentally use it on a pointer. In this commit I add a cmake-time check for it, and conditional on that, switch over the definition of lenof. This should add a safety check for accidental uses of lenof(pointer). When I tested it with new clang, this whole code base compiled cleanly with the new setting, so there aren't currently any such accidents. clang cites C2y as the source for _Countof: WG14 document N3369 initially proposed it under a different name, and then there was a big internet survey about naming (in which of course I voted for lenof!), and document N3469 summarises the results, which show that the name _Countof and/or countof won. Links: https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3369.pdf https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3469.htm My reading of N3469 seems to say that there will _either_ be _Countof by itself, _or_ lowercase 'countof' as a new keyword, but they don't say which. They say they _don't_ intend to do the same equivocation we had with _Complex and _Bool, where you have a _Countof keyword and an optional header file defining a lowercase non-underscore macro wrapping it. But there hasn't been a new whole draft published since N3469 yet, so I don't know what will end up in it when there is. However, as of now, _Countof exists in at least one compiler, and that seems like enough reason to implement it here. If it becomes 'countof' in the real standard, then we can always change over later. (And in that case it would probably make sense to rename the macro throughout the code base to align with what will become the new standard usage.) --- charset/internal.h | 8 +++++++- cmake/cmake.h.in | 2 ++ cmake/setup.cmake | 6 ++++++ misc.h | 4 ++++ utils/tree234.c | 8 +++++++- 5 files changed, 26 insertions(+), 2 deletions(-) diff --git a/charset/internal.h b/charset/internal.h index 32d0f6d5..7fd6628f 100644 --- a/charset/internal.h +++ b/charset/internal.h @@ -6,7 +6,13 @@ #define charset_internal_h /* This invariably comes in handy */ -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) +#ifndef lenof +#if HAVE_COUNTOF +#define lenof(x) _Countof(x) +#else +#define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) +#endif +#endif /* This is an invalid Unicode value used to indicate an error. */ #define ERROR 0xFFFFL /* Unicode value representing error */ diff --git a/cmake/cmake.h.in b/cmake/cmake.h.in index 3d36b3d0..3b64d03f 100644 --- a/cmake/cmake.h.in +++ b/cmake/cmake.h.in @@ -5,6 +5,8 @@ #cmakedefine NO_MULTIMON +#cmakedefine01 HAVE_COUNTOF + #cmakedefine01 HAVE_WINRESRC_H #cmakedefine01 HAVE_WINRES_H #cmakedefine01 HAVE_WIN_H diff --git a/cmake/setup.cmake b/cmake/setup.cmake index 1be448df..75af0eac 100644 --- a/cmake/setup.cmake +++ b/cmake/setup.cmake @@ -116,6 +116,12 @@ int main(int argc, char **argv) { free(p); }" HAVE_ALIGNED_ALLOC) +check_c_source_compiles(" +int main(int argc, char **argv) { + int a[3]; + return _Countof(a); +}" HAVE_COUNTOF) + if(PUTTY_DEBUG) add_compile_definitions(DEBUG) endif() diff --git a/misc.h b/misc.h index c64eb01b..67a1833b 100644 --- a/misc.h +++ b/misc.h @@ -353,8 +353,12 @@ const char *conf_id(int key); #endif #ifndef lenof +#if HAVE_COUNTOF +#define lenof(x) _Countof(x) +#else #define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) #endif +#endif #ifndef min #define min(x,y) ( (x) < (y) ? (x) : (y) ) diff --git a/utils/tree234.c b/utils/tree234.c index 004cfb8d..e7b3d4fc 100644 --- a/utils/tree234.c +++ b/utils/tree234.c @@ -1361,7 +1361,13 @@ int mycmp(void *av, void *bv) return strcmp(a, b); } -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) +#ifndef lenof +#if HAVE_COUNTOF +#define lenof(x) _Countof(x) +#else +#define lenof(x) ( (sizeof((x))) / (sizeof(*(x)))) +#endif +#endif char *strings[] = { "a", "ab", "absque", "coram", "de",