{"thread":{"id":"1114","subject":"[PATCH] sha1_file.c;prepare_packed_git_one() - fix DIR leak","startedAt":"2005-07-06T06:52:17Z","lastAt":"2005-07-06T08:11:52Z","messageCount":2,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"5687","messageId":"7voe9gxw7y.fsf@assigned-by-dhcp.cox.net","threadId":"1114","inReplyTo":null,"subject":"[PATCH] sha1_file.c;prepare_packed_git_one() - fix DIR leak","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-07-06T06:52:17Z","receivedAt":"2005-07-06T06:52:17Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"The function calls opendir() without a matching closedir().\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n sha1_file.c |    1 +\n 1 files changed, 1 insertions(+), 0 deletions(-)\n\n7a47797264a0be7955f3422a6e79e3311ab2775f\ndiff --git a/sha1_file.c b/sha1_file.c\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -463,6 +463,7 @@ static void prepare_packed_git_one(char \n \t\tp->next = packed_git;\n \t\tpacked_git = p;\n \t}\n+\tclosedir(dir);\n }\n \n void prepare_packed_git(void)\n"},{"id":"5699","messageId":"7vfyusxsjb.fsf@assigned-by-dhcp.cox.net","threadId":"1114","inReplyTo":"7voe9gxw7y.fsf@assigned-by-dhcp.cox.net","subject":"[PATCH] clone-pack.c:write_one_ref() - Create leading directories.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-07-06T08:11:52Z","receivedAt":"2005-07-06T08:11:52Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"The function write_one_ref() is passed the list of refs received\nfrom the other end, which was obtained by directory traversal\nunder $GIT_DIR/refs; this can contain paths other than what\ngit-init-db prepares and would fail to clone when there is\nsuch.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n cache.h      |    2 ++\n clone-pack.c |    5 ++++-\n sha1_file.c  |   19 +++++++++++++++++++\n 3 files changed, 25 insertions(+), 1 deletions(-)\n\ndfad9e5e585c1c9e1eaf599878f040f7ae519b18\ndiff --git a/cache.h b/cache.h\n--- a/cache.h\n+++ b/cache.h\n@@ -161,6 +161,8 @@ extern void rollback_index_file(struct c\n extern char *git_path(const char *fmt, ...);\n extern char *sha1_file_name(const unsigned char *sha1);\n \n+int safe_create_leading_directories(char *path);\n+\n /* Read and unpack a sha1 file into memory, write memory to a sha1 file */\n extern void * map_sha1_file(const unsigned char *sha1, unsigned long *size);\n extern int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size);\ndiff --git a/clone-pack.c b/clone-pack.c\n--- a/clone-pack.c\n+++ b/clone-pack.c\n@@ -68,9 +68,12 @@ static int is_master(struct ref *ref)\n static void write_one_ref(struct ref *ref)\n {\n \tchar *path = git_path(ref->name);\n-\tint fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);\n+\tint fd;\n \tchar *hex;\n \n+\tif (safe_create_leading_directories(path))\n+\t\tdie(\"unable to create leading directory for %s\", ref->name);\n+\tfd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);\n \tif (fd < 0)\n \t\tdie(\"unable to create ref %s\", ref->name);\n \thex = sha1_to_hex(ref->sha1);\ndiff --git a/sha1_file.c b/sha1_file.c\n--- a/sha1_file.c\n+++ b/sha1_file.c\n@@ -130,6 +130,25 @@ char *git_path(const char *fmt, ...)\n \treturn ret;\n }\n \n+int safe_create_leading_directories(char *path)\n+{\n+\tchar *pos = path;\n+\n+\twhile (pos) {\n+\t\tpos = strchr(pos, '/');\n+\t\tif (!pos)\n+\t\t\tbreak;\n+\t\t*pos = 0;\n+\t\tif (mkdir(path, 0777) < 0)\n+\t\t\tif (errno != EEXIST) {\n+\t\t\t\t*pos = '/';\n+\t\t\t\treturn -1;\n+\t\t\t}\n+\t\t*pos++ = '/';\n+\t}\n+\treturn 0;\n+}\n+\n int get_sha1(const char *str, unsigned char *sha1)\n {\n \tstatic const char *prefix[] = {\n"}]}