Commit 3cf3593f by Neil Booth Committed by Neil Booth

cppfiles.c (stack_include_file): Push zero-length buffers in case of failure.

        * cppfiles.c (stack_include_file): Push zero-length buffers
        in case of failure.  Return void, as we don't fail any more.
        (read_include_file): Check for files we shouldn't re-read.
        Don't return an error code; errors are implied by marking the
        file NEVER_REREAD.
        (_cpp_execute_include): Move the recursion and in-macro checks
        here.  Update for stack_include_file not failing.
        * cpplib.c (cpp_push_buffer): Always succeed, since
        _cpp_execute_include performs the recursion check.  Tidy up.
        * cpplib.h (cpp_push_buffer): Update prototype.

From-SVN: r38057
parent d53c4221
2000-12-05 Neil Booth <neilb@earthling.net>
* cppfiles.c (stack_include_file): Push zero-length buffers
in case of failure. Return void, as we don't fail any more.
(read_include_file): Check for files we shouldn't re-read.
Don't return an error code; errors are implied by marking the
file NEVER_REREAD.
(_cpp_execute_include): Move the recursion and in-macro checks
here. Update for stack_include_file not failing.
* cpplib.c (cpp_push_buffer): Always succeed, since
_cpp_execute_include performs the recursion check. Tidy up.
* cpplib.h (cpp_push_buffer): Update prototype.
2000-12-05 Alexandre Oliva <aoliva@redhat.com> 2000-12-05 Alexandre Oliva <aoliva@redhat.com>
* sched-vis.c (visual_tbl_line_length): New variable. * sched-vis.c (visual_tbl_line_length): New variable.
......
...@@ -74,8 +74,8 @@ static struct include_file *find_include_file ...@@ -74,8 +74,8 @@ static struct include_file *find_include_file
PARAMS ((cpp_reader *, const char *, PARAMS ((cpp_reader *, const char *,
struct file_name_list *)); struct file_name_list *));
static struct include_file *open_file PARAMS ((cpp_reader *, const char *)); static struct include_file *open_file PARAMS ((cpp_reader *, const char *));
static int read_include_file PARAMS ((cpp_reader *, struct include_file *)); static void read_include_file PARAMS ((cpp_reader *, struct include_file *));
static int stack_include_file PARAMS ((cpp_reader *, struct include_file *)); static void stack_include_file PARAMS ((cpp_reader *, struct include_file *));
static void purge_cache PARAMS ((struct include_file *)); static void purge_cache PARAMS ((struct include_file *));
static void destroy_include_file_node PARAMS ((splay_tree_value)); static void destroy_include_file_node PARAMS ((splay_tree_value));
static int report_missing_guard PARAMS ((splay_tree_node, void *)); static int report_missing_guard PARAMS ((splay_tree_node, void *));
...@@ -204,10 +204,11 @@ open_file (pfile, filename) ...@@ -204,10 +204,11 @@ open_file (pfile, filename)
return 0; return 0;
} }
/* Place the file referenced by INC into a new buffer on PFILE's stack. /* Place the file referenced by INC into a new buffer on PFILE's
Return 1 if successful, 0 if not. */ stack. If there are errors, or the file should not be re-included,
a null buffer is pushed. */
static int static void
stack_include_file (pfile, inc) stack_include_file (pfile, inc)
cpp_reader *pfile; cpp_reader *pfile;
struct include_file *inc; struct include_file *inc;
...@@ -222,45 +223,37 @@ stack_include_file (pfile, inc) ...@@ -222,45 +223,37 @@ stack_include_file (pfile, inc)
lineno = pfile->buffer->lineno; lineno = pfile->buffer->lineno;
} }
if (pfile->context->prev) /* Not in cache? */
cpp_ice (pfile, "attempt to push file buffer with contexts stacked"); if (! inc->buffer)
read_include_file (pfile, inc);
if (DO_NOT_REREAD (inc))
return 0;
if (inc->buffer == NULL)
if (read_include_file (pfile, inc) == 0)
return 0;
/* Push a null buffer. */
fp = cpp_push_buffer (pfile, NULL, 0); fp = cpp_push_buffer (pfile, NULL, 0);
if (fp == 0)
return 0;
/* Initialise controlling macro state. */
pfile->mi_state = MI_OUTSIDE;
pfile->mi_cmacro = 0;
fp->inc = inc; fp->inc = inc;
fp->nominal_fname = inc->name; fp->nominal_fname = inc->name;
fp->buf = inc->buffer; fp->buf = inc->buffer;
fp->rlimit = fp->buf + inc->st.st_size; fp->rlimit = fp->buf;
if (! DO_NOT_REREAD (inc))
fp->rlimit += inc->st.st_size;
fp->cur = fp->buf; fp->cur = fp->buf;
fp->lineno = 0;
fp->line_base = fp->buf; fp->line_base = fp->buf;
fp->lineno = 0; /* For _cpp_do_file_change. */
fp->inc->refcnt++;
/* The ->actual_dir field is only used when ignore_srcdir is not in effect; /* The ->actual_dir field is only used when ignore_srcdir is not in effect;
see do_include */ see do_include */
if (!CPP_OPTION (pfile, ignore_srcdir)) if (!CPP_OPTION (pfile, ignore_srcdir))
fp->actual_dir = actual_directory (pfile, inc->name); fp->actual_dir = actual_directory (pfile, inc->name);
fp->inc->refcnt++; /* Initialise controlling macro state. */
pfile->mi_state = MI_OUTSIDE;
pfile->mi_cmacro = 0;
pfile->include_depth++; pfile->include_depth++;
pfile->input_stack_listing_current = 0; pfile->input_stack_listing_current = 0;
_cpp_do_file_change (pfile, FC_ENTER, filename, lineno); _cpp_do_file_change (pfile, FC_ENTER, filename, lineno);
fp->lineno = 1; fp->lineno = 1;
return 1;
} }
/* Read the file referenced by INC into the file cache. /* Read the file referenced by INC into the file cache.
...@@ -278,7 +271,7 @@ stack_include_file (pfile, inc) ...@@ -278,7 +271,7 @@ stack_include_file (pfile, inc)
FIXME: Flush file cache and try again if we run out of memory. */ FIXME: Flush file cache and try again if we run out of memory. */
static int static void
read_include_file (pfile, inc) read_include_file (pfile, inc)
cpp_reader *pfile; cpp_reader *pfile;
struct include_file *inc; struct include_file *inc;
...@@ -289,6 +282,9 @@ read_include_file (pfile, inc) ...@@ -289,6 +282,9 @@ read_include_file (pfile, inc)
static int pagesize = -1; static int pagesize = -1;
#endif #endif
if (DO_NOT_REREAD (inc))
return;
if (S_ISREG (inc->st.st_mode)) if (S_ISREG (inc->st.st_mode))
{ {
/* off_t might have a wider range than ssize_t - in other words, /* off_t might have a wider range than ssize_t - in other words,
...@@ -373,7 +369,7 @@ read_include_file (pfile, inc) ...@@ -373,7 +369,7 @@ read_include_file (pfile, inc)
close (inc->fd); close (inc->fd);
inc->buffer = buf; inc->buffer = buf;
inc->fd = -1; inc->fd = -1;
return 1; return;
perror_fail: perror_fail:
cpp_error_from_errno (pfile, inc->name); cpp_error_from_errno (pfile, inc->name);
...@@ -382,7 +378,7 @@ read_include_file (pfile, inc) ...@@ -382,7 +378,7 @@ read_include_file (pfile, inc)
close (inc->fd); close (inc->fd);
inc->fd = -1; inc->fd = -1;
inc->cmacro = NEVER_REREAD; inc->cmacro = NEVER_REREAD;
return 0; return;
} }
static void static void
...@@ -583,6 +579,20 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start) ...@@ -583,6 +579,20 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start)
struct include_file *inc; struct include_file *inc;
char *fname; char *fname;
/* Help protect #include or similar from recursion. */
if (pfile->buffer_stack_depth >= CPP_STACK_MAX)
{
cpp_fatal (pfile, "#include nested too deeply");
return;
}
/* Check we've tidied up #include before entering the buffer. */
if (pfile->context->prev)
{
cpp_ice (pfile, "attempt to push file buffer with contexts stacked");
return;
}
fname = alloca (len + 1); fname = alloca (len + 1);
memcpy (fname, header->val.str.text, len); memcpy (fname, header->val.str.text, len);
fname[len] = '\0'; fname[len] = '\0';
...@@ -613,11 +623,13 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start) ...@@ -613,11 +623,13 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start)
inc->include_count++; inc->include_count++;
/* Actually process the file. */ /* Actually process the file. */
if (stack_include_file (pfile, inc)) stack_include_file (pfile, inc);
{
if (angle_brackets) if (angle_brackets)
pfile->system_include_depth++; pfile->system_include_depth++;
if (! DO_NOT_REREAD (inc))
{
if (no_reinclude) if (no_reinclude)
inc->cmacro = NEVER_REREAD; inc->cmacro = NEVER_REREAD;
...@@ -630,6 +642,7 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start) ...@@ -630,6 +642,7 @@ _cpp_execute_include (pfile, header, no_reinclude, search_start)
fprintf (stderr, " %s\n", inc->name); fprintf (stderr, " %s\n", inc->name);
} }
} }
return; return;
} }
...@@ -730,11 +743,8 @@ _cpp_read_file (pfile, fname) ...@@ -730,11 +743,8 @@ _cpp_read_file (pfile, fname)
return 0; return 0;
} }
/* Return success for zero-length files. */ stack_include_file (pfile, f);
if (DO_NOT_REREAD (f))
return 1; return 1;
return stack_include_file (pfile, f);
} }
/* Do appropriate cleanup when a file buffer is popped off the input /* Do appropriate cleanup when a file buffer is popped off the input
......
...@@ -1731,42 +1731,34 @@ handle_assertion (pfile, str, type) ...@@ -1731,42 +1731,34 @@ handle_assertion (pfile, str, type)
run_directive (pfile, type, str, count, 0); run_directive (pfile, type, str, count, 0);
} }
/* Allocate a new cpp_buffer for PFILE, and push it on the input /* Push a new buffer on the buffer stack. Buffer can be NULL, but
buffer stack. If BUFFER != NULL, then use the LENGTH characters in then LEN should be 0. Returns the new buffer; it doesn't fail. */
BUFFER as the new input buffer. Return the new buffer, or NULL on
failure. */
cpp_buffer * cpp_buffer *
cpp_push_buffer (pfile, buffer, length) cpp_push_buffer (pfile, buffer, len)
cpp_reader *pfile; cpp_reader *pfile;
const U_CHAR *buffer; const U_CHAR *buffer;
long length; size_t len;
{ {
cpp_buffer *buf = CPP_BUFFER (pfile); cpp_buffer *new = xobnew (pfile->buffer_ob, cpp_buffer);
cpp_buffer *new;
if (++pfile->buffer_stack_depth == CPP_STACK_MAX)
{
cpp_fatal (pfile, "#include nested too deeply");
return NULL;
}
new = xobnew (pfile->buffer_ob, cpp_buffer);
/* Clears, amongst other things, if_stack and mi_cmacro. */ /* Clears, amongst other things, if_stack and mi_cmacro. */
memset (new, 0, sizeof (cpp_buffer)); memset (new, 0, sizeof (cpp_buffer));
pfile->lexer_pos.output_line = 1;
new->line_base = new->buf = new->cur = buffer; new->line_base = new->buf = new->cur = buffer;
new->rlimit = buffer + length; new->rlimit = buffer + len;
new->prev = buf; new->prev = pfile->buffer;
new->pfile = pfile; new->pfile = pfile;
/* Preprocessed files don't do trigraph and escaped newline processing. */ /* Preprocessed files don't do trigraph and escaped newline processing. */
new->from_stage3 = CPP_OPTION (pfile, preprocessed); new->from_stage3 = CPP_OPTION (pfile, preprocessed);
/* No read ahead or extra char initially. */ /* No read ahead or extra char initially. */
new->read_ahead = EOF; new->read_ahead = EOF;
new->extra_char = EOF; new->extra_char = EOF;
pfile->state.next_bol = 1; pfile->state.next_bol = 1;
pfile->buffer_stack_depth++;
pfile->lexer_pos.output_line = 1;
CPP_BUFFER (pfile) = new; pfile->buffer = new;
return new; return new;
} }
......
...@@ -725,7 +725,7 @@ extern void cpp_undef PARAMS ((cpp_reader *, const char *)); ...@@ -725,7 +725,7 @@ extern void cpp_undef PARAMS ((cpp_reader *, const char *));
extern void cpp_unassert PARAMS ((cpp_reader *, const char *)); extern void cpp_unassert PARAMS ((cpp_reader *, const char *));
extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *, extern cpp_buffer *cpp_push_buffer PARAMS ((cpp_reader *,
const unsigned char *, long)); const unsigned char *, size_t));
extern cpp_buffer *cpp_pop_buffer PARAMS ((cpp_reader *)); extern cpp_buffer *cpp_pop_buffer PARAMS ((cpp_reader *));
extern int cpp_defined PARAMS ((cpp_reader *, const unsigned char *, int)); extern int cpp_defined PARAMS ((cpp_reader *, const unsigned char *, int));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment