{"thread":{"id":"37670","subject":"[PATCH] bundle: plug minor memory leak in is_tag_in_date_range()","startedAt":"2014-10-03T22:40:24Z","lastAt":"2014-10-03T23:40:45Z","messageCount":2,"participants":["René Scharfe","Jonathan Nieder"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"250237","messageId":"542F25D8.1000900@web.de","threadId":"37670","inReplyTo":null,"subject":"[PATCH] bundle: plug minor memory leak in is_tag_in_date_range()","fromName":"René Scharfe","fromEmail":"l.s.r@web.de","sentAt":"2014-10-03T22:40:24Z","receivedAt":"2014-10-03T22:40:24Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Free the buffer returned by read_sha1_file() even if no valid tagger\nline is found.\n\nSigned-off-by: Rene Scharfe <l.s.r@web.de>\n---\n bundle.c | 16 ++++++++++------\n 1 file changed, 10 insertions(+), 6 deletions(-)\n\ndiff --git a/bundle.c b/bundle.c\nindex b2b89fe..9ed865c 100644\n--- a/bundle.c\n+++ b/bundle.c\n@@ -211,24 +211,28 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)\n \tenum object_type type;\n \tchar *buf, *line, *lineend;\n \tunsigned long date;\n+\tint result = 1;\n \n \tif (revs->max_age == -1 && revs->min_age == -1)\n-\t\treturn 1;\n+\t\tgoto out;\n \n \tbuf = read_sha1_file(tag->sha1, &type, &size);\n \tif (!buf)\n-\t\treturn 1;\n+\t\tgoto out;\n \tline = memmem(buf, size, \"\\ntagger \", 8);\n \tif (!line++)\n-\t\treturn 1;\n+\t\tgoto out_free;\n \tlineend = memchr(line, '\\n', buf + size - line);\n \tline = memchr(line, '>', lineend ? lineend - line : buf + size - line);\n \tif (!line++)\n-\t\treturn 1;\n+\t\tgoto out_free;\n \tdate = strtoul(line, NULL, 10);\n-\tfree(buf);\n-\treturn (revs->max_age == -1 || revs->max_age < date) &&\n+\tresult = (revs->max_age == -1 || revs->max_age < date) &&\n \t\t(revs->min_age == -1 || revs->min_age > date);\n+out_free:\n+\tfree(buf);\n+out:\n+\treturn result;\n }\n \n int create_bundle(struct bundle_header *header, const char *path,\n-- \n2.1.2\n"},{"id":"250239","messageId":"20141003234045.GU1175@google.com","threadId":"37670","inReplyTo":"542F25D8.1000900@web.de","subject":"Re: [PATCH] bundle: plug minor memory leak in is_tag_in_date_range()","fromName":"Jonathan Nieder","fromEmail":"jrnieder@gmail.com","sentAt":"2014-10-03T23:40:45Z","receivedAt":"2014-10-03T23:40:45Z","isPatch":true,"sender":{"key":"jrnieder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/281595?v=4"},"body":"René Scharfe wrote:\n\n> --- a/bundle.c\n> +++ b/bundle.c\n> @@ -211,24 +211,28 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)\n>  \tenum object_type type;\n>  \tchar *buf, *line, *lineend;\n\nIf buf is initialized to NULL, there is no need for separate out and\nout_free labels.\n\nWith or without such a change,\nReviewed-by: Jonathan Nieder <jrnieder@gmail.com>\n\ndiff --git i/bundle.c w/bundle.c\nindex 4158e11..101cde0 100644\n--- i/bundle.c\n+++ w/bundle.c\n@@ -209,7 +209,7 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)\n {\n \tunsigned long size;\n \tenum object_type type;\n-\tchar *buf, *line, *lineend;\n+\tchar *buf = NULL, *line, *lineend;\n \tunsigned long date;\n \tint result = 1;\n \n@@ -221,17 +221,16 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)\n \t\tgoto out;\n \tline = memmem(buf, size, \"\\ntagger \", 8);\n \tif (!line++)\n-\t\tgoto out_free;\n+\t\tgoto out;\n \tlineend = memchr(line, '\\n', buf + size - line);\n \tline = memchr(line, '>', lineend ? lineend - line : buf + size - line);\n \tif (!line++)\n-\t\tgoto out_free;\n+\t\tgoto out;\n \tdate = strtoul(line, NULL, 10);\n \tresult = (revs->max_age == -1 || revs->max_age < date) &&\n \t\t(revs->min_age == -1 || revs->min_age > date);\n-out_free:\n-\tfree(buf);\n out:\n+\tfree(buf);\n \treturn result;\n }\n \n"}]}