time: an unrepresentable time is "?", not an uninitialised struct tm

Four places formatted a time with strftime(localtime_r(&tt, &tm)) and
threw the NULL away. When localtime_r cannot represent the value - a
cooldown file can hold anything - tm is untouched and strftime formats
whatever the stack held: the operator read a limit that lifts at
"219250468--1414812756--1414812757", and a NULL deref in the die-hard case.

agent.c, engine.c, session.c and plugins/cli now check the return and fall
back to "?", the answer the foreign plugin already gives for a time it does
not know.

Found by review; the strftime call site is the same shape in all four.
This commit is contained in:
lotherk committed 2026-11-23 10:23:29 +00:00
1 parent 407f073ae3
commit 4f7255548d
4 files changed
+26 -7

No files matched your search

+7 -3
View File
@@ -732,9 +732,13 @@ cli_send(void *ctx, const struct sp_conv *conv, struct sp_reply *out,
time_t tt = (time_t)soonest;
struct tm tm;
if (soonest > 0)
strftime(when, sizeof(when), "%Y-%m-%d %H:%M",
localtime_r(&tt, &tm));
if (soonest > 0) {
/* an unrepresentable time must not become a wrong
* one: the operator reads this as "come back at" */
if (localtime_r(&tt, &tm) != NULL)
strftime(when, sizeof(when),
"%Y-%m-%d %H:%M", &tm);
}
if (tried == 0)
sp_buf_clear(err);
sp_buf_printf(err, "%scli: every account at its usage limit, "
+11 -1
View File
@@ -372,7 +372,17 @@ sp_limit_wait_default(void *ctx, long until, const char *why)
if (until - now > cap)
return 0;
strftime(when, sizeof(when), "%Y-%m-%d %H:%M", localtime_r(&tt, &tm));
/*
* localtime_r() returns NULL for a value it cannot represent - and the
* value is not ours: a cooldown file can hold anything, a provider's
* "ready_at=" can be absurd. strftime() would then format whatever the
* uninitialised tm holds, and the operator reads a limit that lifts at
* "219250468--1414812756--1414812757". "?" is no time instead of a
* wrong one, the same answer the foreign plugin gives.
*/
strcpy(when, "?");
if (localtime_r(&tt, &tm) != NULL)
strftime(when, sizeof(when), "%Y-%m-%d %H:%M", &tm);
fprintf(stderr, "splinter: usage limit reached (%.80s); continuing at %s\n",
why != NULL ? why : "", when);
while ((long)time(NULL) < until)
+4 -1
View File
@@ -1226,7 +1226,10 @@ engine_limit_wait(void *ctx, long until, const char *why)
(void)why;
if (until - (long)time(NULL) > wait)
return 0;
strftime(when, sizeof(when), "%a %H:%M", localtime_r(&tt, &tm));
/* an unrepresentable time must not become a wrong one (see agent.c) */
strcpy(when, "?");
if (localtime_r(&tt, &tm) != NULL)
strftime(when, sizeof(when), "%a %H:%M", &tm);
snprintf(line, sizeof(line), sp_t("ui.limit_wait"), when);
ev_push(e, SP_EV_NOTE, 0, line, NULL);
while ((long)time(NULL) < until && !e->cancel) {
+4 -2
View File
@@ -1113,8 +1113,10 @@ sp_session_folder_history(const char *dir, const char *cwd, unsigned limit,
size_t turns = 0;
char last[512] = "";
(void)localtime_r(&when, &tm);
strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M", &tm);
if (localtime_r(&when, &tm) != NULL)
strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M", &tm);
else
snprintf(stamp, sizeof(stamp), "?");
snprintf(path, sizeof(path), "%.500s/%.120s.jsonl", dir,
found[i].name);
f = fopen(path, "r");