Find a file
Jakub Jelinek 819bc4f670 libcpp: Fix valgrind errors on pr88974.c [PR112956]
On the c-c++-common/cpp/pr88974.c testcase I'm seeing
==600549== Conditional jump or move depends on uninitialised value(s)
==600549==    at 0x1DD3A05: cpp_get_token_1(cpp_reader*, unsigned int*) (macro.cc:3050)
==600549==    by 0x1DBFC7F: _cpp_parse_expr (expr.cc:1392)
==600549==    by 0x1DB9471: do_if(cpp_reader*) (directives.cc:2087)
==600549==    by 0x1DBB4D8: _cpp_handle_directive (directives.cc:572)
==600549==    by 0x1DCD488: _cpp_lex_token (lex.cc:3682)
==600549==    by 0x1DD3A97: cpp_get_token_1(cpp_reader*, unsigned int*) (macro.cc:2936)
==600549==    by 0x7F7EE4: scan_translation_unit (c-ppoutput.cc:350)
==600549==    by 0x7F7EE4: preprocess_file(cpp_reader*) (c-ppoutput.cc:106)
==600549==    by 0x7F6235: c_common_init() (c-opts.cc:1280)
==600549==    by 0x704C8B: lang_dependent_init (toplev.cc:1837)
==600549==    by 0x704C8B: do_compile (toplev.cc:2135)
==600549==    by 0x704C8B: toplev::main(int, char**) (toplev.cc:2306)
==600549==    by 0x7064BA: main (main.cc:39)
error.  The problem is that _cpp_lex_direct can leave result->src_loc
uninitialized in some cases and later on we use that location_t.

_cpp_lex_direct essentially does:
  cppchar_t c;
...
  cpp_token *result = pfile->cur_token++;

 fresh_line:
  result->flags = 0;
...
  if (buffer->need_line)
    {
      if (pfile->state.in_deferred_pragma)
        {
          result->type = CPP_PRAGMA_EOL;
          ... // keeps result->src_loc uninitialized;
          return result;
        }
      if (!_cpp_get_fresh_line (pfile))
        {
          result->type = CPP_EOF;
          if (!pfile->state.in_directive && !pfile->state.parsing_args)
            {
              result->src_loc = pfile->line_table->highest_line;
              ...
            }
          ... // otherwise result->src_loc is sometimes uninitialized here
          return result;
        }
      ...
    }
...
  result->src_loc = pfile->line_table->highest_line;
...
  c = *buffer->cur++;
  switch (c)
    {
...
    case '\n':
...
      buffer->need_line = true;
      if (pfile->state.in_deferred_pragma)
        {
          result->type = CPP_PRAGMA_EOL;
...
          return result;
        }
      goto fresh_line;
...
    }
...
So, if _cpp_lex_direct is called without buffer->need_line initially set,
result->src_loc is always initialized (and actually hundreds of tests rely
on that exact value it has), even when c == '\n' and we set that flag later
on and goto fresh_line.  For CPP_PRAGMA_EOL case we have in that case
separate handling and don't goto.
But if _cpp_lex_direct is called with buffer->need_line initially set and
either decide to return a CPP_PRAGMA_EOL token or if getting a new line fails
for some reason and we return an CPP_ERROR token and we are in directive
or parsing args state, it is kept uninitialized and can be whatever the
allocation left it there as.

The following patch attempts to keep the status quo, use value that was
returned previously if it was initialized (i.e. we went through the
goto fresh_line; statement in c == '\n' handling) and only initialize
result->src_loc if it was uninitialized before.

2023-12-13  Jakub Jelinek  <jakub@redhat.com>

	PR preprocessor/112956
	* lex.cc (_cpp_lex_direct): Initialize c to 0.
	For CPP_PRAGMA_EOL tokens and if c == 0 also for CPP_EOF
	set result->src_loc to highest locus.
2023-12-13 21:16:14 +01:00
.github Minor formatting fix for newly-added file from previous commit 2023-11-01 19:28:56 -04:00
c++tools Daily bump. 2023-06-23 00:16:38 +00:00
config Daily bump. 2023-12-01 00:17:36 +00:00
contrib download_prerequisites: add --only-gettext 2023-12-13 13:14:18 +01:00
fixincludes Daily bump. 2023-11-23 00:18:14 +00:00
gcc c++: TARGET_EXPR location in default arg [PR96997] 2023-12-13 15:04:44 -05:00
gnattools Daily bump. 2023-04-26 00:17:46 +00:00
gotools Daily bump. 2023-11-04 00:16:45 +00:00
include Daily bump. 2023-12-11 00:17:32 +00:00
INSTALL
libada Daily bump. 2023-08-08 00:17:37 +00:00
libatomic Daily bump. 2023-11-14 12:23:39 +00:00
libbacktrace Daily bump. 2023-12-01 00:17:36 +00:00
libcc1 Daily bump. 2023-12-07 00:17:06 +00:00
libcody Daily bump. 2023-06-16 00:17:18 +00:00
libcpp libcpp: Fix valgrind errors on pr88974.c [PR112956] 2023-12-13 21:16:14 +01:00
libdecnumber Daily bump. 2023-06-16 00:17:18 +00:00
libffi Daily bump. 2023-10-27 00:17:12 +00:00
libgcc Daily bump. 2023-12-12 00:17:22 +00:00
libgfortran Daily bump. 2023-12-12 00:17:22 +00:00
libgm2 PR modula2/112921 missing modules shortreal shortstr shortconv convstringshort 2023-12-13 17:35:02 +00:00
libgo libgo: update configure.ac to upstream GCC 2023-11-30 13:23:53 -08:00
libgomp Fix 'libgomp/config/linux/allocator.c' 'size_t' vs. '%ld' format string mismatch 2023-12-13 21:12:47 +01:00
libiberty Daily bump. 2023-12-06 00:17:50 +00:00
libitm Daily bump. 2023-10-23 00:16:43 +00:00
libobjc Daily bump. 2023-10-23 00:16:43 +00:00
libphobos Daily bump. 2023-12-12 00:17:22 +00:00
libquadmath Daily bump. 2023-12-13 00:17:49 +00:00
libsanitizer Daily bump. 2023-11-29 00:17:27 +00:00
libssp Daily bump. 2023-10-23 00:16:43 +00:00
libstdc++-v3 libstdc++: Fix regression in std::format output of %Y for negative years 2023-12-13 12:30:14 +00:00
libvtv Daily bump. 2023-10-23 00:16:43 +00:00
lto-plugin Daily bump. 2023-10-23 00:16:43 +00:00
maintainer-scripts Daily bump. 2023-11-14 12:23:39 +00:00
zlib Daily bump. 2023-10-23 00:16:43 +00:00
.dir-locals.el
.gitattributes
.gitignore *: add modern gettext 2023-11-14 00:47:11 +01:00
ABOUT-NLS
ar-lib
ChangeLog Daily bump. 2023-12-13 00:17:49 +00:00
ChangeLog.jit
ChangeLog.tree-ssa
compile
config-ml.in LoongArch: Reimplement multilib build option handling. 2023-09-15 10:42:12 +08:00
config.guess
config.rpath
config.sub
configure *: add modern gettext 2023-11-14 00:47:11 +01:00
configure.ac *: add modern gettext 2023-11-14 00:47:11 +01:00
COPYING
COPYING.LIB
COPYING.RUNTIME
COPYING3
COPYING3.LIB
depcomp
install-sh
libtool-ldflags
libtool.m4 Build: fix error in fixinclude configure 2023-11-22 11:54:33 +01:00
ltgcc.m4
ltmain.sh
ltoptions.m4
ltsugar.m4
ltversion.m4
lt~obsolete.m4
MAINTAINERS Add myself to write after approval 2023-12-12 09:37:21 +01:00
Makefile.def gettext: disable install, docs targets, libasprintf, threads 2023-12-13 13:17:35 +01:00
Makefile.in gettext: disable install, docs targets, libasprintf, threads 2023-12-13 13:17:35 +01:00
Makefile.tpl Makefile.tpl: Avoid race condition in generating site.exp from the top level 2023-11-19 11:07:09 -05:00
missing
mkdep
mkinstalldirs
move-if-change
multilib.am
README
SECURITY.txt secpol: consistent indentation 2023-10-05 12:00:39 -04:00
symlink-tree
test-driver
ylwrap

This directory contains the GNU Compiler Collection (GCC).

The GNU Compiler Collection is free software.  See the files whose
names start with COPYING for copying permission.  The manuals, and
some of the runtime libraries, are under different terms; see the
individual source files for details.

The directory INSTALL contains copies of the installation information
as HTML and plain text.  The source of this information is
gcc/doc/install.texi.  The installation information includes details
of what is included in the GCC sources and what files GCC installs.

See the file gcc/doc/gcc.texi (together with other files that it
includes) for usage and porting information.  An online readable
version of the manual is in the files gcc/doc/gcc.info*.

See http://gcc.gnu.org/bugs/ for how to report bugs usefully.

Copyright years on GCC source files may be listed using range
notation, e.g., 1987-2012, indicating that every year in the range,
inclusive, is a copyrightable year that could otherwise be listed
individually.