This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[RFA:] Correct handling of NO_IMPLICIT_EXTERN_C in c-lex.c:cb_enter_file
- To: gcc-patches at gcc dot gnu dot org
- Subject: [RFA:] Correct handling of NO_IMPLICIT_EXTERN_C in c-lex.c:cb_enter_file
- From: Hans-Peter Nilsson <hans-peter dot nilsson at axis dot com>
- Date: Fri, 10 Nov 2000 02:17:46 +0100
In cb_enter_file, the variable flags can have one of these
values for NO_IMPLICIT_EXTERN_C (see cpp_syshdr_flags):
" 3 4", " 3" or "".
Now spot the bug, given the patch below. :-) Yep, flags[2]
should not be dereferenced without knowing that flags[0] and
flags[1] are non-zero. Specifically, flags[0] can be zero and
flags[1] and/or flags[2] nonzero. In this case flags[2] was
'u', for "Multiple include guards may be useful for:\n", which
was the following string in memory. The bug shows up for me
(YMMV) during compilation of libio/iomanip.cc, where it was
claimed that the declaration and definition of a function were
in different extern "C"/"C++" contexts with a linked-in cpplib.
It would right now be safe to omit the flags[1] check because of
the given domain of flags, but it doesn't *feel* safe. The
whole "magic string contents" checking looks brittle IMHO.
This was bootstrapped on sparc-sun-solaris2.8, which looks like
it does not define NO_IMPLICIT_EXTERN_C, but which made no
difference there, perhaps due to alignment of constant strings.
I have no dejagnu installation on that system, so I couldn't
make check. (BTW, I'm since long having trouble bootstrapping
on that system. I had to delete the texinfo srcdir, because I
was running into some libz-related problem. Sorry, I do not
intend to pursue this; it might be a local problem.)
The fix seems pretty obvious, but I'll leave it to CPP hackers
to judge that. Ok to commit anyway?
* c-lex.c [! NO_IMPLICIT_EXTERN_C] (cb_enter_file): Check
in_system_header and flags[1] before dereferencing flags[2].
Index: c-lex.c
===================================================================
RCS file: /cvs/gcc/egcs/gcc/c-lex.c,v
retrieving revision 1.109
diff -c -p -c -p -4 -r1.109 c-lex.c
*** c-lex.c 2000/11/06 19:10:05 1.109
--- c-lex.c 2000/11/09 23:17:07
*************** cb_enter_file (pfile)
*** 713,721 ****
in_system_header = (flags[0] != 0);
#ifndef NO_IMPLICIT_EXTERN_C
if (c_header_level)
++c_header_level;
! else if (flags[2] != 0)
{
c_header_level = 1;
++pending_lang_change;
}
--- 713,721 ----
in_system_header = (flags[0] != 0);
#ifndef NO_IMPLICIT_EXTERN_C
if (c_header_level)
++c_header_level;
! else if (in_system_header && flags[1] != 0 && flags[2] != 0)
{
c_header_level = 1;
++pending_lang_change;
}
brgds, H-P