Module: kamailio Branch: master Commit: 776d0d6bb10866e330e62003b7ec5bfcf4761f1a URL: https://github.com/kamailio/kamailio/commit/776d0d6bb10866e330e62003b7ec5bfc...
Author: Henning Westerholt hw@skalatan.de Committer: Henning Westerholt hw@skalatan.de Date: 2019-09-19T23:52:45+02:00
core: replace glibc time function calls with the thread-safe versions
- replace glibc time function calls with the thread-safe versions, to prevent race conditions from multi-process / multi-threaded access - used in (undocumented) SER 'sys' selects, no functional change, locally tested
---
Modified: src/core/select_core.c
---
Diff: https://github.com/kamailio/kamailio/commit/776d0d6bb10866e330e62003b7ec5bfc... Patch: https://github.com/kamailio/kamailio/commit/776d0d6bb10866e330e62003b7ec5bfc...
---
diff --git a/src/core/select_core.c b/src/core/select_core.c index c96db61104..6190c8b875 100644 --- a/src/core/select_core.c +++ b/src/core/select_core.c @@ -1542,16 +1542,22 @@ int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg) { #define SEL_POS 2 time_t t; - struct tm *tm; + struct tm tm; t = time(NULL); switch (s->params[SEL_POS].v.i) { case SEL_NOW_GMT: - tm = gmtime(&t); + if (! gmtime_r(&t, &tm)) { + ERR("Invalid UTC time value\n"); + return -1; + } break; case SEL_NOW_LOCAL: - tm = localtime(&t); + if (! localtime_r(&t, &tm)) { + ERR("Invalid local time value\n"); + return -1; + } break; default: BUG("Unexpected parameter value 'now' "%d" (%p)\n", @@ -1559,19 +1565,22 @@ int select_sys_now_fmt(str* res, select_t* s, struct sip_msg* msg) return -1; } if (s->n <= SEL_POS+1) { - char *c; - c = asctime(tm); - res->len = strlen(c); - while (res->len && c[res->len-1] < ' ') res->len--; /* rtrim */ + char buff[25]; + if (! asctime_r(&tm, buff)) { + ERR("Invalid time value\n"); + return -1; + } + res->len = strlen(buff); + while (res->len && buff[res->len-1] < ' ') res->len--; /* rtrim */ res->s = get_static_buffer(res->len); if (!res->s) return -1; - memcpy(res->s, c, res->len); + memcpy(res->s, &buff, res->len); } else { char c, buff[80]; c = s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len]; s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = '\0'; - res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, tm); + res->len = strftime(buff, sizeof(buff)-1, s->params[SEL_POS+1].v.s.s, &tm); s->params[SEL_POS+1].v.s.s[s->params[SEL_POS+1].v.s.len] = c; res->s = get_static_buffer(res->len); if (!res->s) return -1;