8000 Fix error message on short read of pg_control · limezra/postgres@29ce500 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
This repository was archived by the owner on Aug 17, 2020. It is now read-only.

Commit 29ce500

Browse files
committed
Fix error message on short read of pg_control
Instead of saying "error: success", indicate that we got a working read but it was too short.
1 parent ee68541 commit 29ce500

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

src/backend/access/transam/xlog.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,6 +4475,7 @@ ReadControlFile(void)
44754475
{
44764476
pg_crc32c crc;
44774477
int fd;
4478+
int r;
44784479

44794480
/*
44804481
* Read data...
@@ -4489,10 +4490,17 @@ ReadControlFile(void)
44894490
XLOG_CONTROL_FILE)));
44904491

44914492 10000
pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_READ);
4492-
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
4493-
ereport(PANIC,
4494-
(errcode_for_file_access(),
4495-
errmsg("could not read from control file: %m")));
4493+
r = read(fd, ControlFile, sizeof(ControlFileData));
4494+
if (r != sizeof(ControlFileData))
4495+
{
4496+
if (r < 0)
4497+
ereport(PANIC,
4498+
(errcode_for_file_access(),
4499+
errmsg("could not read from control file: %m")));
4500+
else
4501+
ereport(PANIC,
4502+
(errmsg("could not read from control file: read %d bytes, expected %d", r, (int) sizeof(ControlFileData))));
4503+
}
44964504
pgstat_report_wait_end();
44974505

44984506
close(fd);

src/common/controldata_utils.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
4444
int fd;
4545
char ControlFilePath[MAXPGPATH];
4646
pg_crc32c crc;
47+
int r;
4748

4849
AssertArg(crc_ok_p);
4950

@@ -64,18 +65,34 @@ get_controlfile(const char *DataDir, const char *progname, bool *crc_ok_p)
6465
}
6566
#endif
6667

67-
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
68+
r = read(fd, ControlFile, sizeof(ControlFileData));
69+
if (r != sizeof(ControlFileData))
70+
{
71+
if (r < 0)
6872
#ifndef FRONTEND
69-
ereport(ERROR,
70-
(errcode_for_file_access(),
71-
errmsg("could not read file \"%s\": %m", ControlFilePath)));
73+
ereport(ERROR,
74+
(errcode_for_file_access(),
75+
errmsg("could not read file \"%s\": %m", ControlFilePath)));
7276
#else
73-
{
74-
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
75-
progname, ControlFilePath, strerror(errno));
76-
exit(EXIT_FAILURE);
77-
}
77+
{
78+
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
79+
progname, ControlFilePath, strerror(errno));
80+
exit(EXIT_FAILURE);
81+
}
7882
#endif
83+
else
84+
#ifndef FRONTEND
85+
ereport(ERROR,
86+
(errmsg("could not read file \"%s\": read %d bytes, expected %d",
87+
ControlFilePath, r, (int) sizeof(ControlFileData))));
88+
#else
89+
{
90+
fprintf(stderr, _("%s: could not read file \"%s\": read %d bytes, expected %d\n"),
91+
progname, ControlFilePath, r, (int) sizeof(ControlFileData));
92+
exit(EXIT_FAILURE);
93+
}
94+
#endif
95+
}
7996

8097
close(fd);
8198

0 commit comments

Comments
 (0)
0