git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] mailinfo: use strcmp() for string comparison

From
Jeff King <peff@peff.net>
Date
Jun 2, 2014, 19:34 UTC
Message-ID
<20140602193419.GC2510@sigill.intra.peff.net>
In-Reply-To
<538AEBB8.9070505@web.de>
On Sun, Jun 01, 2014 at 11:00:40AM +0200, René Scharfe wrote:
Show 12 quoted lines
> 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
Previous: René Scharfe
Message 2 of 2 in “mailinfo: use strcmp() for string comparison”
  1. mailinfo: use strcmp() for string comparisonRené Scharfe, Jun 1, 2014
  2. Jeff KingJun 2, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.