log: fix memory leak in open_next_file()

Noticed-by: Jeff King <peff@peff.net> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed May 3, 2017 at 17:16 UTC 15d980a785f8c962bc032df40cb42fdc269c9dc6
1 file changed +8 -4
builtin/log.c
+8 -4
@@ -842,8 +842,10 @@ static int open_next_file(struct commit *commit, const char *subject,
842 if (output_directory) {
843 strbuf_addstr(&filename, output_directory);
844 if (filename.len >=
845 - PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len)
845 + PATH_MAX - FORMAT_PATCH_NAME_MAX - suffix_len) {
846 + strbuf_release(&filename);
847 return error(_("name of output directory is too long"));
848 + }
849 strbuf_complete(&filename, '/');
850 }
851
@@ -857,9 +859,11 @@ static int open_next_file(struct commit *commit, const char *subject,
859 if (!quiet)
860 printf("%s\n", filename.buf + outdir_offset);
861
860 - if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL)
861 - return error_errno(_("Cannot open patch file %s"),
862 - filename.buf);
862 + if ((rev->diffopt.file = fopen(filename.buf, "w")) == NULL) {
863 + error_errno(_("Cannot open patch file %s"), filename.buf);
864 + strbuf_release(&filename);
865 + return -1;
866 + }
867
868 strbuf_release(&filename);
869 return 0;