From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Git Mailing List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] mailinfo: use strcmp() for string comparison
Date: Mon, 2 Jun 2014 15:34:19 -0400 [thread overview]
Message-ID: <20140602193419.GC2510@sigill.intra.peff.net> (raw)
In-Reply-To: <538AEBB8.9070505@web.de>
On Sun, Jun 01, 2014 at 11:00:40AM +0200, René Scharfe wrote:
> The array header is defined as:
>
> static const char *header[MAX_HDR_PARSED] = {
> "From","Subject","Date",
> };
>
> When looking for the index of a specfic string in that array, simply
> use strcmp() instead of memcmp(). This avoids running over the end of
> the string (e.g. with memcmp("Subject", "From", 7)) and gets rid of
> magic string length constants.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
Looks correct to me.
> ---
> This is a minimal fix. A good question, however, would be: Why do we
> keep on looking up constant strings in a (short) constant string array
> anyway?
Yeah, this code is quite confusing. I suspect it would be more readable
to unroll any loops over the header array into a series of function
calls or even just cascading if/else. Some of the sites (e.g.,
check_header) already have a mix, like:
for (i = 0; header[i]; i++)
if (cmp_header(line, header[i]))
... do something for this header ...
if (cmp_header(line, "some-other-header"))
... do something special for this header type ...
else if (cmp_header(line, "another"))
... and something else again ...
The looping is not really helping much there in the first place, since
it is not dealing with half of the headers. And then adding on top that
the loop has its own special cases found by comparing the string to
"Subject", I think it would be simpler to just unroll it.
That's just from a quick 5-minute read of the code, though. This isn't
an area I'm very familiar with, so maybe the refactor would get ugly.
-Peff
prev parent reply other threads:[~2014-06-02 19:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-06-01 9:00 [PATCH] mailinfo: use strcmp() for string comparison René Scharfe
2014-06-02 19:34 ` Jeff King [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=20140602193419.GC2510@sigill.intra.peff.net \
--to=peff@peff.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
/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).