git/oss-fuzz/fuzz-date.c
René Scharfe 9720d23e8c 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>
2024-04-05 15:21:14 -07:00

50 lines
947 B
C

#include "git-compat-util.h"
#include "date.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
int local;
int num;
char *str;
int16_t tz;
timestamp_t ts;
enum date_mode_type dmtype;
struct date_mode dm;
if (size <= 4)
/*
* we use the first byte to fuzz dmtype and the
* second byte to fuzz local, then the next two
* bytes to fuzz tz offset. The remainder
* (at least one byte) is fed as input to
* approxidate_careful().
*/
return 0;
local = !!(*data++ & 0x10);
num = *data++ % DATE_UNIX;
if (num >= DATE_STRFTIME)
num++;
dmtype = (enum date_mode_type)num;
size -= 2;
tz = *data++;
tz = (tz << 8) | *data++;
size -= 2;
str = xmemdupz(data, size);
ts = approxidate_careful(str, &num);
free(str);
dm = date_mode_from_type(dmtype);
dm.local = local;
show_date(ts, (int)tz, dm);
date_mode_release(&dm);
return 0;
}