From fcd12db6af118b70b5c15cf5fdd6800eeecc370a Mon Sep 17 00:00:00 2001 From: Jeff King Date: Mon, 10 Aug 2015 05:35:31 -0400 Subject: 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 Signed-off-by: Junio C Hamano --- notes-merge.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'notes-merge.c') diff --git a/notes-merge.c b/notes-merge.c index 0b2b82c41f..b3d1dab51f 100644 --- a/notes-merge.c +++ b/notes-merge.c @@ -295,7 +295,7 @@ static void write_buf_to_worktree(const unsigned char *obj, const char *buf, unsigned long size) { int fd; - const char *path = git_path(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj)); + char *path = git_pathdup(NOTES_MERGE_WORKTREE "/%s", sha1_to_hex(obj)); if (safe_create_leading_directories_const(path)) die_errno("unable to create directory for '%s'", path); if (file_exists(path)) @@ -320,6 +320,7 @@ static void write_buf_to_worktree(const unsigned char *obj, } close(fd); + free(path); } static void write_note_to_worktree(const unsigned char *obj, -- cgit v1.2.1