Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

2 messages, 2 authors, 2016-06-15 · open the first message on its own page

Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

From: Junio C Hamano <hidden>
Date: 2016-06-15 23:04:33

Elia Pinto [off-list ref] writes:
This is the second version of this patch.  It had not been
discussed before. In the second version, I just tried to clarify
the comment in the commit. I resend it just in case you missed
I do not recall seeing it before.  No discussion usually means no
interest, so I'll see what happens this time on the list for a few
days before picking it up.
+#if SUPPORT__BUILTIN_TYPES_COMPATIBLE_P
+/* &arr[0] degrades to a pointer: a different type from an array */
+#define _array_size_chk(arr)						\
+	BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(typeof(arr), \
+							typeof(&(arr)[0])))
+#else
+#define _array_size_chk(arr) 0
+#endif
Wouldn't there be a more sensible name?  _chk does not tell us
anything about what is being checked, and the only thing this name
gives us is "what uses it" (i.e. it is some magic used by array-size
and does not say what it checks and what for).

I think you are checking arr is an array and not a pointer.  Perhaps
"#define is_an_array(arr)" or something along that line may be a
more descriptive name for it.

I doubt the leading underscore is particularly a good idea, though.
+/*
+ * ARRAY_SIZE - get the number of elements in a visible array
+ *  <at> x: the array whose size you want.
+ *
+ * This does not work on pointers, or arrays declared as [], or
+ * function parameters.  With correct compiler support, such usage
+ * will cause a build error (see build_assert).
+ */
+#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + _array_size_chk(x))
 #define bitsizeof(x)  (CHAR_BIT * sizeof(x))
 
 #define maximum_signed_value_of_type(a) \

Re: [PATCH v2] git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array

From: Jeff King <hidden>
Date: 2016-06-15 23:04:33

On Mon, Apr 27, 2015 at 10:56:30AM -0700, Junio C Hamano wrote:
Elia Pinto [off-list ref] writes:
quoted
This is the second version of this patch.  It had not been
discussed before. In the second version, I just tried to clarify
the comment in the commit. I resend it just in case you missed
I do not recall seeing it before.  No discussion usually means no
interest, so I'll see what happens this time on the list for a few
days before picking it up.
I'm in favor of any method for improving compile-time bug-checking,
provided that:

  1. It does not carry a maintenance cost that spreads throughout the
     code base (e.g., here the cost is localized to making ARRAY_SIZE a
     bit more complex, but callers do not have to care about the extra
     checking).

  2. It gracefully degrades when using older compilers (i.e., we can
     fall back to the existing ARRAY_SIZE definition when the magic
     builtin is not available).

I think this is OK for both points. It would be nice if we could
auto-detect the presence of the builtin without using autoconf. I think
most git developers do not use autoconf at all, and the feature does not
help much if nobody turns it on. Is there a __GNUC__ or similar
feature-test macro we could use for this?
quoted
+#define _array_size_chk(arr) 0
+#endif
Wouldn't there be a more sensible name?  _chk does not tell us
anything about what is being checked, and the only thing this name
gives us is "what uses it" (i.e. it is some magic used by array-size
and does not say what it checks and what for).

I think you are checking arr is an array and not a pointer.  Perhaps
"#define is_an_array(arr)" or something along that line may be a
more descriptive name for it.

I doubt the leading underscore is particularly a good idea, though.
Agreed on the naming (modulo the "not" from your follow-on email). Also,
should it be in ALL_CAPS like the other added macros? We are slightly
inconsistent about that in our code-base.

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help