git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Jeff King <peff@peff.net>
Date
Apr 28, 2015, 05:42 UTC
Message-ID
<20150428054237.GI24580@peff.net>
In-Reply-To
<xmqq1tj5xxo1.fsf@gitster.dls.corp.google.com>
On Mon, Apr 27, 2015 at 10:56:30AM -0700, Junio C Hamano wrote:
Show 9 quoted lines
> Elia Pinto <gitter.spiros@gmail.com> 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.

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?

Show 13 quoted lines
> > +#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
Previous: Junio C Hamano
Message 4 of 4 in “git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of array”
  1. git-compat-util.h: implement a different ARRAY_SIZE macro for for safely deriving the size of arrayElia Pinto, Apr 27, 2015
  2. Junio C HamanoApr 27, 2015
  3. Junio C HamanoApr 27, 2015
  4. Jeff KingApr 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.