]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fuzz: add new oss-fuzz fuzzer for date.c / date.h
authorArthur Chan <arthur.chan@adalogics.com>
Fri, 17 Nov 2023 17:47:47 +0000 (17:47 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 19 Nov 2023 23:17:51 +0000 (08:17 +0900)
Signed-off-by: Arthur Chan <arthur.chan@adalogics.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
oss-fuzz/.gitignore
oss-fuzz/fuzz-date.c [new file with mode: 0644]

index 5776309365357a5848c5f03214690e75d7deb57c..94ceadb6cbf74e836dcaa63bcfae2e9c457f3c17 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -750,6 +750,7 @@ SCRIPTS = $(SCRIPT_SH_GEN) \
 ETAGS_TARGET = TAGS
 
 FUZZ_OBJS += oss-fuzz/fuzz-commit-graph.o
+FUZZ_OBJS += oss-fuzz/fuzz-date.o
 FUZZ_OBJS += oss-fuzz/fuzz-pack-headers.o
 FUZZ_OBJS += oss-fuzz/fuzz-pack-idx.o
 .PHONY: fuzz-objs
index 9acb74412ef0fc28589262b392c9d56c7d381137..5b954088254b21a6cbc90b41d0dac415a552d6b7 100644 (file)
@@ -1,3 +1,4 @@
 fuzz-commit-graph
+fuzz-date
 fuzz-pack-headers
 fuzz-pack-idx
diff --git a/oss-fuzz/fuzz-date.c b/oss-fuzz/fuzz-date.c
new file mode 100644 (file)
index 0000000..036378b
--- /dev/null
@@ -0,0 +1,49 @@
+#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;
+}