From: Eric Sunshine <sunshine@sunshineco.com>
To: Jeff King <peff@peff.net>
Cc: Hamza Mahfooz <someguy@effective-light.com>,
Git List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH v5 2/2] pretty: colorize pattern matches in commit messages
Date: Fri, 17 Sep 2021 18:15:46 -0400 [thread overview]
Message-ID: <CAPig+cTD22MM1vjXMSjVcNXfVWfqDgT7rDp1j-e_Fko33osATA@mail.gmail.com> (raw)
In-Reply-To: <YUUQA1qNCzY7Vx+j@coredump.intra.peff.net>
On Fri, Sep 17, 2021 at 6:01 PM Jeff King <peff@peff.net> wrote:
> On Fri, Sep 17, 2021 at 05:14:56PM -0400, Hamza Mahfooz wrote:
> > On Fri, Sep 17 2021 at 03:10:12 AM -0400, Eric Sunshine
> > <sunshine@sunshineco.com> wrote:
> > > `buf` and `eol` seem like an accident waiting to happen...
> > > ... because strbuf_grow() may reallocate the underlying buffer, which
> > > means that `buf` and `eol` will end up pointing at freed memory, which
> > > will be accessed by the next call to grep_next_match().
> >
> > I don't see how it's problematic, since `tmp_sb` isn't modified after `buf`
> > is initialized (until strbuf_release() is called, of course).
>
> Yes, you are correct.
Indeed, I got confused by the multiple strbufs with similar names. However...
> However, I do think the code would be much clearer
> if you skipped the strbuf entirely, like:
>
> line_as_string = xmemdupz(line, linelen);
> ...
> buf = line_as_string;
> eol = buf + linelen;
>
> which makes it much clearer that you don't intend to modify it further
> (especially with all those other calls operating on the _other_ strbuf,
> it's hard to see immediately that this is the case).
>
> The "as_string" name is assuming the purpose is to get a NUL-terminated
> string. I'm not sure why we need one, though, since we pass the buf/eol
> pointers to grep_next_match(). A comment above the xmemdupz() line might
> be a good place to explain that (which would help especially if the
> reasons change later and we can get rid of it).
... as Peff points out, it indeed is unclear why a copy of `line` is
needed at all. It seems like a simple:
buf = line;
eol = buf + linelen;
would more than suffice without making any copies. If you do that, then the:
if (buf != line) {
later in the code -- and which I questioned in my review as making no
sense -- suddenly makes sense.
prev parent reply other threads:[~2021-09-17 22:16 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-16 14:09 [PATCH v5 1/2] grep: refactor next_match() and match_one_pattern() for external use Hamza Mahfooz
2021-09-16 14:09 ` [PATCH v5 2/2] pretty: colorize pattern matches in commit messages Hamza Mahfooz
2021-09-17 7:10 ` Eric Sunshine
2021-09-17 21:14 ` Hamza Mahfooz
2021-09-17 22:00 ` Jeff King
2021-09-17 22:15 ` Eric Sunshine [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=CAPig+cTD22MM1vjXMSjVcNXfVWfqDgT7rDp1j-e_Fko33osATA@mail.gmail.com \
--to=sunshine@sunshineco.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=peff@peff.net \
--cc=someguy@effective-light.com \
/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 NNTP newsgroup(s).