@matheusbsbs / matheus-bagliano / commits / 7c3fd25dcf
Fork 0

Make builtin-commit.c more careful about parenthood

When creating the commit object, be a whole lot more careful about making sure that the parent lines really are valid parent lines. Check things like MERGE_HEAD having proper SHA1 lines in it, and double-check that all the parents exist and are actually commits. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Linus Torvalds committed Jan 15, 2008 at 16:12 UTC 7c3fd25dcf2a23ed43bae2ba23a46edab4644a9f
1 file changed +19 -5
builtin-commit.c
+19 -5
@@ -756,6 +756,17 @@ static const char commit_utf8_warn[] =
756 "You may want to amend it after fixing the message, or set the config\n"
757 "variable i18n.commitencoding to the encoding your project uses.\n";
758
759 +static void add_parent(struct strbuf *sb, const unsigned char *sha1)
760 +{
761 + struct object *obj = parse_object(sha1);
762 + const char *parent = sha1_to_hex(sha1);
763 + if (!obj)
764 + die("Unable to find commit parent %s", parent);
765 + if (obj->type != OBJ_COMMIT)
766 + die("Parent %s isn't a proper commit", parent);
767 + strbuf_addf(sb, "parent %s\n", parent);
768 +}
769 +
770 int cmd_commit(int argc, const char **argv, const char *prefix)
771 {
772 int header_len;
@@ -818,21 +829,24 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
829 die("could not parse HEAD commit");
830
831 for (c = commit->parents; c; c = c->next)
821 - strbuf_addf(&sb, "parent %s\n",
822 - sha1_to_hex(c->item->object.sha1));
832 + add_parent(&sb, c->item->object.sha1);
833 } else if (in_merge) {
834 struct strbuf m;
835 FILE *fp;
836
837 reflog_msg = "commit (merge)";
828 - strbuf_addf(&sb, "parent %s\n", sha1_to_hex(head_sha1));
838 + add_parent(&sb, head_sha1);
839 strbuf_init(&m, 0);
840 fp = fopen(git_path("MERGE_HEAD"), "r");
841 if (fp == NULL)
842 die("could not open %s for reading: %s",
843 git_path("MERGE_HEAD"), strerror(errno));
834 - while (strbuf_getline(&m, fp, '\n') != EOF)
835 - strbuf_addf(&sb, "parent %s\n", m.buf);
844 + while (strbuf_getline(&m, fp, '\n') != EOF) {
845 + unsigned char sha1[20];
846 + if (get_sha1_hex(m.buf, sha1) < 0)
847 + die("Corrupt MERGE_HEAD file (%s)", m.buf);
848 + add_parent(&sb, sha1);
849 + }
850 fclose(fp);
851 strbuf_release(&m);
852 } else {