Commit 6cb9c766 authored by Martin Blix Grydeland's avatar Martin Blix Grydeland Committed by Reza Naghibi

Handle unformatable VCL_TIME to string conversion failures

For VCL_TIME values that would convert to a year element that can not fit
in an int, gmtime_r would fail, and VTIM_format() would use random stack
values when picking weekday and month strings.

This patch changes VTIM_format to return "" when gmtime_r reports
failures. This way the API is not changed. Callers can test for empty
string to catch the failure if needed.

VRT_TIME_string is patched to catch the VTIM_format error, and return NULL
on failure.

Fixes: #3308
parent 0644d58a
......@@ -629,8 +629,11 @@ VRT_TIME_string(VRT_CTX, VCL_TIME t)
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
p = WS_Alloc(ctx->ws, VTIM_FORMAT_SIZE);
if (p != NULL)
if (p != NULL) {
VTIM_format(t, p);
if (*p == '\0')
p = NULL;
}
return (p);
}
......
varnishtest "Unformatable VCL_TIME"
server s1 {
rxreq
txresp
} -start
varnish v1 -vcl+backend {
import std;
sub vcl_deliver {
set resp.http.ts = std.real2time(std.real("1e+22", 0), now);
}
} -start
client c1 {
txreq
rxresp
expect resp.http.ts == ""
} -run
......@@ -165,11 +165,16 @@ VTIM_format(vtim_real t, char *p)
struct tm tm;
time_t tt;
AN(p);
tt = (time_t) t;
(void)gmtime_r(&tt, &tm);
AN(snprintf(p, VTIM_FORMAT_SIZE, "%s, %02d %s %4d %02d:%02d:%02d GMT",
weekday_name[tm.tm_wday], tm.tm_mday, month_name[tm.tm_mon],
tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec));
if (gmtime_r(&tt, &tm) != NULL)
AN(snprintf(p, VTIM_FORMAT_SIZE,
"%s, %02d %s %4d %02d:%02d:%02d GMT",
weekday_name[tm.tm_wday],
tm.tm_mday, month_name[tm.tm_mon],
tm.tm_year + 1900, tm.tm_hour, tm.tm_min, tm.tm_sec));
else
*p = '\0';
}
#ifdef TEST_DRIVER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment