Skip to content

Commit d5cb91c

Browse files
committed
Handle localtime_r() failure in j1939 timedate
When the call to localtime_r() fails (by returning NULL, as per the POSIX specification), make sure to fill the hour/minute offsets with the fallback values as specified in the J1939DA supporting information.
1 parent 99ab671 commit d5cb91c

1 file changed

Lines changed: 17 additions & 11 deletions

File tree

j1939_timedate/j1939_timedate_srv.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,23 @@ static void gmtime_to_j1939_pgn_65254_td(struct j1939_time_date_packet *tdp)
4141
utc_tm = gmtime_r(&now, &utc_tm_buf);
4242
local_tm = localtime_r(&now, &local_tm_buf);
4343

44-
/* Calculate the offsets */
45-
hour_offset = local_tm->tm_hour - utc_tm->tm_hour;
46-
minute_offset = local_tm->tm_min - utc_tm->tm_min;
47-
48-
/* Handle date rollover */
49-
if (local_tm->tm_mday != utc_tm->tm_mday) {
50-
if (local_tm->tm_hour < 12)
51-
hour_offset += 24; /* past midnight */
52-
else
53-
hour_offset -= 24; /* before midnight */
54-
}
44+
if (local_tm) {
45+
/* Calculate the offsets */
46+
hour_offset = local_tm->tm_hour - utc_tm->tm_hour;
47+
minute_offset = local_tm->tm_min - utc_tm->tm_min;
48+
49+
/* Handle date rollover */
50+
if (local_tm->tm_mday != utc_tm->tm_mday) {
51+
if (local_tm->tm_hour < 12)
52+
hour_offset += 24; /* past midnight */
53+
else
54+
hour_offset -= 24; /* before midnight */
55+
}
56+
} else {
57+
/* The local time offsets cannot be determined at the moment */
58+
hour_offset = 0xF9;
59+
minute_offset = 0xFF;
60+
}
5561

5662
/*
5763
* Seconds (spn959):

0 commit comments

Comments
 (0)