Skip to content

Commit

Permalink
Merge pull request #805 from greenbone/date-parse
Browse files Browse the repository at this point in the history
Change: Drop parsing for $Date
  • Loading branch information
mattmundell authored Jan 15, 2024
2 parents 7277fb4 + df88998 commit 438edbb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 39 deletions.
44 changes: 5 additions & 39 deletions base/nvti.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,45 +300,20 @@ parse_nvt_timestamp (const gchar *str_time)
int offset;
struct tm tm;

if ((strcmp ((char *) str_time, "") == 0)
|| (strcmp ((char *) str_time, "$Date: $") == 0)
|| (strcmp ((char *) str_time, "$Date$") == 0)
|| (strcmp ((char *) str_time, "$Date:$") == 0)
|| (strcmp ((char *) str_time, "$Date") == 0)
|| (strcmp ((char *) str_time, "$$") == 0))
{
return 0;
}
if (strcmp ((char *) str_time, "") == 0)
return 0;

/* Parse the time. */

/* 2011-08-09 08:20:34 +0200 (Tue, 09 Aug 2011) */
/* $Date: 2012-02-17 16:05:26 +0100 (Fr, 17. Feb 2012) $ */
/* $Date: Fri, 11 Nov 2011 14:42:28 +0100 $ */
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "%F %T %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %F %T %z", &tm) == NULL)
if (strptime ((char *) str_time, "%a %b %d %T %Y %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "%a %b %d %T %Y %z", &tm) == NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %a, %d %b %Y %T %z", &tm)
== NULL)
{
memset (&tm, 0, sizeof (struct tm));
if (strptime ((char *) str_time, "$Date: %a %b %d %T %Y %z",
&tm)
== NULL)
{
g_warning ("%s: Failed to parse time: %s", __func__,
str_time);
return 0;
}
}
}
g_warning ("%s: Failed to parse time: %s", __func__, str_time);
return 0;
}
}
epoch_time = mktime (&tm);
Expand All @@ -352,17 +327,8 @@ parse_nvt_timestamp (const gchar *str_time)

if ((sscanf ((char *) str_time, "%*u-%*u-%*u %*u:%*u:%*u %d%*[^]]", &offset)
!= 1)
&& (sscanf ((char *) str_time, "$Date: %*u-%*u-%*u %*u:%*u:%*u %d%*[^]]",
&offset)
!= 1)
&& (sscanf ((char *) str_time, "%*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]",
&offset)
!= 1)
&& (sscanf ((char *) str_time,
"$Date: %*s %*s %*s %*u %*u:%*u:%*u %d%*[^]]", &offset)
!= 1)
&& (sscanf ((char *) str_time,
"$Date: %*s %*s %*s %*u:%*u:%*u %*u %d%*[^]]", &offset)
!= 1))
{
g_warning ("%s: Failed to parse timezone offset: %s", __func__, str_time);
Expand Down
20 changes: 20 additions & 0 deletions base/nvti_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ Ensure (nvti, nvti_new_never_returns_null)

/* nvti solution_method */

Ensure (nvti, nvti_parse_timestamp)
{
setenv ("TZ", "utc 0", 1);
tzset ();

assert_that (
parse_nvt_timestamp ("2018-09-07 11:08:31 +0200 (Fri, 07 Sep 2018)"),
is_equal_to (1536311311));
assert_that_double (
parse_nvt_timestamp ("2022-05-31 20:54:22 +0100 (Tue, 31 May 2022)"),
is_equal_to_double (1654026862));
assert_that_double (parse_nvt_timestamp ("2012-09-23 02:15:34 +0400"),
is_equal_to_double (1348352134));
assert_that_double (parse_nvt_timestamp ("Fri Feb 10 16:09:30 2023 +0100"),
is_equal_to_double (1676041770));
}

/* nvti solution_method */

Ensure (nvti, nvti_set_solution_method_correct)
{
nvti_t *nvti;
Expand Down Expand Up @@ -195,6 +214,7 @@ main (int argc, char **argv)
add_test_with_context (suite, nvti,
nvti_get_severity_vector_no_severity_vector);
add_test_with_context (suite, nvti, nvti_get_severity_vector_no_cvss_base);
add_test_with_context (suite, nvti, nvti_parse_timestamp);

if (argc > 1)
return run_single_test (suite, argv[1], create_text_reporter ());
Expand Down

0 comments on commit 438edbb

Please sign in to comment.