git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mailinfo: use strcmp() for string comparison
@ 2014-06-01  9:00 René Scharfe
  2014-06-02 19:34 ` Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: René Scharfe @ 2014-06-01  9:00 UTC (permalink / raw)
  To: Git Mailing List; +Cc: Junio C Hamano

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>
---
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?

 builtin/mailinfo.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c
index 2c3cd8e..cf11c8d 100644
--- a/builtin/mailinfo.c
+++ b/builtin/mailinfo.c
@@ -334,7 +334,7 @@ static int check_header(const struct strbuf *line,
 	}
 	if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
 		for (i = 0; header[i]; i++) {
-			if (!memcmp("Subject", header[i], 7)) {
+			if (!strcmp("Subject", header[i])) {
 				handle_header(&hdr_data[i], line);
 				ret = 1;
 				goto check_header_out;
@@ -929,13 +929,13 @@ static void handle_info(void)
 		else
 			continue;
 
-		if (!memcmp(header[i], "Subject", 7)) {
+		if (!strcmp(header[i], "Subject")) {
 			if (!keep_subject) {
 				cleanup_subject(hdr);
 				cleanup_space(hdr);
 			}
 			output_header_lines(fout, "Subject", hdr);
-		} else if (!memcmp(header[i], "From", 4)) {
+		} else if (!strcmp(header[i], "From")) {
 			cleanup_space(hdr);
 			handle_from(hdr);
 			fprintf(fout, "Author: %s\n", name.buf);
-- 
2.0.0

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] mailinfo: use strcmp() for string comparison
  2014-06-01  9:00 [PATCH] mailinfo: use strcmp() for string comparison René Scharfe
@ 2014-06-02 19:34 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2014-06-02 19:34 UTC (permalink / raw)
  To: René Scharfe; +Cc: Git Mailing List, Junio C Hamano

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2014-06-02 19:34 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-01  9:00 [PATCH] mailinfo: use strcmp() for string comparison René Scharfe
2014-06-02 19:34 ` Jeff King

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).