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 3.2.2.3 (Pointers), 3.3.4 (Cast operators) and 3.3.16.1 (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.) Environment: 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 How-To-Repeat: 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 */ }
Present in mainline.
Generic C front-end problem.
Still present
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 genoutput.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 type. * 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. testsuite: * gcc.dg/func-ptr-conv-1.c: New test. * gcc.dg/weak/weak-6.c, gcc.dg/weak/weak-7.c: Update. Patches: http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/ChangeLog.diff?cvsroot=gcc&r1=2.2202&r2=2.2203 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/c-typeck.c.diff?cvsroot=gcc&r1=1.270&r2=1.271 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/except.c.diff?cvsroot=gcc&r1=1.254&r2=1.255 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/recog.h.diff?cvsroot=gcc&r1=1.47&r2=1.48 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/final.c.diff?cvsroot=gcc&r1=1.303&r2=1.304 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/genoutput.c.diff?cvsroot=gcc&r1=1.79&r2=1.80 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/ChangeLog.diff?cvsroot=gcc&r1=1.3351&r2=1.3352 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/func-ptr-conv-1.c.diff?cvsroot=gcc&r1=NONE&r2=1.1 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/weak/weak-6.c.diff?cvsroot=gcc&r1=1.2&r2=1.3 http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/gcc.dg/weak/weak-7.c.diff?cvsroot=gcc&r1=1.2&r2=1.3
Fixed for 3.4.
*** Bug 83584 has been marked as a duplicate of this bug. ***
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.
https://gcc.gnu.org/viewcvs/gcc?view=revision&revision=75595
https://gcc.gnu.org/ml/gcc-patches/2004-01/msg00599.html This patch fixes PR c/11234 (nondiagnosis of certain invalid conversions between function and object pointers). So fixed.
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. Hallvard