Bug 11234 - -pedantic accepts function pointer <-> void*
Summary: -pedantic accepts function pointer <-> void*
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 3.3
: P3 normal
Target Milestone: 3.4.0
Assignee: Not yet assigned to anyone
Keywords: accepts-invalid, diagnostic
: 83584 (view as bug list)
Depends on:
Reported: 2003-06-18 13:20 UTC by Hallvard B Furuseth
Modified: 2023-03-21 04:16 UTC (History)
3 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed: 2003-10-24 18:11:31


Note You need to log in before you can comment on or make changes to this bug.
Description Hallvard B Furuseth 2003-06-18 13:20:25 UTC
	gcc -ansi -pedantic accepts some instances of conversion between
	function pointers and object pointers:

		void *v = function;
		(function_type) char_ptr;
		(char *) function;

	That's not valid ANSI C, even with casts.  ANSI
	(Pointers), 3.3.4 (Cast operators) and (Simple
	assignment) all explicitly talk about conversion to/from
	pointers to an 'object or incomplete type'; that explicitly
	excludes pointers to functions.

	(Note, the assignment void_ptr = function; does get a warning,
	but the corresponding initialization does not.)

System: SunOS bombur.uio.no 5.8 Generic_108528-21 sun4u sparc SUNW,Ultra-5_10
Architecture: sun4

host: sparc-sun-solaris2.8
build: sparc-sun-solaris2.8
target: sparc-sun-solaris2.8
configured with: ../gcc-3.3/configure --enable-languages=c,c++,f77 --disable-shared --disable-multilib --prefix=/bombur/tmp/gcc --enable-version-specific-runtime-libs --enable-threads

bash$ gcc -ansi -pedantic -S a.c
a.c: In function `function':
a.c:5: warning: ISO C forbids initialization between function pointer and `void *'
a.c:6: warning: initialization from incompatible pointer type
a.c:12: warning: assignment from incompatible pointer type
a.c:13: warning: ISO C forbids assignment between function pointer and `void *'
bash$ cat a.c
typedef void (*function_type)(void);

void function(void *void_ptr, char *char_ptr)
    function_type func_ptr = void_ptr;  /* warning (ISO C) */
    char *s = function;                 /* warning */

    void *v = function;                 /* should warn */
    (function_type) char_ptr;           /* should warn */
    (char *) function;                  /* should warn */

    char_ptr = function;                /* warning */
    void_ptr = function;                /* warning (ISO C) */
    func_ptr = (void *)0;               /* OK - null pointer constant */
Comment 1 Nathanael C. Nerode 2003-07-05 21:20:10 UTC
Present in mainline.
Comment 2 Eric Botcazou 2003-09-08 07:28:01 UTC
Generic C front-end problem.
Comment 3 Dara Hazeghi 2003-10-24 18:11:31 UTC
Still present
Comment 4 GCC Commits 2004-01-09 20:04:03 UTC
Subject: Bug 11234

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jsm28@gcc.gnu.org	2004-01-09 20:03:58

Modified files:
	gcc            : ChangeLog c-typeck.c except.c recog.h final.c 
	gcc/testsuite  : ChangeLog 
	gcc/testsuite/gcc.dg/weak: weak-6.c weak-7.c 
Added files:
	gcc/testsuite/gcc.dg: func-ptr-conv-1.c 

Log message:
	PR c/11234
	* c-typeck.c (build_c_cast): If pedantic, warn for conversions
	between function and object pointers.
	(digest_init): When comparing a pointer to function type to the
	target type, only apply TREE_TYPE once to the pointer to function
	* except.c (for_each_eh_label_1): Treat data as a pointer to a
	function pointer rather than casting it to a function pointer.
	(for_each_eh_label): Update caller.
	* recog.h (struct insn_data): Use a struct or union for output.
	* genoutput.c (output_insn_data): Update.
	* final.c (get_insn_template): Update.
	* gcc.dg/func-ptr-conv-1.c: New test.
	* gcc.dg/weak/weak-6.c, gcc.dg/weak/weak-7.c: Update.


Comment 5 Andrew Pinski 2004-01-10 04:01:56 UTC
Fixed for 3.4.
Comment 6 Andrew Pinski 2017-12-26 20:12:47 UTC
*** Bug 83584 has been marked as a duplicate of this bug. ***
Comment 7 Andrew Pinski 2017-12-26 20:13:39 UTC
Reopening based on the duplicated bug says our reading of the standard is incorrect.  But I will let someone else decide if that is really true or not.
Comment 9 Andrew Pinski 2017-12-26 20:22:58 UTC

This patch fixes PR c/11234 (nondiagnosis of certain invalid
conversions between function and object pointers). 

So fixed.
Comment 10 Hallvard B Furuseth 2017-12-26 23:57:19 UTC
On 26/12/17 21:13, pinskia at gcc dot gnu.org wrote:
> Reopening based on the duplicated bug says our reading of the standard is
> incorrect.  But I will let someone else decide if that is really true or not.

Er, yes.  I wrote the bug report that caused that warning, and I think I
was wrong.  The warning should go away, or move to -Wextra or something.