Skip to content

Commit 3be7391

Browse files
committed
Merge branch 'ds/trace2-tolerate-failed-timestamp' into seen
The trace2 telemetry library has been updated to tolerate failures from system calls like gettimeofday() and datetime formatting functions, replacing potential program crashes with blank placeholder timestamps in the traces. * ds/trace2-tolerate-failed-timestamp: trace2: tolerate failed timestamp formatting
2 parents dad645a + 8e67370 commit 3be7391

1 file changed

Lines changed: 34 additions & 15 deletions

File tree

‎trace2/tr2_tbuf.c‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,64 @@
33

44
void tr2_tbuf_local_time(struct tr2_tbuf *tb)
55
{
6-
struct timeval tv;
7-
struct tm tm;
6+
struct timeval tv = { 0 };
7+
struct tm tm = { 0 };
88
time_t secs;
9+
int len;
910

1011
gettimeofday(&tv, NULL);
1112
secs = tv.tv_sec;
1213
localtime_r(&secs, &tm);
1314

14-
xsnprintf(tb->buf, sizeof(tb->buf), "%02d:%02d:%02d.%06ld", tm.tm_hour,
15-
tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
15+
len = snprintf(tb->buf, sizeof(tb->buf), "%02d:%02d:%02d.%06ld",
16+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
17+
18+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
19+
const char *blank = "00:00:00.000000";
20+
strlcpy(tb->buf, blank, sizeof(tb->buf));
21+
}
1622
}
1723

1824
void tr2_tbuf_utc_datetime_extended(struct tr2_tbuf *tb)
1925
{
20-
struct timeval tv;
21-
struct tm tm;
26+
struct timeval tv = { 0 };
27+
struct tm tm = { 0 };
2228
time_t secs;
29+
int len;
2330

2431
gettimeofday(&tv, NULL);
2532
secs = tv.tv_sec;
2633
gmtime_r(&secs, &tm);
2734

28-
xsnprintf(tb->buf, sizeof(tb->buf),
29-
"%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ", tm.tm_year + 1900,
30-
tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
31-
(long)tv.tv_usec);
35+
len = snprintf(tb->buf, sizeof(tb->buf),
36+
"%4d-%02d-%02dT%02d:%02d:%02d.%06ldZ",
37+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
38+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
39+
40+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
41+
const char *blank = "1900-00-00T00:00:00.000000Z";
42+
strlcpy(tb->buf, blank, sizeof(tb->buf));
43+
}
3244
}
3345

3446
void tr2_tbuf_utc_datetime(struct tr2_tbuf *tb)
3547
{
36-
struct timeval tv;
37-
struct tm tm;
48+
struct timeval tv = { 0 };
49+
struct tm tm = { 0 };
3850
time_t secs;
51+
int len;
3952

4053
gettimeofday(&tv, NULL);
4154
secs = tv.tv_sec;
4255
gmtime_r(&secs, &tm);
4356

44-
xsnprintf(tb->buf, sizeof(tb->buf), "%4d%02d%02dT%02d%02d%02d.%06ldZ",
45-
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
46-
tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
57+
len = snprintf(tb->buf, sizeof(tb->buf),
58+
"%4d%02d%02dT%02d%02d%02d.%06ldZ",
59+
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
60+
tm.tm_hour, tm.tm_min, tm.tm_sec, (long)tv.tv_usec);
61+
62+
if (len < 0 || (size_t)len >= sizeof(tb->buf)) {
63+
const char *blank = "19000000T000000.000000Z";
64+
strlcpy(tb->buf, blank, sizeof(tb->buf));
65+
}
4766
}

0 commit comments

Comments
 (0)