The Buffer That Worked In Testing
This prints the same timestamp twice, or garbage, depending on the compiler.
const char *fmt_ts(time_t t) {
char buf[32];
struct tm tm;
localtime_r(&t, &tm);
strftime(buf, sizeof buf, "%Y-%m-%dT%H:%M:%S", &tm);
return buf; /* frame dies here */
}
void log_pair(time_t a, time_t b) {
printf("%s .. %s\n", fmt_ts(a), fmt_ts(b));
}
Four fixes are proposed. Pick the one that leaves log_pair printing two different timestamps, is safe when several threads call fmt_ts at once, and hands the caller nothing to release.
Sign in to answer questions and track your progress
Sign In