1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-10 11:56:09 +02:00

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
This commit is contained in:
Junio C Hamano 2022-02-25 15:47:36 -08:00
commit 0a01df08c0
20 changed files with 112 additions and 54 deletions

View File

@ -9,6 +9,7 @@
#include "object-store.h"
#include "userdiff.h"
#include "xdiff-interface.h"
#include "date.h"
static int zip_date;
static int zip_time;

View File

@ -34,6 +34,7 @@
#include "string-list.h"
#include "packfile.h"
#include "repository.h"
#include "pretty.h"
/**
* Returns the length of the first line of msg.

View File

@ -37,6 +37,7 @@
#include "help.h"
#include "commit-reach.h"
#include "commit-graph.h"
#include "pretty.h"
static const char * const builtin_commit_usage[] = {
N_("git commit [<options>] [--] <pathspec>..."),

View File

@ -19,6 +19,7 @@
#include "mem-pool.h"
#include "commit-reach.h"
#include "khash.h"
#include "date.h"
#define PACK_ID_BITS 16
#define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)

View File

@ -8,6 +8,7 @@
#include "parse-options.h"
#include "dir.h"
#include "commit-slab.h"
#include "date.h"
static const char* show_branch_usage[] = {
N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"

View File

@ -20,6 +20,7 @@
#include "oid-array.h"
#include "column.h"
#include "ref-filter.h"
#include "date.h"
static const char * const git_tag_usage[] = {
N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>]\n"

50
cache.h
View File

@ -1558,48 +1558,6 @@ struct object *repo_peel_to_type(struct repository *r,
#define peel_to_type(name, namelen, obj, type) \
repo_peel_to_type(the_repository, name, namelen, obj, type)
enum date_mode_type {
DATE_NORMAL = 0,
DATE_HUMAN,
DATE_RELATIVE,
DATE_SHORT,
DATE_ISO8601,
DATE_ISO8601_STRICT,
DATE_RFC2822,
DATE_STRFTIME,
DATE_RAW,
DATE_UNIX
};
struct date_mode {
enum date_mode_type type;
const char *strftime_fmt;
int local;
};
/*
* Convenience helper for passing a constant type, like:
*
* show_date(t, tz, DATE_MODE(NORMAL));
*/
#define DATE_MODE(t) date_mode_from_type(DATE_##t)
struct date_mode *date_mode_from_type(enum date_mode_type type);
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
void show_date_human(timestamp_t time, int tz, const struct timeval *now,
struct strbuf *timebuf);
int parse_date(const char *date, struct strbuf *out);
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
int parse_expiry_date(const char *date, timestamp_t *timestamp);
void datestamp(struct strbuf *out);
#define approxidate(s) approxidate_careful((s), NULL)
timestamp_t approxidate_careful(const char *, int *);
timestamp_t approxidate_relative(const char *date);
void parse_date_format(const char *format, struct date_mode *mode);
int date_overflows(timestamp_t date);
time_t tm_to_time_t(const struct tm *tm);
#define IDENT_STRICT 1
#define IDENT_NO_DATE 2
#define IDENT_NO_NAME 4
@ -1645,14 +1603,6 @@ struct ident_split {
*/
int split_ident_line(struct ident_split *, const char *, int);
/*
* Like show_date, but pull the timestamp and tz parameters from
* the ident_split. It will also sanity-check the values and produce
* a well-known sentinel date if they appear bogus.
*/
const char *show_ident_date(const struct ident_split *id,
const struct date_mode *mode);
/*
* Compare split idents for equality or strict ordering. Note that we
* compare only the ident part of the line, ignoring any timestamp.

View File

@ -6,6 +6,7 @@
*
*/
#include "cache.h"
#include "date.h"
#include "branch.h"
#include "config.h"
#include "environment.h"

9
date.c
View File

@ -5,6 +5,7 @@
*/
#include "cache.h"
#include "date.h"
/*
* 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)
struct date_mode *date_mode_from_type(enum date_mode_type type)
{
static struct date_mode mode;
static struct date_mode mode = DATE_MODE_INIT;
if (type == DATE_STRFTIME)
BUG("cannot create anonymous strftime date_mode struct");
mode.type = type;
mode.local = 0;
return &mode;
}
@ -993,6 +993,11 @@ void parse_date_format(const char *format, struct date_mode *mode)
die("unknown date format %s", format);
}
void date_mode_release(struct date_mode *mode)
{
free((char *)mode->strftime_fmt);
}
void datestamp(struct strbuf *out)
{
time_t now;

74
date.h Normal file
View File

@ -0,0 +1,74 @@
#ifndef DATE_H
#define DATE_H
/**
* The date mode type. This has DATE_NORMAL at an explicit "= 0" to
* accommodate a memset([...], 0, [...]) initialization when "struct
* date_mode" is used as an embedded struct member, as in the case of
* e.g. "struct pretty_print_context" and "struct rev_info".
*/
enum date_mode_type {
DATE_NORMAL = 0,
DATE_HUMAN,
DATE_RELATIVE,
DATE_SHORT,
DATE_ISO8601,
DATE_ISO8601_STRICT,
DATE_RFC2822,
DATE_STRFTIME,
DATE_RAW,
DATE_UNIX
};
struct date_mode {
enum date_mode_type type;
const char *strftime_fmt;
int local;
};
#define DATE_MODE_INIT { \
.type = DATE_NORMAL, \
}
/**
* Convenience helper for passing a constant type, like:
*
* show_date(t, tz, DATE_MODE(NORMAL));
*/
#define DATE_MODE(t) date_mode_from_type(DATE_##t)
struct date_mode *date_mode_from_type(enum date_mode_type type);
/**
* Format <'time', 'timezone'> into static memory according to 'mode'
* and return it. The mode is an initialized "struct date_mode"
* (usually from the DATE_MODE() macro).
*/
const char *show_date(timestamp_t time, int timezone, const struct date_mode *mode);
/**
* Parse a date format for later use with show_date().
*
* When the "date_mode_type" is DATE_STRFTIME the "strftime_fmt"
* member of "struct date_mode" will be a malloc()'d format string to
* be used with strbuf_addftime(), in which case you'll need to call
* date_mode_release() later.
*/
void parse_date_format(const char *format, struct date_mode *mode);
/**
* Release a "struct date_mode", currently only required if
* parse_date_format() has parsed a "DATE_STRFTIME" format.
*/
void date_mode_release(struct date_mode *mode);
void show_date_relative(timestamp_t time, struct strbuf *timebuf);
int parse_date(const char *date, struct strbuf *out);
int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
int parse_expiry_date(const char *date, timestamp_t *timestamp);
void datestamp(struct strbuf *out);
#define approxidate(s) approxidate_careful((s), NULL)
timestamp_t approxidate_careful(const char *, int *);
timestamp_t approxidate_relative(const char *date);
int date_overflows(timestamp_t date);
time_t tm_to_time_t(const struct tm *tm);
#endif

View File

@ -13,6 +13,7 @@
#include "packfile.h"
#include "object-store.h"
#include "protocol.h"
#include "date.h"
static const char content_type[] = "Content-Type";
static const char content_length[] = "Content-Length";

View File

@ -7,6 +7,7 @@
*/
#include "cache.h"
#include "config.h"
#include "date.h"
static struct strbuf git_default_name = STRBUF_INIT;
static struct strbuf git_default_email = STRBUF_INIT;

View File

@ -15,6 +15,7 @@
#include "submodule.h"
#include "midx.h"
#include "commit-reach.h"
#include "date.h"
static int get_oid_oneline(struct repository *r, const char *, struct object_id *, struct commit_list *);

View File

@ -2,6 +2,7 @@
#define PRETTY_H
#include "cache.h"
#include "date.h"
#include "string-list.h"
struct commit;
@ -163,4 +164,13 @@ int format_set_trailers_options(struct process_trailer_options *opts,
const char **arg,
char **invalid_arg);
/*
* Like show_date, but pull the timestamp and tz parameters from
* the ident_split. It will also sanity-check the values and produce
* a well-known sentinel date if they appear bogus.
*/
const char *show_ident_date(const struct ident_split *id,
const struct date_mode *mode);
#endif /* PRETTY_H */

View File

@ -1251,7 +1251,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
char *zone;
timestamp_t timestamp;
long tz;
struct date_mode date_mode = { DATE_NORMAL };
struct date_mode date_mode = DATE_MODE_INIT;
const char *formatp;
/*
@ -1276,6 +1276,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
goto bad;
v->s = xstrdup(show_date(timestamp, tz, &date_mode));
v->value = timestamp;
date_mode_release(&date_mode);
return;
bad:
v->s = xstrdup("");

View File

@ -5,6 +5,7 @@
struct commit;
struct reflog_walk_info;
struct date_mode;
void init_reflog_walk(struct reflog_walk_info **info);
int add_reflog_for_walk(struct reflog_walk_info *info,

1
refs.c
View File

@ -19,6 +19,7 @@
#include "strvec.h"
#include "repository.h"
#include "sigchain.h"
#include "date.h"
/*
* List of all available backends

View File

@ -2,6 +2,7 @@
#include "refs.h"
#include "string-list.h"
#include "utf8.h"
#include "date.h"
int starts_with(const char *str, const char *prefix)
{

View File

@ -1,5 +1,6 @@
#include "test-tool.h"
#include "cache.h"
#include "date.h"
static const char *usage_msg = "\n"
" test-tool date relative [time_t]...\n"
@ -34,7 +35,7 @@ static void show_human_dates(const char **argv)
static void show_dates(const char **argv, const char *format)
{
struct date_mode mode;
struct date_mode mode = DATE_MODE_INIT;
parse_date_format(format, &mode);
for (; *argv; argv++) {
@ -53,6 +54,8 @@ static void show_dates(const char **argv, const char *format)
printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
}
date_mode_release(&mode);
}
static void parse_dates(const char **argv)

View File

@ -1,6 +1,8 @@
#!/bin/sh
test_description='test date parsing and printing'
TEST_PASSES_SANITIZE_LEAK=true
. ./test-lib.sh
# arbitrary reference time: 2009-08-30 19:20:00