public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Martin Sebor <msebor@gmail.com>
To: David Malcolm <dmalcolm@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] Better error messages for merge-conflict markers (v3)
Date: Wed, 09 Dec 2015 23:50:00 -0000	[thread overview]
Message-ID: <5668BE29.7030302@gmail.com> (raw)
In-Reply-To: <1446218187-720-1-git-send-email-dmalcolm@redhat.com>

> @@ -471,6 +471,20 @@ c_parser_peek_2nd_token (c_parser *parser)
>     return &parser->tokens[1];
>   }
>
> +/* Return a pointer to the Nth token from PARSER, reading it
> +   in if necessary.  The N-1th token is already read in.  */
> +
> +static c_token *
> +c_parser_peek_nth_token (c_parser *parser, unsigned int n)
> +{
> +  if (parser->tokens_avail >= n)
> +    return &parser->tokens[n - 1];
> +  gcc_assert (parser->tokens_avail == n - 1);
> +  c_lex_one_token (parser, &parser->tokens[n - 1]);
> +  parser->tokens_avail = n;
> +  return &parser->tokens[n - 1];
> +}

David, I know little about the code in this area and so I looked
at the patch mostly out of curiosity.  This little function caught
my eye for some reason.  I see it's called only in two places and
in a safe way, but it also looks like it could easily be called
unsafely and cause either the assert to fire (when N is greater
than tokens_avail + 1), or a bad address to be returned (when N
is zero).  It's also a third peek function in the C parser,
making the choice not completely trivial (at least to those not
as familiar with the code).

When compared to the equivalent function in the C++ lexer, that
one is more like I would expect.  I.e., it asserts that N is
positive and doesn't assume any specific prior sequence of peeks.
I can see how someone familiar with the C++ lexer but not so well
with the C parser might inadvertently use the new C function
incorrectly.

May I suggest making the C function equivalent to the C++ one
in terms of its preconditions?

Martin

      parent reply	other threads:[~2015-12-09 23:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-30 15:02 David Malcolm
2015-11-02 22:52 ` Jeff Law
2015-11-03  4:05   ` Trevor Saunders
2015-11-04 13:56 ` Bernd Schmidt
2015-12-09 16:39   ` [PATCH] Better error recovery for merge-conflict markers (v4) David Malcolm
2015-12-09 17:44     ` Bernd Schmidt
2015-12-09 20:18       ` Jeff Law
2015-12-16 18:23       ` David Malcolm
2015-12-15 19:11   ` [PATCH] Better error recovery for merge-conflict markers (v5) David Malcolm
2015-12-15 23:52     ` Bernd Schmidt
2015-12-16 18:33       ` David Malcolm
2015-12-09 23:50 ` Martin Sebor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5668BE29.7030302@gmail.com \
    --to=msebor@gmail.com \
    --cc=dmalcolm@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).