FWD: Enhahncement: new function attribute for stdarg functions

Paul D. Smith psmith@baynetworks.com
Mon Mar 15 08:30:00 GMT 1999


I suppose many of you follow gnu.gcc.bug but I thought I'd forward this
along to EGCS for consideration and comment, as well:

  After spending a half-hour cleaning up broken variadic function calls, I
  had an idea for a new GCC attribute: nullsentinel or something (I have
  no idea what a good name is :).

  Basically, with variadic functions there are only a few ways to write
  them: with some kind of format string a la *printf() etc., with some
  count value provided at the beginning, or with a sentinel value
  signaling the end.

  One of the most common uses of variadic functions is to pass an array of
  pointers where the last pointer is a null pointer, denoting the end of
  the list of args.  The problem here is that many people forget you have
  to cast the 0, if you use a 0, and even cast your NULL macro.

  What would be nice is if you could add an attribute like this to a
  variadic function so the compiler would check to make sure that the last
  argument was some sort of pointer.  I suppose you could even make it an
  attribute that takes an argument, which would be a type definition, and
  the compiler could check that the last argument was compatible with that
  type.  I _suppose_ you could even have the ability to check that _all_
  variable arguments were compatible with a particular type.

And Miles Bader <miles@ccs.mt.nec.co.jp> provided this excellent idea in
a followup:

  If the last argument is constant, it should additionally check to make
  sure that it is indeed a *null* pointer.

  This would catch the common `execl ("/bin/foo", "foo", "bar")' mistake.
  [and I think a very large proportion of calls to sentinel-terminated
  variadic functions are indeed of the `list-of-constants' sort.]

Comments?

-- 
-------------------------------------------------------------------------------
 Paul D. Smith <psmith@baynetworks.com>         Network Management Development
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
-------------------------------------------------------------------------------
   These are my opinions---Nortel Networks takes no responsibility for them.


More information about the Gcc mailing list