public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Safer vararg calls
@ 2022-06-21 10:16 Yair Lenga
  2022-06-21 10:43 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Yair Lenga @ 2022-06-21 10:16 UTC (permalink / raw)
  To: gcc

Hi,

Looking for feedback on the adding new attribute to function calls that will help create safer vararg functions.

Consider the case where a vararg function takes list of arguments of the same type. In my case, there are terminated with a sentinel of null.

Char *result = delimitedstr(‘:’ “foo”, “bar”, “zoo”, NULL) ;

The standard prototype
is char * delimitedstr(char delim, char *p1…) ;

Which will currently allow many incorrect calls:
 delimitedstr(‘:’, “foo”, 5, 7.3, ‘a’) ;    // bad types + missing sentinel.

The __attribute__((sentinel)) can force the last arg to be null.

My proposal is to add new attribute ((va_vector)) that will add a check that all parameters in a vararg list match the typeof the last parameter. So that:

__attribute__ ((va_typed)) delimitedstr(char delim, char *p1…) ;

Will flag a call where any of the parameter after p1, is not a string.

This can result in cleaner, safer code, without making the calling sequence more difficult, or modifying the behavior of the call.

For Java developers, this is basically the same type checking provided by the as ‘datatype …’ (without the conversion into array).

I am Looking for feedback, Pointers on how to implement, as I do not have experience with extending gcc.

Yair

^ permalink raw reply	[flat|nested] 4+ messages in thread
* Re: Gcc Digest, Vol 29, Issue 7
@ 2022-07-05 12:16 Florian Weimer
  2022-07-05 21:24 ` Yair Lenga
  0 siblings, 1 reply; 4+ messages in thread
From: Florian Weimer @ 2022-07-05 12:16 UTC (permalink / raw)
  To: Yair Lenga via Gcc; +Cc: Yair Lenga

* Yair Lenga via Gcc:

> My question: does anyone know how much effort it will be to add a new GCC
> built-in (or extension), that will automatically generate a descriptive
> format string, consistent with scanf formatting, avoiding the need to
> manually enter the formatting string.

It's already possible to do this with C++.  Can't you just use C++
instead?

Thanks,
Florian


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-07-07 10:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-21 10:16 Safer vararg calls Yair Lenga
2022-06-21 10:43 ` Jonathan Wakely
2022-06-25 14:27   ` Yair Lenga
2022-07-05 12:16 Gcc Digest, Vol 29, Issue 7 Florian Weimer
2022-07-05 21:24 ` Yair Lenga
2022-07-07 10:02   ` Safer vararg calls Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).