Skip to content

Commit 974c919

Browse files
avargitster
authored andcommitted
date API: add and use a date_mode_release()
Fix a memory leak in the parse_date_format() function by providing a new date_mode_release() companion function. By using this in "t/helper/test-date.c" we can mark the "t0006-date.sh" test as passing when git is compiled with SANITIZE=leak, and whitelist it to run under "GIT_TEST_PASSING_SANITIZE_LEAK=true" by adding "TEST_PASSES_SANITIZE_LEAK=true" to the test itself. The other tests that expose this memory leak (i.e. take the "mode->type == DATE_STRFTIME" branch in parse_date_format()) are "t6300-for-each-ref.sh" and "t7004-tag.sh". The former is due to an easily fixed leak in "ref-filter.c", and brings the failures in "t6300-for-each-ref.sh" down from 51 to 48. Fixing the remaining leaks will have to wait until there's a release_revisions() in "revision.c", as they have to do with leaks via "struct rev_info". There is also a leak in "builtin/blame.c" due to its call to parse_date_format() to parse the "blame.date" configuration. However as it declares a file-level "static struct date_mode blame_date_mode" to track the data, LSAN will not report it as a leak. It's possible to get valgrind(1) to complain about it with e.g.: valgrind --leak-check=full --show-leak-kinds=all ./git -P -c blame.date=format:%Y blame README.md But let's focus on things LSAN complains about, and are thus observable with "TEST_PASSES_SANITIZE_LEAK=true". We should get to fixing memory leaks in "builtin/blame.c", but as doing so would require some re-arrangement of cmd_blame() let's leave it for some other time. Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2bacb83 commit 974c919

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

date.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,11 @@ void parse_date_format(const char *format, struct date_mode *mode)
993993
die("unknown date format %s", format);
994994
}
995995

996+
void date_mode_release(struct date_mode *mode)
997+
{
998+
free((char *)mode->strftime_fmt);
999+
}
1000+
9961001
void datestamp(struct strbuf *out)
9971002
{
9981003
time_t now;

date.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,17 @@ const char *show_date(timestamp_t time, int timezone, const struct date_mode *mo
5050
*
5151
* When the "date_mode_type" is DATE_STRFTIME the "strftime_fmt"
5252
* member of "struct date_mode" will be a malloc()'d format string to
53-
* be used with strbuf_addftime().
53+
* be used with strbuf_addftime(), in which case you'll need to call
54+
* date_mode_release() later.
5455
*/
5556
void parse_date_format(const char *format, struct date_mode *mode);
5657

58+
/**
59+
* Release a "struct date_mode", currently only required if
60+
* parse_date_format() has parsed a "DATE_STRFTIME" format.
61+
*/
62+
void date_mode_release(struct date_mode *mode);
63+
5764
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
5865
int parse_date(const char *date, struct strbuf *out);
5966
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);

ref-filter.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
12761276
goto bad;
12771277
v->s = xstrdup(show_date(timestamp, tz, &date_mode));
12781278
v->value = timestamp;
1279+
date_mode_release(&date_mode);
12791280
return;
12801281
bad:
12811282
v->s = xstrdup("");

t/helper/test-date.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ static void show_dates(const char **argv, const char *format)
5454

5555
printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
5656
}
57+
58+
date_mode_release(&mode);
5759
}
5860

5961
static void parse_dates(const char **argv)

t/t0006-date.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#!/bin/sh
22

33
test_description='test date parsing and printing'
4+
5+
TEST_PASSES_SANITIZE_LEAK=true
46
. ./test-lib.sh
57

68
# arbitrary reference time: 2009-08-30 19:20:00

0 commit comments

Comments
 (0)