Skip to content

Commit

Permalink
fsck: move typename() printing to its own function
Browse files Browse the repository at this point in the history
When an object has a problem, we mention its type. But we do
so by feeding the result of typename() directly to
fprintf(). This is potentially dangerous because typename()
can return NULL for some type values (like OBJ_NONE).

It's doubtful that this can be triggered in practice with
the current code, so this is probably not fixing a bug. But
it future-proofs us against modifications that make things
like OBJ_NONE more likely (and gives future patches a
central point to handle them).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Jan 26, 2017
1 parent c20d4d7 commit 97ca7ca
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions builtin/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ static const char *describe_object(struct object *obj)
return buf.buf;
}

static const char *printable_type(struct object *obj)
{
const char *ret;

ret = typename(obj->type);
if (!ret)
ret = "unknown";

return ret;
}

static int fsck_config(const char *var, const char *value, void *cb)
{
if (strcmp(var, "fsck.skiplist") == 0) {
Expand Down Expand Up @@ -83,7 +94,7 @@ static void objreport(struct object *obj, const char *msg_type,
const char *err)
{
fprintf(stderr, "%s in %s %s: %s\n",
msg_type, typename(obj->type), describe_object(obj), err);
msg_type, printable_type(obj), describe_object(obj), err);
}

static int objerror(struct object *obj, const char *err)
Expand Down Expand Up @@ -114,7 +125,7 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
if (!obj) {
/* ... these references to parent->fld are safe here */
printf("broken link from %7s %s\n",
typename(parent->type), describe_object(parent));
printable_type(parent), describe_object(parent));
printf("broken link from %7s %s\n",
(type == OBJ_ANY ? "unknown" : typename(type)), "unknown");
errors_found |= ERROR_REACHABLE;
Expand All @@ -131,9 +142,9 @@ static int mark_object(struct object *obj, int type, void *data, struct fsck_opt
if (!(obj->flags & HAS_OBJ)) {
if (parent && !has_object_file(&obj->oid)) {
printf("broken link from %7s %s\n",
typename(parent->type), describe_object(parent));
printable_type(parent), describe_object(parent));
printf(" to %7s %s\n",
typename(obj->type), describe_object(obj));
printable_type(obj), describe_object(obj));
errors_found |= ERROR_REACHABLE;
}
return 1;
Expand Down Expand Up @@ -205,7 +216,7 @@ static void check_reachable_object(struct object *obj)
if (!(obj->flags & HAS_OBJ)) {
if (has_sha1_pack(obj->oid.hash))
return; /* it is in pack - forget about it */
printf("missing %s %s\n", typename(obj->type),
printf("missing %s %s\n", printable_type(obj),
describe_object(obj));
errors_found |= ERROR_REACHABLE;
return;
Expand All @@ -231,7 +242,7 @@ static void check_unreachable_object(struct object *obj)
* since this is something that is prunable.
*/
if (show_unreachable) {
printf("unreachable %s %s\n", typename(obj->type),
printf("unreachable %s %s\n", printable_type(obj),
describe_object(obj));
return;
}
Expand All @@ -250,7 +261,7 @@ static void check_unreachable_object(struct object *obj)
*/
if (!obj->used) {
if (show_dangling)
printf("dangling %s %s\n", typename(obj->type),
printf("dangling %s %s\n", printable_type(obj),
describe_object(obj));
if (write_lost_and_found) {
char *filename = git_pathdup("lost-found/%s/%s",
Expand Down Expand Up @@ -324,7 +335,7 @@ static int fsck_obj(struct object *obj)

if (verbose)
fprintf(stderr, "Checking %s %s\n",
typename(obj->type), describe_object(obj));
printable_type(obj), describe_object(obj));

if (fsck_walk(obj, NULL, &fsck_obj_options))
objerror(obj, "broken links");
Expand All @@ -350,7 +361,7 @@ static int fsck_obj(struct object *obj)
struct tag *tag = (struct tag *) obj;

if (show_tags && tag->tagged) {
printf("tagged %s %s", typename(tag->tagged->type),
printf("tagged %s %s", printable_type(tag->tagged),
describe_object(tag->tagged));
printf(" (%s) in %s\n", tag->tag,
describe_object(&tag->object));
Expand Down

0 comments on commit 97ca7ca

Please sign in to comment.