1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-09 06:21:09 +03:00

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

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