Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5d6187d

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 f391d9d commit 5d6187d

File tree

6 files changed

+10
-10
lines changed

6 files changed

+10
-10
lines changed

contrib/postgres_fdw/option.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ process_pgfdw_appname(const char *appname)
522522
appendStringInfoString(&buf, application_name);
523523
break;
524524
case 'c':
525-
appendStringInfo(&buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
525+
appendStringInfo(&buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
526526
break;
527527
case 'C':
528528
appendStringInfoString(&buf, cluster_name);

src/backend/utils/error/csvlog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ write_csvlog(ErrorData *edata)
120120
appendStringInfoChar(&buf, ',');
121121

122122
/* session id */
123-
appendStringInfo(&buf, "%lx.%x", (long) MyStartTime, MyProcPid);
123+
appendStringInfo(&buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
124124
appendStringInfoChar(&buf, ',');
125125

126126
/* Line number */

src/backend/utils/error/elog.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2944,12 +2944,12 @@ log_status_format(StringInfo buf, const char *format, ErrorData *edata)
29442944
{
29452945
char strfbuf[128];
29462946

2947-
snprintf(strfbuf, sizeof(strfbuf) - 1, "%lx.%x",
2948-
(long) (MyStartTime), MyProcPid);
2947+
snprintf(strfbuf, sizeof(strfbuf) - 1, "%" INT64_MODIFIER "x.%x",
2948+
MyStartTime, MyProcPid);
29492949
appendStringInfo(buf, "%*s", padding, strfbuf);
29502950
}
29512951
else
2952-
appendStringInfo(buf, "%lx.%x", (long) (MyStartTime), MyProcPid);
2952+
appendStringInfo(buf, "%" INT64_MODIFIER "x.%x", MyStartTime, MyProcPid);
29532953
break;
29542954
case 'p':
29552955
if (padding != 0)

src/backend/utils/error/jsonlog.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ write_jsonlog(ErrorData *edata)
168168
}
169169

170170
/* Session id */
171-
appendJSONKeyValueFmt(&buf, "session_id", true, "%lx.%x",
172-
(long) MyStartTime, MyProcPid);
171+
appendJSONKeyValueFmt(&buf, "session_id", true, "%" INT64_MODIFIER "x.%x",
172+
MyStartTime, MyProcPid);
173173

174174
/* Line number */
175175
appendJSONKeyValueFmt(&buf, "line_num", false, "%ld", log_line_number);

src/backend/utils/init/miscinit.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,10 +1372,10 @@ CreateLockFile(const char *filename, bool amPostmaster,
13721372
* both datadir and socket lockfiles; although more stuff may get added to
13731373
* the datadir lockfile later.
13741374
*/
1375-
snprintf(buffer, sizeof(buffer), "%d\n%s\n%ld\n%d\n%s\n",
1375+
snprintf(buffer, sizeof(buffer), "%d\n%s\n" INT64_FORMAT "\n%d\n%s\n",
13761376
amPostmaster ? (int) my_pid : -((int) my_pid),
13771377
DataDir,
1378-
(long) MyStartTime,
1378+
MyStartTime,
13791379
PostPortNumber,
13801380
socketDir);
13811381

src/bin/pg_ctl/pg_ctl.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ wait_for_postmaster_start(pid_t pm_pid, bool do_checkpoint)
618618
* Allow 2 seconds slop for possible cross-process clock skew.
619619
*/
620620
pmpid = atol(optlines[LOCK_FILE_LINE_PID - 1]);
621-
pmstart = atol(optlines[LOCK_FILE_LINE_START_TIME - 1]);
621+
pmstart = atoll(optlines[LOCK_FILE_LINE_START_TIME - 1]);
622622
if (pmstart >= start_time - 2 &&
623623
#ifndef WIN32
624624
pmpid == pm_pid

0 commit comments

Comments
 (0)