{"thread":{"id":"36226","subject":"[PATCHv2] fsck.c:fsck_commit() use skip_prefix() and starts_with()","startedAt":"2014-03-19T13:44:33Z","lastAt":"2014-03-21T00:24:23Z","messageCount":2,"participants":["Othman Darraz","Eric Sunshine"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"237072","messageId":"1395236673-8936-1-git-send-email-darrazo16@gmail.com","threadId":"36226","inReplyTo":null,"subject":"[PATCHv2] fsck.c:fsck_commit() use skip_prefix() and starts_with()","fromName":"Othman Darraz","fromEmail":"darrazo16@gmail.com","sentAt":"2014-03-19T13:44:33Z","receivedAt":"2014-03-19T13:44:33Z","isPatch":false,"sender":{"key":"darrazo16@gmail.com","avatar":null},"body":"make buffer const char* because the content of the memory referenced by this variable doesn't change in this function. \nAdd cast to buffer to match fsck_ident signature which is (char**,...)\n\nuse skip_prefix() instead of memcmp() to avoid using magic number.\n\nSigned-off-by: Othman Darraz <darrazo16@gmail.com>\n---\nI am planning to apply for GSOC2014\n fsck.c | 27 ++++++++++++++-------------\n 1 file changed, 14 insertions(+), 13 deletions(-)\n\ndiff --git a/fsck.c b/fsck.c\nindex 5eae856..128d426 100644\n--- a/fsck.c\n+++ b/fsck.c\n@@ -284,21 +284,22 @@ static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)\n \n static int fsck_commit(struct commit *commit, fsck_error error_func)\n {\n-\tchar *buffer = commit->buffer;\n+\tconst char *buffer = commit->buffer;\n \tunsigned char tree_sha1[20], sha1[20];\n \tstruct commit_graft *graft;\n \tint parents = 0;\n \tint err;\n-\n-\tif (starts_with(buffer, \"tree \"))\n+\tbuffer = skip_prefix(buffer, \"tree \");\n+\tif (!buffer)\n \t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'tree' line\");\n-\tif (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\\n')() \n+\tif (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\\n')\n \t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid 'tree' line format - bad sha1\");\n-\tbuffer += 46;\n-\twhile (!memcmp(buffer, \"parent \", 7)) {\n-\t\tif (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\\n')\n+\tbuffer += 41;\n+\twhile (starts_with(buffer, \"parent \")) {\n+\t\tbuffer = skip_prefix(buffer, \"parent \");\n+\t\tif (get_sha1_hex(buffer, sha1) || buffer[40] != '\\n')\n \t\t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid 'parent' line format - bad sha1\");\n-\t\tbuffer += 48;\n+\t\tbuffer += 41;\n \t\tparents++;\n \t}\n \tgraft = lookup_commit_graft(commit->object.sha1);\n@@ -322,16 +323,16 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)\n \t\tif (p || parents)\n \t\t\treturn error_func(&commit->object, FSCK_ERROR, \"parent objects missing\");\n \t}\n-\tif (starts_with(buffer, \"author \"))\n+\tbuffer = skip_prefix(buffer, \"author \");\n+\tif (!buffer)\n \t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'author' line\");\n-\tbuffer += 7;\n-\terr = fsck_ident(&buffer, &commit->object, error_func);\n+\terr = fsck_ident((char **)&buffer, &commit->object, error_func);\n \tif (err)\n \t\treturn err;\n-\tbuffer = (char* )skip_prefix(buffer,\"committer \");\n+\tbuffer = skip_prefix(buffer,\"committer \");\n \tif (!buffer)\n \t\treturn error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'committer' line\");\n-\terr = fsck_ident(&buffer, &commit->object, error_func);\n+\terr = fsck_ident((char **)&buffer, &commit->object, error_func);\n \tif (err)\n \t\treturn err;\n \tif (!commit->tree)\n-- \n1.9.0.258.g00eda23.dirty\n"},{"id":"237245","messageId":"CAPig+cQCn+mzauXHpwMUZjtVhai5cJ=8PC5rr5P3bJLXzWeSpQ@mail.gmail.com","threadId":"36226","inReplyTo":"1395236673-8936-1-git-send-email-darrazo16@gmail.com","subject":"Re: [PATCHv2] fsck.c:fsck_commit() use skip_prefix() and starts_with()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2014-03-21T00:24:23Z","receivedAt":"2014-03-21T00:24:23Z","isPatch":false,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Mar 19, 2014 at 9:44 AM, Othman Darraz <darrazo16@gmail.com> wrote:\n> Subject: fsck.c:fsck_commit() use skip_prefix() and starts_with()\n\nAdd a space after the colon. Add a colon after fsck_commit().\n\n> make buffer const char* because the content of the memory referenced by this variable doesn't change in this function.\n\nWrap lines to 65-70 characters. Capitalize start of sentence. You\ndon't need to spell out 'const char*'. It's okay to say that you're\njust making it 'const'.\n\nConceptually, this is a distinct change, so it deserves its own patch.\nThus, your submission should become a two-patch series. See\ndocumentation for \"git format-patch\" and \"git send-email\" to learn how\nto create a multi-part patch series.\n\n> Add cast to buffer to match fsck_ident signature which is (char**,...)\n\nBut, is _this_ cast really needed? In my earlier review, I asked if\nyou could drop the cast you had in that attempt by making 'buffer'\nconst. I also mentioned that you might have to fix some small fallout\nfrom making it const. That was a hint that you should dig a bit\ndeeper. It is possible to eliminate the casts. See if you can figure\nout how.\n\n> use skip_prefix() instead of memcmp() to avoid using magic number.\n\nCapitalize start of sentence. You might want to explain a bit that\nskip_prefix() conveys more clearly than memcmp() that the code is\nchecking the string for a prefix. An added benefit is that it allows\nyou to eliminate some magic numbers.\n\n> Signed-off-by: Othman Darraz <darrazo16@gmail.com>\n> ---\n> I am planning to apply for GSOC2014\n>  fsck.c | 27 ++++++++++++++-------------\n>  1 file changed, 14 insertions(+), 13 deletions(-)\n>\n> diff --git a/fsck.c b/fsck.c\n> index 5eae856..128d426 100644\n> --- a/fsck.c\n> +++ b/fsck.c\n> @@ -284,21 +284,22 @@ static int fsck_ident(char **ident, struct object *obj, fsck_error error_func)\n>\n>  static int fsck_commit(struct commit *commit, fsck_error error_func)\n>  {\n> -       char *buffer = commit->buffer;\n> +       const char *buffer = commit->buffer;\n>         unsigned char tree_sha1[20], sha1[20];\n>         struct commit_graft *graft;\n>         int parents = 0;\n>         int err;\n> -\n> -       if (starts_with(buffer, \"tree \"))\n> +       buffer = skip_prefix(buffer, \"tree \");\n\nUnfortunately, this patch is quite broken since it is based upon your\nprevious patch rather than upon a clean copy of fsck.c. I'll abort the\nreview at this point since this and your previous attempt are\nintermingled, and the patch does not provide a clear picture of how\nthe original fsck.c gets changed.\n\n> +       if (!buffer)\n>                 return error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'tree' line\");\n> -       if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\\n')()\n> +       if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\\n')\n>                 return error_func(&commit->object, FSCK_ERROR, \"invalid 'tree' line format - bad sha1\");\n> -       buffer += 46;\n> -       while (!memcmp(buffer, \"parent \", 7)) {\n> -               if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\\n')\n> +       buffer += 41;\n> +       while (starts_with(buffer, \"parent \")) {\n> +               buffer = skip_prefix(buffer, \"parent \");\n> +               if (get_sha1_hex(buffer, sha1) || buffer[40] != '\\n')\n>                         return error_func(&commit->object, FSCK_ERROR, \"invalid 'parent' line format - bad sha1\");\n> -               buffer += 48;\n> +               buffer += 41;\n>                 parents++;\n>         }\n>         graft = lookup_commit_graft(commit->object.sha1);\n> @@ -322,16 +323,16 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)\n>                 if (p || parents)\n>                         return error_func(&commit->object, FSCK_ERROR, \"parent objects missing\");\n>         }\n> -       if (starts_with(buffer, \"author \"))\n> +       buffer = skip_prefix(buffer, \"author \");\n> +       if (!buffer)\n>                 return error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'author' line\");\n> -       buffer += 7;\n> -       err = fsck_ident(&buffer, &commit->object, error_func);\n> +       err = fsck_ident((char **)&buffer, &commit->object, error_func);\n>         if (err)\n>                 return err;\n> -       buffer = (char* )skip_prefix(buffer,\"committer \");\n> +       buffer = skip_prefix(buffer,\"committer \");\n>         if (!buffer)\n>                 return error_func(&commit->object, FSCK_ERROR, \"invalid format - expected 'committer' line\");\n> -       err = fsck_ident(&buffer, &commit->object, error_func);\n> +       err = fsck_ident((char **)&buffer, &commit->object, error_func);\n>         if (err)\n>                 return err;\n>         if (!commit->tree)\n> --\n> 1.9.0.258.g00eda23.dirty\n"}]}