Skip to content

Commit 5cea716

Browse files
Fix Y2038 issues with MyStartTime.
Several places treat MyStartTime as a "long", which is only 32 bits wide on some platforms. In reality, MyStartTime is a pg_time_t, i.e., a signed 64-bit integer. This will lead to interesting bugs on the aforementioned systems in 2038 when signed 32-bit integers are no longer sufficient to store Unix time (e.g., "pg_ctl start" hanging). To fix, ensure that MyStartTime is handled as a 64-bit value everywhere. (Of course, users will need to ensure that time_t is 64 bits wide on their system, too.) Co-authored-by: Max Johnson Discussion: https://postgr.es/m/CO1PR07MB905262E8AC270FAAACED66008D682%40CO1PR07MB9052.namprd07.prod.outlook.com Backpatch-through: 12
1 parent efe706e commit 5cea716

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

src/backend/utils/error/elog.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,12 +2538,12 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
25382538
{
25392539
char strfbuf[128];
25402540

2541-
snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x",
2542-
(long) (MyStartTime), MyProcPid);
2541+
snprintf(strfbuf, sizeof(strfbuf) - 1, "%" INT64_MODIFIER "x.%x",
2542+
MyStartTime, MyProcPid);
25432543
appendStringInfo(buf, "%*s", padding, strfbuf);
25442544
}
25452545
else
2546-
appendStringInfo(buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
2546+
appendStringInfo(buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
25472547
break;
25482548
case 'p':
25492549
if (padding != 0)
@@ -2848,7 +2848,7 @@ write_csvlog(ErrorData *edata)
28482848
appendStringInfoChar(&buf, ',');
28492849

28502850
/* session id */
2851-
appendStringInfo(&buf, "%lx.%x", (long) MyStartTime, MyProcPid);
2851+
appendStringInfo(&buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
28522852
appendStringInfoChar(&buf, ',');
28532853

28542854
/* Line number */

src/backend/utils/init/miscinit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,10 +1205,10 @@ CreateLockFile(const char *filename, bool amPostmaster,
12051205
* both datadir and socket lockfiles; although more stuff may get added to
12061206
* the datadir lockfile later.
12071207
*/
1208-
snprintf(buffer, sizeof(buffer), "%d\n%s\n%ld\n%d\n%s\n",
1208+
snprintf(buffer, sizeof(buffer), "%d\n%s\n" INT64_FORMAT "\n%d\n%s\n",
12091209
amPostmaster ? (int) my_pid : -((int) my_pid),
12101210
DataDir,
1211-
(long) MyStartTime,
1211+
MyStartTime,
12121212
PostPortNumber,
12131213
socketDir);
12141214

src/bin/pg_ctl/pg_ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ wait_for_postmaster_start(pgpid_t pm_pid, bool do_checkpoint)
624624
* Allow 2 seconds slop for possible cross-process clock skew.
625625
*/
626626
pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]);
627-
pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]);
627+
pmstart = atoll(optlines[LOCK_FILE_LINE_START_TIME - 1]);
628628
if (pmstart >= start_time - 2 &&
629629
#ifndef WIN32
630630
pmpid == pm_pid

0 commit comments

Comments
 (0)