summaryrefslogtreecommitdiff
path: root/builtin/fsck.c
diff options
context:
space:
mode:
authorLibravatar Jeff King <peff@peff.net>2015-08-10 05:35:31 -0400
committerLibravatar Junio C Hamano <gitster@pobox.com>2015-08-10 15:37:12 -0700
commitfcd12db6af118b70b5c15cf5fdd6800eeecc370a (patch)
tree8050bcc9515081c8a6a52285297fe23f0bf08916 /builtin/fsck.c
parentadd_to_alternates_file: don't add duplicate entries (diff)
downloadtgif-fcd12db6af118b70b5c15cf5fdd6800eeecc370a.tar.xz
prefer git_pathdup to git_path in some possibly-dangerous cases
Because git_path uses a static buffer that is shared with calls to git_path, mkpath, etc, it can be dangerous to assign the result to a variable or pass it to a non-trivial function. The value may change unexpectedly due to other calls. None of the cases changed here has a known bug, but they're worth converting away from git_path because: 1. It's easy to use git_pathdup in these cases. 2. They use constructs (like assignment) that make it hard to tell whether they're safe or not. The extra malloc overhead should be trivial, as an allocation should be an order of magnitude cheaper than a system call (which we are clearly about to make, since we are constructing a filename). The real cost is that we must remember to free the result. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin/fsck.c')
-rw-r--r--builtin/fsck.c4
1 files changed, 3 insertions, 1 deletions
diff --git a/builtin/fsck.c b/builtin/fsck.c
index f4b87e9b33..079470342f 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -243,13 +243,14 @@ static void check_unreachable_object(struct object *obj)
printf("dangling %s %s\n", typename(obj->type),
sha1_to_hex(obj->sha1));
if (write_lost_and_found) {
- const char *filename = git_path("lost-found/%s/%s",
+ char *filename = git_pathdup("lost-found/%s/%s",
obj->type == OBJ_COMMIT ? "commit" : "other",
sha1_to_hex(obj->sha1));
FILE *f;
if (safe_create_leading_directories_const(filename)) {
error("Could not create lost-found");
+ free(filename);
return;
}
if (!(f = fopen(filename, "w")))
@@ -262,6 +263,7 @@ static void check_unreachable_object(struct object *obj)
if (fclose(f))
die_errno("Could not finish '%s'",
filename);
+ free(filename);
}
return;
}