Skip to content

Commit 0a01df0

Browse files
committed
Merge branch 'ab/date-mode-release'
Plug (some) memory leaks around parse_date_format(). * ab/date-mode-release: date API: add and use a date_mode_release() date API: add basic API docs date API: provide and use a DATE_MODE_INIT date API: create a date.h, split from cache.h cache.h: remove always unused show_date_human() declaration
2 parents 294f296 + 974c919 commit 0a01df0

20 files changed

+112
-54
lines changed

archive-zip.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "object-store.h"
1010
#include "userdiff.h"
1111
#include "xdiff-interface.h"
12+
#include "date.h"
1213

1314
static int zip_date;
1415
static int zip_time;

builtin/am.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "string-list.h"
3535
#include "packfile.h"
3636
#include "repository.h"
37+
#include "pretty.h"
3738

3839
/**
3940
* Returns the length of the first line of msg.

builtin/commit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "help.h"
3838
#include "commit-reach.h"
3939
#include "commit-graph.h"
40+
#include "pretty.h"
4041

4142
static const char * const builtin_commit_usage[] = {
4243
N_("git commit [<options>] [--] <pathspec>..."),

builtin/fast-import.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "mem-pool.h"
2020
#include "commit-reach.h"
2121
#include "khash.h"
22+
#include "date.h"
2223

2324
#define PACK_ID_BITS 16
2425
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)

builtin/show-branch.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "parse-options.h"
99
#include "dir.h"
1010
#include "commit-slab.h"
11+
#include "date.h"
1112

1213
static const char* show_branch_usage[] = {
1314
N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"

builtin/tag.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "oid-array.h"
2121
#include "column.h"
2222
#include "ref-filter.h"
23+
#include "date.h"
2324

2425
static const char * const git_tag_usage[] = {
2526
N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"

cache.h

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,48 +1558,6 @@ struct object *repo_peel_to_type(struct repository *r,
15581558
#define peel_to_type(name, namelen, obj, type) \
15591559
repo_peel_to_type(the_repository, name, namelen, obj, type)
15601560

1561-
enum date_mode_type {
1562-
DATE_NORMAL = 0,
1563-
DATE_HUMAN,
1564-
DATE_RELATIVE,
1565-
DATE_SHORT,
1566-
DATE_ISO8601,
1567-
DATE_ISO8601_STRICT,
1568-
DATE_RFC2822,
1569-
DATE_STRFTIME,
1570-
DATE_RAW,
1571-
DATE_UNIX
1572-
};
1573-
1574-
struct date_mode {
1575-
enum date_mode_type type;
1576-
const char *strftime_fmt;
1577-
int local;
1578-
};
1579-
1580-
/*
1581-
* Convenience helper for passing a constant type, like:
1582-
*
1583-
* show_date(t, tz, DATE_MODE(NORMAL));
1584-
*/
1585-
#define DATE_MODE(t) date_mode_from_type(DATE_##t)
1586-
struct date_mode *date_mode_from_type(enum date_mode_type type);
1587-
1588-
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
1589-
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
1590-
void show_date_human(timestamp_t time, int tz, const struct timeval *now,
1591-
struct strbuf *timebuf);
1592-
int parse_date(const char *date, struct strbuf *out);
1593-
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
1594-
int parse_expiry_date(const char *date, timestamp_t *timestamp);
1595-
void datestamp(struct strbuf *out);
1596-
#define approxidate(s) approxidate_careful((s), NULL)
1597-
timestamp_t approxidate_careful(const char *, int *);
1598-
timestamp_t approxidate_relative(const char *date);
1599-
void parse_date_format(const char *format, struct date_mode *mode);
1600-
int date_overflows(timestamp_t date);
1601-
time_t tm_to_time_t(const struct tm *tm);
1602-
16031561
#define IDENT_STRICT 1
16041562
#define IDENT_NO_DATE 2
16051563
#define IDENT_NO_NAME 4
@@ -1645,14 +1603,6 @@ struct ident_split {
16451603
*/
16461604
int split_ident_line(struct ident_split *, const char *, int);
16471605

1648-
/*
1649-
* Like show_date, but pull the timestamp and tz parameters from
1650-
* the ident_split. It will also sanity-check the values and produce
1651-
* a well-known sentinel date if they appear bogus.
1652-
*/
1653-
const char *show_ident_date(const struct ident_split *id,
1654-
const struct date_mode *mode);
1655-
16561606
/*
16571607
* Compare split idents for equality or strict ordering. Note that we
16581608
* compare only the ident part of the line, ignoring any timestamp.

config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*
77
*/
88
#include "cache.h"
9+
#include "date.h"
910
#include "branch.h"
1011
#include "config.h"
1112
#include "environment.h"

date.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "cache.h"
8+
#include "date.h"
89

910
/*
1011
* This is like mktime, but without normalization of tm_wday and tm_yday.
@@ -205,11 +206,10 @@ void show_date_relative(timestamp_t time, struct strbuf *timebuf)
205206

206207
struct date_mode *date_mode_from_type(enum date_mode_type type)
207208
{
208-
static struct date_mode mode;
209+
static struct date_mode mode = DATE_MODE_INIT;
209210
if (type == DATE_STRFTIME)
210211
BUG("cannot create anonymous strftime date_mode struct");
211212
mode.type = type;
212-
mode.local = 0;
213213
return &mode;
214214
}
215215

@@ -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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#ifndef DATE_H
2+
#define DATE_H
3+
4+
/**
5+
* The date mode type. This has DATE_NORMAL at an explicit "= 0" to
6+
* accommodate a memset([...], 0, [...]) initialization when "struct
7+
* date_mode" is used as an embedded struct member, as in the case of
8+
* e.g. "struct pretty_print_context" and "struct rev_info".
9+
*/
10+
enum date_mode_type {
11+
DATE_NORMAL = 0,
12+
DATE_HUMAN,
13+
DATE_RELATIVE,
14+
DATE_SHORT,
15+
DATE_ISO8601,
16+
DATE_ISO8601_STRICT,
17+
DATE_RFC2822,
18+
DATE_STRFTIME,
19+
DATE_RAW,
20+
DATE_UNIX
21+
};
22+
23+
struct date_mode {
24+
enum date_mode_type type;
25+
const char *strftime_fmt;
26+
int local;
27+
};
28+
29+
#define DATE_MODE_INIT { \
30+
.type = DATE_NORMAL, \
31+
}
32+
33+
/**
34+
* Convenience helper for passing a constant type, like:
35+
*
36+
* show_date(t, tz, DATE_MODE(NORMAL));
37+
*/
38+
#define DATE_MODE(t) date_mode_from_type(DATE_##t)
39+
struct date_mode *date_mode_from_type(enum date_mode_type type);
40+
41+
/**
42+
* Format <'time', 'timezone'> into static memory according to 'mode'
43+
* and return it. The mode is an initialized "struct date_mode"
44+
* (usually from the DATE_MODE() macro).
45+
*/
46+
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
47+
48+
/**
49+
* Parse a date format for later use with show_date().
50+
*
51+
* When the "date_mode_type" is DATE_STRFTIME the "strftime_fmt"
52+
* member of "struct date_mode" will be a malloc()'d format string to
53+
* be used with strbuf_addftime(), in which case you'll need to call
54+
* date_mode_release() later.
55+
*/
56+
void parse_date_format(const char *format, struct date_mode *mode);
57+
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+
64+
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
65+
int parse_date(const char *date, struct strbuf *out);
66+
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
67+
int parse_expiry_date(const char *date, timestamp_t *timestamp);
68+
void datestamp(struct strbuf *out);
69+
#define approxidate(s) approxidate_careful((s), NULL)
70+
timestamp_t approxidate_careful(const char *, int *);
71+
timestamp_t approxidate_relative(const char *date);
72+
int date_overflows(timestamp_t date);
73+
time_t tm_to_time_t(const struct tm *tm);
74+
#endif

0 commit comments

Comments
 (0)