This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [RFA] [PATCH] Add a warning for invalid function casts
- From: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- To: Joseph Myers <joseph at codesourcery dot com>
- Cc: "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 5 Oct 2017 14:02:51 +0000
- Subject: Re: [RFA] [PATCH] Add a warning for invalid function casts
- Authentication-results: sourceware.org; auth=none
- Authentication-results: codesourcery.com; dkim=none (message not signed) header.d=none;codesourcery.com; dmarc=none action=none header.from=hotmail.de;
- References: <AM5PR0701MB265700BCFDF7620223717DCCE4720@AM5PR0701MB2657.eurprd07.prod.outlook.com> <alpine.DEB.2.20.1710032128580.8372@digraph.polyomino.org.uk>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:99
On 10/03/17 23:34, Joseph Myers wrote:
> On Tue, 3 Oct 2017, Bernd Edlinger wrote:
>
>> invalid, also if both function types have a non-null TYPE_ARG_TYPES
>> I would say this deserves a warning. As an exception I have
>
> I'm not convinced by the TYPE_ARG_TYPES check, at least for C.
>
I will drop that, for C and C++, and try to get it working without
that kludge.
> In C, unprototyped function types are not compatible with variadic
> function types or functions with argument types changed by default
> argument promotions (that is, int () and int (char) and int (int, ...) are
> all incompatible). I'd think it appropriate to warn about such
> conversions, given that they are cases where calling the converted
> function has undefined behavior.
>
Right, interesting is that this does not produce a warning,
int test(int);
int foo()
{
int (*x)();
x = test;
return x(1);
}
while the following example produces a warning:
int test(int,...);
int foo()
{
int (*x)(int);
x = test;
return x(1);
}
gcc -Wall -W -S t1.c
t1.c: In function 'foo':
t1.c:6:5: warning: assignment to 'int (*)(int)' from incompatible
pointer type 'int (*)(int)' [-Wincompatible-pointer-types]
x = test;
^
I will send a patch that adds ", ..." to the parameter list
in the diagnostics...
But why is int(*)(int) compatible to (int)(*)() but not
to int(*)(int,...) ?
> There may well be cases of interfaces where void (*) (void) is used as a
> generic function pointer type (always converted to / from the actual type
> of the function in question), for which this warning would not be
> suitable.
>
Yes, those would get a warning, or have to add a cast to uintptr_t, if
it is not possible to fix the code otherwise. libffi does this...
Bernd.