Avoid null pointer dereference in cgit_print_diff().

When calling cgit_print_diff() with a bad new_rev and a NULL old_rev,
checking for new_rev's parent commit will result in a null pointer
dereference. Returning on an invalid commit before dereferencing fixes
this. Spotted with clang-analyzer.

Signed-off-by: Lukas Fleischer <cgit@cryptocrack.de>
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
This commit is contained in:
Lukas Fleischer 2011-04-05 10:38:53 +02:00 committed by Lars Hjemli
parent a0bf375a1a
commit 9afc883297

View File

@ -345,8 +345,10 @@ void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefi
return; return;
} }
commit = lookup_commit_reference(new_rev_sha1); commit = lookup_commit_reference(new_rev_sha1);
if (!commit || parse_commit(commit)) if (!commit || parse_commit(commit)) {
cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1))); cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(new_rev_sha1)));
return;
}
if (old_rev) if (old_rev)
get_sha1(old_rev, old_rev_sha1); get_sha1(old_rev, old_rev_sha1);
@ -362,8 +364,10 @@ void cgit_print_diff(const char *new_rev, const char *old_rev, const char *prefi
return; return;
} }
commit2 = lookup_commit_reference(old_rev_sha1); commit2 = lookup_commit_reference(old_rev_sha1);
if (!commit2 || parse_commit(commit2)) if (!commit2 || parse_commit(commit2)) {
cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1))); cgit_print_error(fmt("Bad commit: %s", sha1_to_hex(old_rev_sha1)));
return;
}
} }
if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff)) if ((ctx.qry.ssdiff && !ctx.cfg.ssdiff) || (!ctx.qry.ssdiff && ctx.cfg.ssdiff))