{"thread":{"id":"45545","subject":"[PATCH] update-index: fix xgetcwd() related memory leak","startedAt":"2017-03-30T06:22:30Z","lastAt":"2017-03-30T06:23:47Z","messageCount":2,"participants":["Christian Couder","Jeff King"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"315748","messageId":"20170330062208.29018-1-chriscool@tuxfamily.org","threadId":"45545","inReplyTo":null,"subject":"[PATCH] update-index: fix xgetcwd() related memory leak","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2017-03-30T06:22:08Z","receivedAt":"2017-03-30T06:22:30Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"As xgetcwd() returns an allocated buffer, we should free this\nbuffer when we don't need it any more.\n\nThis was found by Coverity.\n\nReported-by: Jeff King <peff@peff.net>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/update-index.c | 6 +++++-\n 1 file changed, 5 insertions(+), 1 deletion(-)\n\ndiff --git a/builtin/update-index.c b/builtin/update-index.c\nindex d74d72cc7f..ebfc09faa0 100644\n--- a/builtin/update-index.c\n+++ b/builtin/update-index.c\n@@ -125,12 +125,16 @@ static int test_if_untracked_cache_is_supported(void)\n \tstruct stat st;\n \tstruct stat_data base;\n \tint fd, ret = 0;\n+\tchar *cwd;\n \n \tstrbuf_addstr(&mtime_dir, \"mtime-test-XXXXXX\");\n \tif (!mkdtemp(mtime_dir.buf))\n \t\tdie_errno(\"Could not make temporary directory\");\n \n-\tfprintf(stderr, _(\"Testing mtime in '%s' \"), xgetcwd());\n+\tcwd = xgetcwd();\n+\tfprintf(stderr, _(\"Testing mtime in '%s' \"), cwd);\n+\tfree(cwd);\n+\n \tatexit(remove_test_directory);\n \txstat_mtime_dir(&st);\n \tfill_stat_data(&base, &st);\n-- \n2.12.2.510.ge1104a5ee5.dirty\n\n"},{"id":"315749","messageId":"20170330062337.rwd2u5k7s57zczuo@sigill.intra.peff.net","threadId":"45545","inReplyTo":"20170330062208.29018-1-chriscool@tuxfamily.org","subject":"Re: [PATCH] update-index: fix xgetcwd() related memory leak","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2017-03-30T06:23:37Z","receivedAt":"2017-03-30T06:23:47Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Thu, Mar 30, 2017 at 08:22:08AM +0200, Christian Couder wrote:\n\n> As xgetcwd() returns an allocated buffer, we should free this\n> buffer when we don't need it any more.\n> \n> This was found by Coverity.\n> \n> Reported-by: Jeff King <peff@peff.net>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/update-index.c | 6 +++++-\n>  1 file changed, 5 insertions(+), 1 deletion(-)\n> \n> diff --git a/builtin/update-index.c b/builtin/update-index.c\n> index d74d72cc7f..ebfc09faa0 100644\n> --- a/builtin/update-index.c\n> +++ b/builtin/update-index.c\n> @@ -125,12 +125,16 @@ static int test_if_untracked_cache_is_supported(void)\n>  \tstruct stat st;\n>  \tstruct stat_data base;\n>  \tint fd, ret = 0;\n> +\tchar *cwd;\n>  \n>  \tstrbuf_addstr(&mtime_dir, \"mtime-test-XXXXXX\");\n>  \tif (!mkdtemp(mtime_dir.buf))\n>  \t\tdie_errno(\"Could not make temporary directory\");\n>  \n> -\tfprintf(stderr, _(\"Testing mtime in '%s' \"), xgetcwd());\n> +\tcwd = xgetcwd();\n> +\tfprintf(stderr, _(\"Testing mtime in '%s' \"), cwd);\n> +\tfree(cwd);\n> +\n\nYep, this solution looks obviously correct to me.\n\n-Peff\n"}]}