Skip to content

Commit cfaa64a

Browse files
authored
fix: use gmtime_r in get_timestamp for thread safety (#655)
gmtime(3) returns a pointer to a static internal buffer shared across all threads. In a multi-threaded decoder, concurrent calls to get_timestamp() could race on that buffer, corrupting decoded timestamps. gmtime_r(3) takes a caller-supplied struct tm buffer, eliminating the race. It is already used consistently elsewhere in the codebase (log.c, waypoint.c, encode_aprs.c, beacon.c) and the portability shim that makes it available on all platforms is in direwolf.h. Add a null-return guard as the FIXME comment requested. Fixes the FIXME at decode_aprs.c line 4041.
1 parent 78d6559 commit cfaa64a

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

src/decode_aprs.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4034,13 +4034,12 @@ time_t get_timestamp (decode_aprs_t *A, char *p)
40344034
}
40354035

40364036
struct tm *ptm;
4037-
4037+
struct tm tm_buf;
40384038
time_t ts;
40394039

40404040
ts = time(NULL);
4041-
// FIXME: use gmtime_r instead.
4042-
// Besides not being thread safe, gmtime could possibly return null.
4043-
ptm = gmtime(&ts);
4041+
ptm = gmtime_r(&ts, &tm_buf);
4042+
if (ptm == NULL) return ((time_t)0);
40444043

40454044
pdhm = (void *)p;
40464045
phms = (void *)p;

0 commit comments

Comments
 (0)