From cc794e993c88677b4d7265a68fabab21ccc4baab Mon Sep 17 00:00:00 2001 From: Pau Espin Pedrol Date: Wed, 12 Jun 2019 16:22:53 +0200 Subject: 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 --- src/logging.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/logging.c') 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) { -- cgit v1.2.3