ps: Make STIME entries thread safe

Using localtime() can be a problem due to the static buffer for
the return value. It's simple enough to use localtime_r()

Signed-off-by: Craig Small <csmall@dropbear.xyz>
This commit is contained in:
Craig Small 2022-06-30 21:15:37 +10:00
parent b0adacf1ea
commit bcce3e440a

View File

@ -1058,23 +1058,25 @@ setREL1(TICS_BEGAN)
* as long as it still shows as STIME when using the -f option. * as long as it still shows as STIME when using the -f option.
*/ */
static int pr_stime(char *restrict const outbuf, const proc_t *restrict const pp){ static int pr_stime(char *restrict const outbuf, const proc_t *restrict const pp){
struct tm *proc_time; struct tm proc_time;
struct tm *our_time; struct tm our_time;
time_t t; time_t t;
const char *fmt; const char *fmt;
int tm_year; int tm_year;
int tm_yday; int tm_yday;
size_t len; size_t len;
setREL1(TICS_BEGAN) setREL1(TICS_BEGAN)
our_time = localtime(&seconds_since_1970); /* not reentrant */ if (localtime_r(&seconds_since_1970, &our_time) == NULL)
tm_year = our_time->tm_year; return 0;
tm_yday = our_time->tm_yday; tm_year = our_time.tm_year;
tm_yday = our_time.tm_yday;
t = boot_time() + rSv(TICS_BEGAN, ull_int, pp) / Hertz; t = boot_time() + rSv(TICS_BEGAN, ull_int, pp) / Hertz;
proc_time = localtime(&t); /* not reentrant, this corrupts our_time */ if (localtime_r(&t, &proc_time) == NULL)
return 0;
fmt = "%H:%M"; /* 03:02 23:59 */ fmt = "%H:%M"; /* 03:02 23:59 */
if(tm_yday != proc_time->tm_yday) fmt = "%b%d"; /* Jun06 Aug27 */ if(tm_yday != proc_time.tm_yday) fmt = "%b%d"; /* Jun06 Aug27 */
if(tm_year != proc_time->tm_year) fmt = "%Y"; /* 1991 2001 */ if(tm_year != proc_time.tm_year) fmt = "%Y"; /* 1991 2001 */
len = strftime(outbuf, COLWID, fmt, proc_time); len = strftime(outbuf, COLWID, fmt, &proc_time);
if(len <= 0 || len >= COLWID) outbuf[len = 0] = '\0'; if(len <= 0 || len >= COLWID) outbuf[len = 0] = '\0';
return len; return len;
} }