Commit 6c0d5366d9a9d24c76c1ca444a592111a1b2bdc3

Yoichi Nakayama 2021-10-22T07:54:12

Cause error when date parsing is failed

diff --git a/src/date.c b/src/date.c
index 2297ee6..af6694e 100644
--- a/src/date.c
+++ b/src/date.c
@@ -853,7 +853,7 @@ static git_time_t approxidate_str(const char *date,
 	}
 	pending_number(&tm, &number);
 	if (!touched)
-		*error_ret = 1;
+		*error_ret = -1;
 	return update_tm(&tm, &now, 0);
 }
 
@@ -872,7 +872,7 @@ int git__date_parse(git_time_t *out, const char *date)
 		return -1;
 
 	*out = approxidate_str(date, time_sec, &error_ret);
-   return error_ret;
+	return error_ret;
 }
 
 int git__date_rfc2822_fmt(char *out, size_t len, const git_time *date)
diff --git a/src/revparse.c b/src/revparse.c
index 8be22d6..2815bcf 100644
--- a/src/revparse.c
+++ b/src/revparse.c
@@ -348,8 +348,10 @@ static int handle_at_syntax(git_object **out, git_reference **ref, const char *s
 		goto cleanup;
 	}
 
-	if (git__date_parse(&timestamp, curly_braces_content) < 0)
+	if (git__date_parse(&timestamp, curly_braces_content) < 0) {
+		error = GIT_EINVALIDSPEC;
 		goto cleanup;
+	}
 
 	error = retrieve_revobject_from_reflog(out, ref, repo, git_str_cstr(&identifier), (size_t)timestamp);
 
diff --git a/tests/date/date.c b/tests/date/date.c
index 88881d1..ae787c0 100644
--- a/tests/date/date.c
+++ b/tests/date/date.c
@@ -13,3 +13,10 @@ void test_date_date__overflow(void)
    cl_assert(d2038 < d2039);
 #endif
 }
+
+void test_date_date__invalid_date(void)
+{
+   git_time_t d;
+   cl_git_fail(git__date_parse(&d, ""));
+   cl_git_fail(git__date_parse(&d, "NEITHER_INTEGER_NOR_DATETIME"));
+}
diff --git a/tests/refs/revparse.c b/tests/refs/revparse.c
index d6ec802..8d3a7e0 100644
--- a/tests/refs/revparse.c
+++ b/tests/refs/revparse.c
@@ -460,6 +460,19 @@ void test_refs_revparse__date(void)
 	test_object("with-empty-log@{2 days ago}", NULL);
 }
 
+void test_refs_revparse__invalid_date(void)
+{
+	/*
+	 * $ git rev-parse HEAD@{} --
+	 * fatal: bad revision 'HEAD@{}'
+	 *
+	 * $ git rev-parse HEAD@{NEITHER_INTEGER_NOR_DATETIME} --
+	 * fatal: bad revision 'HEAD@{NEITHER_INTEGER_NOR_DATETIME}'
+	 */
+	test_object("HEAD@{}", NULL);
+	test_object("HEAD@{NEITHER_INTEGER_NOR_DATETIME}", NULL);
+}
+
 void test_refs_revparse__colon(void)
 {
 	assert_invalid_single_spec(":/");