From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb2f.google.com (mail-yb1-xb2f.google.com [IPv6:2607:f8b0:4864:20::b2f]) by sourceware.org (Postfix) with ESMTPS id 68EAC383878F for ; Sat, 25 Jun 2022 14:27:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 68EAC383878F Received: by mail-yb1-xb2f.google.com with SMTP id r3so9262849ybr.6 for ; Sat, 25 Jun 2022 07:27:38 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=0XkMNpcAfY0OUQiQZN1TGefZDnYSQQVKZnpjo2ypj9I=; b=i6HQdTkPkHOlJrDU7W1b/WpIQ5FvD9BVNX1HrpomDgGa+KC7IIo79xoGIPoYfG8C2/ PrV9BgajPkQWenUV+1nrw6hE+9CDCEuPaQkrMZD8wjBpCaMQKvy7SduCJKpTdB+9lM/D VVHq64/W+OZ8mo63kJxKlgriGmwdTfO82CfEG7d7cb6iAH1Z/QOhB605Acppj3DF2pSK Xm6vhiPALjQg+nAAjsHw1viOuvsiqxe+UzGbSmkw2/G8oCEcyuUJlyIsZbHqEG28/z1C JT/6rd3NN30usj/7t7837gKdju2bguu2ArD+VzGXWpeSO+U0etOQo5QIZsM7wtvgdaX2 Dzfw== X-Gm-Message-State: AJIora/ffinY+6TkIlFn9GNKu4iG06xqvAEEetijStudmPwmPq2B65CP aKhSfHXN6ZUe6aLNU4sSIBoOD+SKzH1sbAu3fJI= X-Google-Smtp-Source: AGRyM1vsspqqGRnuVf6pMMkWpfK8mt/nH2Yzv8HkvRlicmwgQIgOtDJaVAViximRboJtI9wF5sebBJ32/QSzObxziQw= X-Received: by 2002:a25:3357:0:b0:669:27:52d with SMTP id z84-20020a253357000000b006690027052dmr4253719ybz.500.1656167257738; Sat, 25 Jun 2022 07:27:37 -0700 (PDT) MIME-Version: 1.0 References: <17F275A7-BDEC-46F3-A6E3-4EBE354771A9@gmail.com> In-Reply-To: From: Yair Lenga Date: Sat, 25 Jun 2022 10:27:26 -0400 Message-ID: Subject: Re: Safer vararg calls To: Jonathan Wakely Cc: "gcc@gcc.gnu.org" X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, HTML_MESSAGE, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Content-Filtered-By: Mailman/MimeDel 2.1.29 X-BeenThere: gcc@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 25 Jun 2022 14:27:40 -0000 Hi Jonathan, thanks for taking the time to review. I agree with your comment about the attribute name (va_vector, va_type). My best improvement is "va_sametype". Is it better ? ? May be "va_matchtype" ? any other suggestions ? For the case of the sentinel/va_sametype, I hope that the implementation will recognize the combination of the two, and will allow NULL to be used as-is, without having to cast it. Altough I believe that NULL pointers are considered compatible with with any pointer. Not 100% sure about this. I'm not sure I understand the question about mixing char * and const char *. Probably I cause confusion with my rushed example, which should be: // join all parameters, return newly allocate string. __attribute__ ((malloc(free), va_matchtype, sentinel)) char *delimitedstr(char delim, const char *p1, ...); On Tue, Jun 21, 2022 at 6:44 AM Jonathan Wakely wrote: > On Tue, 21 Jun 2022 at 11:17, Yair Lenga via Gcc 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 th= e > same type. In my case, there are terminated with a sentinel of null. > > > > Char *result =3D delimitedstr(=E2=80=98:=E2=80=99 =E2=80=9Cfoo=E2=80=9D= , =E2=80=9Cbar=E2=80=9D, =E2=80=9Czoo=E2=80=9D, NULL) ; > > > > The standard prototype > > is char * delimitedstr(char delim, char *p1=E2=80=A6) ; > > > > Which will currently allow many incorrect calls: > > delimitedstr(=E2=80=98:=E2=80=99, =E2=80=9Cfoo=E2=80=9D, 5, 7.3, =E2= =80=98a=E2=80=99) ; // 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=E2=80=A6) = ; > > "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 =E2=80=98datatype =E2=80=A6=E2=80=99 (without the conversion in= to array). > > > > I am Looking for feedback, Pointers on how to implement, as I do not > have experience with extending gcc. > > > > Yair >