Skip to content

Commit b255493

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 4a17acd commit b255493

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
@@ -2455,12 +2455,12 @@ log_line_prefix(StringInfo buf, ErrorData *edata)
24552455
{
24562456
char strfbuf[128];
24572457

2458-
snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x",
2459-
(long) (MyStartTime), MyProcPid);
2458+
snprintf(strfbuf, sizeof(strfbuf) - 1, "%" INT64_MODIFIER "x.%x",
2459+
MyStartTime, MyProcPid);
24602460
appendStringInfo(buf, "%*s", padding, strfbuf);
24612461
}
24622462
else
2463-
appendStringInfo(buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
2463+
appendStringInfo(buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
24642464
break;
24652465
case 'p':
24662466
if (padding != 0)
@@ -2734,7 +2734,7 @@ write_csvlog(ErrorData *edata)
27342734
appendStringInfoChar(&buf, ',');
27352735

27362736
/* session id */
2737-
appendStringInfo(&buf, "%lx.%x", (long) MyStartTime, MyProcPid);
2737+
appendStringInfo(&buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
27382738
appendStringInfoChar(&buf, ',');
27392739

27402740
/* Line number */

src/backend/utils/init/miscinit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,10 +1175,10 @@ CreateLockFile(const char *filename, bool amPostmaster,
11751175
* both datadir and socket lockfiles; although more stuff may get added to
11761176
* the datadir lockfile later.
11771177
*/
1178-
snprintf(buffer, sizeof(buffer), "%d\n%s\n%ld\n%d\n%s\n",
1178+
snprintf(buffer, sizeof(buffer), "%d\n%s\n" INT64_FORMAT "\n%d\n%s\n",
11791179
amPostmaster ? (int) my_pid : -((int) my_pid),
11801180
DataDir,
1181-
(long) MyStartTime,
1181+
MyStartTime,
11821182
PostPortNumber,
11831183
socketDir);
11841184

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)