public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Yair Lenga <yair.lenga@gmail.com>
Cc: "gcc@gcc.gnu.org" <gcc@gcc.gnu.org>
Subject: Re: Safer vararg calls
Date: Tue, 21 Jun 2022 11:43:54 +0100	[thread overview]
Message-ID: <CAH6eHdQNBeaR=Tc6XKD2c8K3QGRGPh4OmoEXshnLpRm95bN-kw@mail.gmail.com> (raw)
In-Reply-To: <17F275A7-BDEC-46F3-A6E3-4EBE354771A9@gmail.com>

On Tue, 21 Jun 2022 at 11:17, Yair Lenga via Gcc <gcc@gcc.gnu.org> wrote:
>
> 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:

"va_vector" is a bad name IMHO. It tells me nothing about what it
means. Does it have something to do with SIMD vectors?

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

"va_typed" at least suggests something to do with types, but it
doesn't tell me they have to be the same type.

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

In your example NULL does not have the same type as the earlier
arguments. You would have to write (char*)NULL to suppress a
diagnostic.

I also wonder how a mixture of char* and const char* arguments would
be handled in your example.


>
> 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

  reply	other threads:[~2022-06-21 10:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-21 10:16 Yair Lenga
2022-06-21 10:43 ` Jonathan Wakely [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH6eHdQNBeaR=Tc6XKD2c8K3QGRGPh4OmoEXshnLpRm95bN-kw@mail.gmail.com' \
    --to=jwakely.gcc@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=yair.lenga@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).