]> git.ipfire.org Git - thirdparty/git.git/commitdiff
date: make DATE_MODE thread-safe
authorRené Scharfe <l.s.r@web.de>
Fri, 5 Apr 2024 17:44:59 +0000 (19:44 +0200)
committerJunio C Hamano <gitster@pobox.com>
Fri, 5 Apr 2024 22:21:14 +0000 (15:21 -0700)
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>
12 files changed:
builtin/blame.c
date.c
date.h
gpg-interface.c
log-tree.c
oss-fuzz/fuzz-date.c
pretty.c
pretty.h
ref-filter.c
reflog-walk.c
reflog-walk.h
t/helper/test-date.c

index db1f56de61a434c3028869a2f4105c319ac6fd33..9aa74680a39b12da26e7ddba3090f38cc13b13b1 100644 (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 */
diff --git a/date.c b/date.c
index 44cf2221d81f61760fa26605765eaea5eee9ee4d..7365a4ad24fd18fb51032bdff7f7c57d52ada76d 100644 (file)
--- a/date.c
+++ b/date.c
@@ -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;
 }
 
diff --git a/date.h b/date.h
index 6136212a19004497382e5bae8d6675ebaac62011..0747864fd71c8082dc15c0e20675a7065a83568b 100644 (file)
--- a/date.h
+++ b/date.h
@@ -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().
index 95e764acb14b3e069a255a8261a7d724c5f8d1ad..1ee2c94a3b2be3de03c2194b9500357e60c255cb 100644 (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,
index e5438b029d90f352f0c434ebc8088c01fce11f3a..5a14eecbdd13af70b51460e34b76f86b8d05b829 100644 (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;
index 036378b946f0505eb52998f0d98a757becbf5236..9619dae40ed89935a7333b976ae3e540f95d3ffb 100644 (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;
 }
index bdbed4295aab2f0309eb872170251e2e9b185bbf..18f817604be682385c9d44ff1f11a876bd089172 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -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);
index 421209e9ec2732e1a08b130a6895d2688a28f52e..0d419ee24e80b98125208a80b658669b62aaaf7f 100644 (file)
--- a/pretty.h
+++ b/pretty.h
@@ -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 */
index 03542d023686f8b9d7089dddef0d17caf6c1ae44..59ad6f54ddb0983d9e5d50cf7352d7462e245334 100644 (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;
index d216f6f966da91459ea946790be56ba011760170..66484f4f32108a36923eb4c1e137b9395dfb7b40 100644 (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;
index 4d93a269571980772c49cb25d37749a033482b4f..989583dc556d82662be188aa2016515da86135e5 100644 (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);
index 0683d46574fa41e6b4186e8ec49d06ce65d93256..f25512de9a465b276539f0177d2ec4807707de58 100644 (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);