diff --git a/src/core/ngx_times.c b/src/core/ngx_times.c index 16788c98c..d7ef77ef5 100644 --- a/src/core/ngx_times.c +++ b/src/core/ngx_times.c @@ -360,7 +360,7 @@ ngx_gmtime(time_t t, ngx_tm_t *tp) */ /* days since March 1, 1 BC */ - days = days - (31 + 28) + 719527; + days = days + 719527 - (31 + 28); /* * The "days" should be adjusted to 1 only, however, some March 1st's go diff --git a/src/os/unix/ngx_time.c b/src/os/unix/ngx_time.c index c97bae2ed..386b073eb 100644 --- a/src/os/unix/ngx_time.c +++ b/src/os/unix/ngx_time.c @@ -46,6 +46,9 @@ ngx_timezone_update(void) s = time(NULL); t = localtime(&s); + if (t == NULL) { + return; + } strftime(buf, 4, "%H", t); @@ -57,12 +60,20 @@ void ngx_localtime(time_t s, ngx_tm_t *tm) { #if (NGX_HAVE_LOCALTIME_R) - (void) localtime_r(&s, tm); + if (localtime_r(&s, tm) == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } #else ngx_tm_t *t; t = localtime(&s); + if (t == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } + *tm = *t; #endif @@ -76,12 +87,20 @@ void ngx_libc_localtime(time_t s, struct tm *tm) { #if (NGX_HAVE_LOCALTIME_R) - (void) localtime_r(&s, tm); + if (localtime_r(&s, tm) == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } #else struct tm *t; t = localtime(&s); + if (t == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } + *tm = *t; #endif @@ -92,12 +111,20 @@ void ngx_libc_gmtime(time_t s, struct tm *tm) { #if (NGX_HAVE_LOCALTIME_R) - (void) gmtime_r(&s, tm); + if (gmtime_r(&s, tm) == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } #else struct tm *t; t = gmtime(&s); + if (t == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } + *tm = *t; #endif diff --git a/src/os/win32/ngx_time.c b/src/os/win32/ngx_time.c index 79149b2c7..d578d31d0 100644 --- a/src/os/win32/ngx_time.c +++ b/src/os/win32/ngx_time.c @@ -44,6 +44,11 @@ ngx_libc_localtime(time_t s, struct tm *tm) struct tm *t; t = localtime(&s); + if (t == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } + *tm = *t; } @@ -54,6 +59,11 @@ ngx_libc_gmtime(time_t s, struct tm *tm) struct tm *t; t = gmtime(&s); + if (t == NULL) { + ngx_memzero(tm, sizeof(struct tm)); + return; + } + *tm = *t; }