fsck.c:fsck_commit(): use skip_prefix() to verify and skip constant

fsck_commit() uses memcmp() to check if the buffer starts with a certain prefix, and skips the prefix if it does. This is exactly what skip_prefix() was designed for. Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com> Helped-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Yuxuan Shui committed Mar 13, 2014 at 12:45 UTC 2d820a61dfd50d50cafab47f75a507f3e6657e9b
1 file changed +13 -11
fsck.c
+13 -11
@@ -281,7 +281,7 @@ static int fsck_ident(const char **ident, struct object *obj, fsck_error error_f
281
282 static int fsck_commit(struct commit *commit, fsck_error error_func)
283 {
284 - const char *buffer = commit->buffer;
284 + const char *buffer = commit->buffer, *tmp;
285 unsigned char tree_sha1[20], sha1[20];
286 struct commit_graft *graft;
287 int parents = 0;
@@ -290,15 +290,17 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
290 if (commit->date == ULONG_MAX)
291 return error_func(&commit->object, FSCK_ERROR, "invalid author/committer line");
292
293 - if (memcmp(buffer, "tree ", 5))
293 + buffer = skip_prefix(buffer, "tree ");
294 + if (!buffer)
295 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'tree' line");
295 - if (get_sha1_hex(buffer+5, tree_sha1) || buffer[45] != '\n')
296 + if (get_sha1_hex(buffer, tree_sha1) || buffer[40] != '\n')
297 return error_func(&commit->object, FSCK_ERROR, "invalid 'tree' line format - bad sha1");
297 - buffer += 46;
298 - while (!memcmp(buffer, "parent ", 7)) {
299 - if (get_sha1_hex(buffer+7, sha1) || buffer[47] != '\n')
298 + buffer += 41;
299 + while ((tmp = skip_prefix(buffer, "parent "))) {
300 + buffer = tmp;
301 + if (get_sha1_hex(buffer, sha1) || buffer[40] != '\n')
302 return error_func(&commit->object, FSCK_ERROR, "invalid 'parent' line format - bad sha1");
301 - buffer += 48;
303 + buffer += 41;
304 parents++;
305 }
306 graft = lookup_commit_graft(commit->object.sha1);
@@ -322,15 +324,15 @@ static int fsck_commit(struct commit *commit, fsck_error error_func)
324 if (p || parents)
325 return error_func(&commit->object, FSCK_ERROR, "parent objects missing");
326 }
325 - if (memcmp(buffer, "author ", 7))
327 + buffer = skip_prefix(buffer, "author ");
328 + if (!buffer)
329 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'author' line");
327 - buffer += 7;
330 err = fsck_ident(&buffer, &commit->object, error_func);
331 if (err)
332 return err;
331 - if (memcmp(buffer, "committer ", strlen("committer ")))
333 + buffer = skip_prefix(buffer, "committer ");
334 + if (!buffer)
335 return error_func(&commit->object, FSCK_ERROR, "invalid format - expected 'committer' line");
333 - buffer += strlen("committer ");
336 err = fsck_ident(&buffer, &commit->object, error_func);
337 if (err)
338 return err;