Avoid assuming that time_t can fit in an int.

We had several places that used cast-to-unsigned-int as a substitute
for properly checking for overflow.  Coverity has started objecting
to that practice as likely introducing Y2038 bugs.  An extra
comparison is surely not much compared to the cost of time(NULL), nor
is this coding practice particularly readable.  Let's do it honestly,
with explicit logic covering the cases of first-time-through and
clock-went-backwards.

I don't feel a need to back-patch though: our released versions
will be out of support long before 2038, and besides which I think
the code would accidentally work anyway for another 70 years or so.
This commit is contained in:
Tom Lane 2025-10-22 17:50:05 -04:00
parent d10866f1fd
commit fe9c051fd3
3 changed files with 45 additions and 27 deletions

View File

@ -185,8 +185,8 @@ PgArchShmemInit(void)
/* /*
* PgArchCanRestart * PgArchCanRestart
* *
* Return true and archiver is allowed to restart if enough time has * Return true, indicating archiver is allowed to restart, if enough time has
* passed since it was launched last to reach PGARCH_RESTART_INTERVAL. * passed since it was last launched to reach PGARCH_RESTART_INTERVAL.
* Otherwise return false. * Otherwise return false.
* *
* This is a safety valve to protect against continuous respawn attempts if the * This is a safety valve to protect against continuous respawn attempts if the
@ -201,15 +201,18 @@ PgArchCanRestart(void)
time_t curtime = time(NULL); time_t curtime = time(NULL);
/* /*
* Return false and don't restart archiver if too soon since last archiver * If first time through, or time somehow went backwards, always update
* start. * last_pgarch_start_time to match the current clock and allow archiver
* start. Otherwise allow it only once enough time has elapsed.
*/ */
if ((unsigned int) (curtime - last_pgarch_start_time) < if (last_pgarch_start_time == 0 ||
(unsigned int) PGARCH_RESTART_INTERVAL) curtime < last_pgarch_start_time ||
return false; curtime - last_pgarch_start_time >= PGARCH_RESTART_INTERVAL)
{
last_pgarch_start_time = curtime; last_pgarch_start_time = curtime;
return true; return true;
}
return false;
} }
@ -332,7 +335,8 @@ pgarch_MainLoop(void)
* SIGUSR2 arrives. However, that means a random SIGTERM would * SIGUSR2 arrives. However, that means a random SIGTERM would
* disable archiving indefinitely, which doesn't seem like a good * disable archiving indefinitely, which doesn't seem like a good
* idea. If more than 60 seconds pass since SIGTERM, exit anyway, so * idea. If more than 60 seconds pass since SIGTERM, exit anyway, so
* that the postmaster can start a new archiver if needed. * that the postmaster can start a new archiver if needed. Also exit
* if time unexpectedly goes backward.
*/ */
if (ShutdownRequestPending) if (ShutdownRequestPending)
{ {
@ -340,8 +344,8 @@ pgarch_MainLoop(void)
if (last_sigterm_time == 0) if (last_sigterm_time == 0)
last_sigterm_time = curtime; last_sigterm_time = curtime;
else if ((unsigned int) (curtime - last_sigterm_time) >= else if (curtime < last_sigterm_time ||
(unsigned int) 60) curtime - last_sigterm_time >= 60)
break; break;
} }

View File

@ -1557,13 +1557,21 @@ DetermineSleepTime(void)
{ {
if (AbortStartTime != 0) if (AbortStartTime != 0)
{ {
time_t curtime = time(NULL);
int seconds; int seconds;
/* time left to abort; clamp to 0 in case it already expired */ /*
seconds = SIGKILL_CHILDREN_AFTER_SECS - * time left to abort; clamp to 0 if it already expired, or if
(time(NULL) - AbortStartTime); * time goes backwards
*/
if (curtime < AbortStartTime ||
curtime - AbortStartTime >= SIGKILL_CHILDREN_AFTER_SECS)
seconds = 0;
else
seconds = SIGKILL_CHILDREN_AFTER_SECS -
(curtime - AbortStartTime);
return Max(seconds * 1000, 0); return seconds * 1000;
} }
else else
return 60 * 1000; return 60 * 1000;

View File

@ -1636,8 +1636,9 @@ ShutDownSlotSync(void)
/* /*
* SlotSyncWorkerCanRestart * SlotSyncWorkerCanRestart
* *
* Returns true if enough time (SLOTSYNC_RESTART_INTERVAL_SEC) has passed * Return true, indicating worker is allowed to restart, if enough time has
* since it was launched last. Otherwise returns false. * passed since it was last launched to reach SLOTSYNC_RESTART_INTERVAL_SEC.
* Otherwise return false.
* *
* This is a safety valve to protect against continuous respawn attempts if the * This is a safety valve to protect against continuous respawn attempts if the
* worker is dying immediately at launch. Note that since we will retry to * worker is dying immediately at launch. Note that since we will retry to
@ -1649,14 +1650,19 @@ SlotSyncWorkerCanRestart(void)
{ {
time_t curtime = time(NULL); time_t curtime = time(NULL);
/* Return false if too soon since last start. */ /*
if ((unsigned int) (curtime - SlotSyncCtx->last_start_time) < * If first time through, or time somehow went backwards, always update
(unsigned int) SLOTSYNC_RESTART_INTERVAL_SEC) * last_start_time to match the current clock and allow worker start.
return false; * Otherwise allow it only once enough time has elapsed.
*/
SlotSyncCtx->last_start_time = curtime; if (SlotSyncCtx->last_start_time == 0 ||
curtime < SlotSyncCtx->last_start_time ||
return true; curtime - SlotSyncCtx->last_start_time >= SLOTSYNC_RESTART_INTERVAL_SEC)
{
SlotSyncCtx->last_start_time = curtime;
return true;
}
return false;
} }
/* /*