date: make DATE_MODE thread-safe

date_mode_from_type() modifies a static variable and returns a pointer
to it.  This is not thread-safe.  Most callers of date_mode_from_type()
use it via the macro DATE_MODE and pass its result on to functions like
show_date(), which take a const pointer and don't modify the struct.

Avoid the static storage by putting the variable on the stack and
returning the whole struct date_mode.  Change functions that take a
constant pointer to expect the whole struct instead.

Reduce the cost of passing struct date_mode around on 64-bit systems
by reordering its members to close the hole between the 32-bit wide
.type and the 64-bit aligned .strftime_fmt as well as the alignment
hole at the end.  sizeof reports 24 before and 16 with this change
on x64.  Keep .type at the top to still allow initialization without
designator -- though that's only done in a single location, in
builtin/blame.c.

Signed-off-by: René Scharfe <l.s.r@web.de>
Acked-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2024-04-05 19:44:59 +02:00 committed by Junio C Hamano
parent 1f49f7506f
commit 9720d23e8c
12 changed files with 44 additions and 44 deletions

View File

@ -316,7 +316,7 @@ static const char *format_time(timestamp_t time, const char *tz_str,
size_t time_width;
int tz;
tz = atoi(tz_str);
time_str = show_date(time, tz, &blame_date_mode);
time_str = show_date(time, tz, blame_date_mode);
strbuf_addstr(&time_buf, time_str);
/*
* Add space paddings to time_buf to display a fixed width
@ -1029,7 +1029,7 @@ parse_done:
blame_date_width = sizeof("Thu Oct 19 16:00:04 2006 -0700");
break;
case DATE_STRFTIME:
blame_date_width = strlen(show_date(0, 0, &blame_date_mode)) + 1; /* add the null */
blame_date_width = strlen(show_date(0, 0, blame_date_mode)) + 1; /* add the null */
break;
}
blame_date_width -= 1; /* strip the null */

36
date.c
View File

@ -207,13 +207,13 @@ void show_date_relative(timestamp_t time, struct strbuf *timebuf)
(diff + 183) / 365);
}
struct date_mode *date_mode_from_type(enum date_mode_type type)
struct date_mode date_mode_from_type(enum date_mode_type type)
{
static struct date_mode mode = DATE_MODE_INIT;
struct date_mode mode = DATE_MODE_INIT;
if (type == DATE_STRFTIME)
BUG("cannot create anonymous strftime date_mode struct");
mode.type = type;
return &mode;
return mode;
}
static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm, int tz, struct tm *human_tm, int human_tz, int local)
@ -283,7 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
strbuf_addf(buf, " %+05d", tz);
}
const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
const char *show_date(timestamp_t time, int tz, struct date_mode mode)
{
struct tm *tm;
struct tm tmbuf = { 0 };
@ -291,13 +291,13 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
int human_tz = -1;
static struct strbuf timebuf = STRBUF_INIT;
if (mode->type == DATE_UNIX) {
if (mode.type == DATE_UNIX) {
strbuf_reset(&timebuf);
strbuf_addf(&timebuf, "%"PRItime, time);
return timebuf.buf;
}
if (mode->type == DATE_HUMAN) {
if (mode.type == DATE_HUMAN) {
struct timeval now;
get_time(&now);
@ -306,22 +306,22 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
human_tz = local_time_tzoffset(now.tv_sec, &human_tm);
}
if (mode->local)
if (mode.local)
tz = local_tzoffset(time);
if (mode->type == DATE_RAW) {
if (mode.type == DATE_RAW) {
strbuf_reset(&timebuf);
strbuf_addf(&timebuf, "%"PRItime" %+05d", time, tz);
return timebuf.buf;
}
if (mode->type == DATE_RELATIVE) {
if (mode.type == DATE_RELATIVE) {
strbuf_reset(&timebuf);
show_date_relative(time, &timebuf);
return timebuf.buf;
}
if (mode->local)
if (mode.local)
tm = time_to_tm_local(time, &tmbuf);
else
tm = time_to_tm(time, tz, &tmbuf);
@ -331,17 +331,17 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
}
strbuf_reset(&timebuf);
if (mode->type == DATE_SHORT)
if (mode.type == DATE_SHORT)
strbuf_addf(&timebuf, "%04d-%02d-%02d", tm->tm_year + 1900,
tm->tm_mon + 1, tm->tm_mday);
else if (mode->type == DATE_ISO8601)
else if (mode.type == DATE_ISO8601)
strbuf_addf(&timebuf, "%04d-%02d-%02d %02d:%02d:%02d %+05d",
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tz);
else if (mode->type == DATE_ISO8601_STRICT) {
else if (mode.type == DATE_ISO8601_STRICT) {
strbuf_addf(&timebuf, "%04d-%02d-%02dT%02d:%02d:%02d",
tm->tm_year + 1900,
tm->tm_mon + 1,
@ -354,16 +354,16 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
tz = abs(tz);
strbuf_addf(&timebuf, "%02d:%02d", tz / 100, tz % 100);
}
} else if (mode->type == DATE_RFC2822)
} else if (mode.type == DATE_RFC2822)
strbuf_addf(&timebuf, "%.3s, %d %.3s %d %02d:%02d:%02d %+05d",
weekday_names[tm->tm_wday], tm->tm_mday,
month_names[tm->tm_mon], tm->tm_year + 1900,
tm->tm_hour, tm->tm_min, tm->tm_sec, tz);
else if (mode->type == DATE_STRFTIME)
strbuf_addftime(&timebuf, mode->strftime_fmt, tm, tz,
!mode->local);
else if (mode.type == DATE_STRFTIME)
strbuf_addftime(&timebuf, mode.strftime_fmt, tm, tz,
!mode.local);
else
show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode->local);
show_date_normal(&timebuf, time, tm, tz, &human_tm, human_tz, mode.local);
return timebuf.buf;
}

6
date.h
View File

@ -22,8 +22,8 @@ enum date_mode_type {
struct date_mode {
enum date_mode_type type;
const char *strftime_fmt;
int local;
const char *strftime_fmt;
};
#define DATE_MODE_INIT { \
@ -36,14 +36,14 @@ struct date_mode {
* 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);
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);
const char *show_date(timestamp_t time, int timezone, struct date_mode mode);
/**
* Parse a date format for later use with show_date().

View File

@ -483,7 +483,7 @@ static int verify_ssh_signed_buffer(struct signature_check *sigc,
if (sigc->payload_timestamp)
strbuf_addf(&verify_time, "-Overify-time=%s",
show_date(sigc->payload_timestamp, 0, &verify_date_mode));
show_date(sigc->payload_timestamp, 0, verify_date_mode));
/* Find the principal from the signers */
strvec_pushl(&ssh_keygen.args, fmt->program,

View File

@ -777,7 +777,7 @@ void show_log(struct rev_info *opt)
*/
show_reflog_message(opt->reflog_info,
opt->commit_format == CMIT_FMT_ONELINE,
&opt->date_mode,
opt->date_mode,
opt->date_mode_explicit);
if (opt->commit_format == CMIT_FMT_ONELINE)
return;

View File

@ -11,7 +11,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
int16_t tz;
timestamp_t ts;
enum date_mode_type dmtype;
struct date_mode *dm;
struct date_mode dm;
if (size <= 4)
/*
@ -40,10 +40,10 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
free(str);
dm = date_mode_from_type(dmtype);
dm->local = local;
dm.local = local;
show_date(ts, (int)tz, dm);
date_mode_release(dm);
date_mode_release(&dm);
return 0;
}

View File

@ -428,7 +428,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, size_t len,
}
const char *show_ident_date(const struct ident_split *ident,
const struct date_mode *mode)
struct date_mode mode)
{
timestamp_t date = 0;
long tz = 0;
@ -592,7 +592,7 @@ void pp_user_info(struct pretty_print_context *pp,
switch (pp->fmt) {
case CMIT_FMT_MEDIUM:
strbuf_addf(sb, "Date: %s\n",
show_ident_date(&ident, &pp->date_mode));
show_ident_date(&ident, pp->date_mode));
break;
case CMIT_FMT_EMAIL:
case CMIT_FMT_MBOXRD:
@ -601,7 +601,7 @@ void pp_user_info(struct pretty_print_context *pp,
break;
case CMIT_FMT_FULLER:
strbuf_addf(sb, "%sDate: %s\n", what,
show_ident_date(&ident, &pp->date_mode));
show_ident_date(&ident, pp->date_mode));
break;
default:
/* notin' */
@ -775,7 +775,7 @@ static int mailmap_name(const char **email, size_t *email_len,
static size_t format_person_part(struct strbuf *sb, char part,
const char *msg, int len,
const struct date_mode *dmode)
struct date_mode dmode)
{
/* currently all placeholders have same length */
const int placeholder_len = 2;
@ -1034,7 +1034,7 @@ static void rewrap_message_tail(struct strbuf *sb,
static int format_reflog_person(struct strbuf *sb,
char part,
struct reflog_walk_info *log,
const struct date_mode *dmode)
struct date_mode dmode)
{
const char *ident;
@ -1602,7 +1602,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
if (c->pretty_ctx->reflog_info)
get_reflog_selector(sb,
c->pretty_ctx->reflog_info,
&c->pretty_ctx->date_mode,
c->pretty_ctx->date_mode,
c->pretty_ctx->date_mode_explicit,
(placeholder[1] == 'd'));
return 2;
@ -1617,7 +1617,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
return format_reflog_person(sb,
placeholder[1],
c->pretty_ctx->reflog_info,
&c->pretty_ctx->date_mode);
c->pretty_ctx->date_mode);
}
return 0; /* unknown %g placeholder */
case 'N':
@ -1712,11 +1712,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
case 'a': /* author ... */
return format_person_part(sb, placeholder[1],
msg + c->author.off, c->author.len,
&c->pretty_ctx->date_mode);
c->pretty_ctx->date_mode);
case 'c': /* committer ... */
return format_person_part(sb, placeholder[1],
msg + c->committer.off, c->committer.len,
&c->pretty_ctx->date_mode);
c->pretty_ctx->date_mode);
case 'e': /* encoding */
if (c->commit_encoding)
strbuf_addstr(sb, c->commit_encoding);

View File

@ -168,7 +168,7 @@ int format_set_trailers_options(struct process_trailer_options *opts,
* a well-known sentinel date if they appear bogus.
*/
const char *show_ident_date(const struct ident_split *id,
const struct date_mode *mode);
struct date_mode mode);
#endif /* PRETTY_H */

View File

@ -1627,7 +1627,7 @@ static void grab_date(const char *buf, struct atom_value *v, const char *atomnam
tz = strtol(zone, NULL, 10);
if ((tz == LONG_MIN || tz == LONG_MAX) && errno == ERANGE)
goto bad;
v->s = xstrdup(show_date(timestamp, tz, &date_mode));
v->s = xstrdup(show_date(timestamp, tz, date_mode));
v->value = timestamp;
date_mode_release(&date_mode);
return;

View File

@ -223,7 +223,7 @@ int add_reflog_for_walk(struct reflog_walk_info *info,
void get_reflog_selector(struct strbuf *sb,
struct reflog_walk_info *reflog_info,
const struct date_mode *dmode, int force_date,
struct date_mode dmode, int force_date,
int shorten)
{
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;
@ -297,7 +297,7 @@ timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)
}
void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,
const struct date_mode *dmode, int force_date)
struct date_mode dmode, int force_date)
{
if (reflog_info && reflog_info->last_commit_reflog) {
struct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;

View File

@ -10,14 +10,14 @@ void reflog_walk_info_release(struct reflog_walk_info *info);
int add_reflog_for_walk(struct reflog_walk_info *info,
struct commit *commit, const char *name);
void show_reflog_message(struct reflog_walk_info *info, int,
const struct date_mode *, int force_date);
struct date_mode, int force_date);
void get_reflog_message(struct strbuf *sb,
struct reflog_walk_info *reflog_info);
const char *get_reflog_ident(struct reflog_walk_info *reflog_info);
timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);
void get_reflog_selector(struct strbuf *sb,
struct reflog_walk_info *reflog_info,
const struct date_mode *dmode, int force_date,
struct date_mode dmode, int force_date,
int shorten);
int reflog_walk_empty(struct reflog_walk_info *walk);

View File

@ -52,7 +52,7 @@ static void show_dates(const char **argv, const char *format)
arg++;
tz = atoi(arg);
printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
printf("%s -> %s\n", *argv, show_date(t, tz, mode));
}
date_mode_release(&mode);