Skip to content

Commit

Permalink
Add gmtime_r support and test for failure case.
Browse files Browse the repository at this point in the history
* src/ne_dates.c (ne_rfc1123_date): Add gmtime_r support.

* test/util-tests.c (bad_dates): Add test for failure case
  if sizeof(time_t) == 8.

* macros/neon.m4 (LIBNEON_SOURCE_CHECKS): Test for gmtime_r.
  • Loading branch information
notroj committed Nov 23, 2023
1 parent aa315bd commit 579ed4b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion macros/neon.m4
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ NE_LARGEFILE
AC_REPLACE_FUNCS(strcasecmp)
AC_CHECK_FUNCS([signal setvbuf setsockopt stpcpy poll fcntl getsockopt \
explicit_bzero sendmsg gettimeofday])
explicit_bzero sendmsg gettimeofday gmtime_r])
if test "x${ac_cv_func_poll}${ac_cv_header_sys_poll_h}y" = "xyesyesy"; then
AC_DEFINE([NE_USE_POLL], 1, [Define if poll() should be used])
Expand Down
25 changes: 17 additions & 8 deletions src/ne_dates.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,26 @@ time_t gmt_to_local_win32(void)
#endif

char *ne_rfc1123_date(time_t anytime) {
struct tm *gmt;
struct tm gmt;
struct tm *rv;
char *ret;
gmt = gmtime(&anytime);
if (gmt == NULL)
return NULL;

#ifdef HAVE_GMTIME_R
if ((rv = gmtime(&anytime, &gmt)) == NULL)
return NULL;
#else
if ((rv = gmtime(&anytime)) == NULL)
return NULL;

gmt = *rv;
#endif

ret = ne_malloc(29 + 1); /* dates are 29 chars long */
ne_snprintf(ret, 30, IMFFIX_FORMAT,
rfc1123_weekdays[gmt->tm_wday], gmt->tm_mday,
short_months[gmt->tm_mon], 1900 + gmt->tm_year,
gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
rfc1123_weekdays[gmt.tm_wday], gmt.tm_mday,
short_months[gmt.tm_mon], 1900 + gmt.tm_year,
gmt.tm_hour, gmt.tm_min, gmt.tm_sec);

return ret;
}

Expand Down
15 changes: 15 additions & 0 deletions test/util-tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif

#include "ne_utils.h"
#include "ne_md5.h"
Expand Down Expand Up @@ -254,6 +257,18 @@ static int bad_dates(void)
BAD_DATE("asctime", ne_asctime_parse(dates[n]));
}

#if SIZEOF_TIME_T == 8 && defined(INT64_MAX)
{
char *rv = ne_rfc1123_date(INT64_MAX);

ONV(rv != NULL,
("RFC1123 date conversion surprisingly worked for INT64_MAX: %s",
rv));

ne_free(rv);
}
#endif

return OK;
}

Expand Down

0 comments on commit 579ed4b

Please sign in to comment.