summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPau Espin Pedrol <pespin@sysmocom.de>2019-06-12 16:22:53 +0200
committerPau Espin Pedrol <pespin@sysmocom.de>2019-06-13 19:35:11 +0200
commitcc794e993c88677b4d7265a68fabab21ccc4baab (patch)
tree3e1aa21a703de901f3219734000bf0d3f02ca776 /src
parent77021c7bec2e2e647bc4ca145d320064f13d5155 (diff)
logging: Use reentrant ctime_r instead of ctime
It was noticed that multithreaded processes like osmo-trx can crash upon using ctime(). Related: OS#4055 Change-Id: I19ebf29a2f1fc855bb7d56766b338c7c3432dfd1
Diffstat (limited to 'src')
-rw-r--r--src/logging.c13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/logging.c b/src/logging.c
index 4c864739..1c3544fa 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -359,15 +359,18 @@ static void _output(struct log_target *target, unsigned int subsys,
OSMO_SNPRINTF_RET(ret, rem, offset, len);
#endif
} else if (target->print_timestamp) {
- char *timestr;
time_t tm;
if ((tm = time(NULL)) == (time_t) -1)
goto err;
- timestr = ctime(&tm);
- timestr[strlen(timestr)-1] = '\0';
- ret = snprintf(buf + offset, rem, "%s ", timestr);
- if (ret < 0)
+ /* Get human-readable representation of time.
+ man ctime: we need at least 26 bytes in buf */
+ if (rem < 26 || !ctime_r(&tm, buf + offset))
+ goto err;
+ ret = strlen(buf + offset);
+ if (ret <= 0)
goto err;
+ /* Get rid of useless final '\n' added by ctime_r. We want a space instead. */
+ buf[offset + ret - 1] = ' ';
OSMO_SNPRINTF_RET(ret, rem, offset, len);
}
if (target->print_category) {