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

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

From
René Scharfe <l.s.r@web.de>
Date
Jun 1, 2014, 09:00 UTC
Message-ID
<538AEBB8.9070505@web.de>
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
Next: Jeff King
Message 1 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.