{"thread":{"id":"42476","subject":"[PATCH] patch-id: use starts_with() and skip_prefix()","startedAt":"2016-05-28T16:20:23Z","lastAt":"2016-05-28T16:20:23Z","messageCount":1,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"287750","messageId":"5749C547.7040305@web.de","threadId":"42476","inReplyTo":null,"subject":"[PATCH] patch-id: use starts_with() and skip_prefix()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2016-05-28T16:20:23Z","receivedAt":"2016-05-28T16:20:23Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Get rid of magic numbers and avoid running over the end of a NUL\nterminated string by using starts_with() and skip_prefix() instead\nof memcmp().\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n builtin/patch-id.c | 23 ++++++++++-------------\n 1 file changed, 10 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/patch-id.c b/builtin/patch-id.c\nindex 366ce5a..a84d000 100644\n--- a/builtin/patch-id.c\n+++ b/builtin/patch-id.c\n@@ -81,16 +81,13 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,\n \n \twhile (strbuf_getwholeline(line_buf, stdin, '\\n') != EOF) {\n \t\tchar *line = line_buf->buf;\n-\t\tchar *p = line;\n+\t\tconst char *p = line;\n \t\tint len;\n \n-\t\tif (!memcmp(line, \"diff-tree \", 10))\n-\t\t\tp += 10;\n-\t\telse if (!memcmp(line, \"commit \", 7))\n-\t\t\tp += 7;\n-\t\telse if (!memcmp(line, \"From \", 5))\n-\t\t\tp += 5;\n-\t\telse if (!memcmp(line, \"\\\\ \", 2) && 12 < strlen(line))\n+\t\tif (!skip_prefix(line, \"diff-tree \", &p) &&\n+\t\t    !skip_prefix(line, \"commit \", &p) &&\n+\t\t    !skip_prefix(line, \"From \", &p) &&\n+\t\t    starts_with(line, \"\\\\ \") && 12 < strlen(line))\n \t\t\tcontinue;\n \n \t\tif (!get_oid_hex(p, next_oid)) {\n@@ -99,14 +96,14 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,\n \t\t}\n \n \t\t/* Ignore commit comments */\n-\t\tif (!patchlen && memcmp(line, \"diff \", 5))\n+\t\tif (!patchlen && !starts_with(line, \"diff \"))\n \t\t\tcontinue;\n \n \t\t/* Parsing diff header?  */\n \t\tif (before == -1) {\n-\t\t\tif (!memcmp(line, \"index \", 6))\n+\t\t\tif (starts_with(line, \"index \"))\n \t\t\t\tcontinue;\n-\t\t\telse if (!memcmp(line, \"--- \", 4))\n+\t\t\telse if (starts_with(line, \"--- \"))\n \t\t\t\tbefore = after = 1;\n \t\t\telse if (!isalpha(line[0]))\n \t\t\t\tbreak;\n@@ -114,14 +111,14 @@ static int get_one_patchid(struct object_id *next_oid, struct object_id *result,\n \n \t\t/* Looking for a valid hunk header?  */\n \t\tif (before == 0 && after == 0) {\n-\t\t\tif (!memcmp(line, \"@@ -\", 4)) {\n+\t\t\tif (starts_with(line, \"@@ -\")) {\n \t\t\t\t/* Parse next hunk, but ignore line numbers.  */\n \t\t\t\tscan_hunk_header(line, &before, &after);\n \t\t\t\tcontinue;\n \t\t\t}\n \n \t\t\t/* Split at the end of the patch.  */\n-\t\t\tif (memcmp(line, \"diff \", 5))\n+\t\t\tif (!starts_with(line, \"diff \"))\n \t\t\t\tbreak;\n \n \t\t\t/* Else we're parsing another header.  */\n-- \n2.8.3\n"}]}