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

Commit 830e8e3

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 4a9b44d commit 830e8e3

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
@@ -4311,6 +4311,7 @@ ReadControlFile(void)
43114311
{
43124312
pg_crc32c crc;
43134313
int fd;
4314+
int r;
43144315

43154316
/*
43164317
* Read data...
@@ -4324,10 +4325,17 @@ ReadControlFile(void)
43244325
errmsg("could not open control file \"%s\": %m",
43254326
XLOG_CONTROL_FILE)));
43264327

4327-
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
4328-
ereport(PANIC,
4329-
(errcode_for_file_access(),
4330-
errmsg("could not read from control file: %m")));
4328+
r = read(fd, ControlFile, sizeof(ControlFileData));
4329+
if (r != sizeof(ControlFileData))
4330+
{
4331+
if (r < 0)
4332+
ereport(PANIC,
4333+
(errcode_for_file_access(),
4334+
errmsg("could not read from control file: %m")));
4335+
else
4336+
ereport(PANIC,
4337+
(errmsg("could not read from control file: read %d bytes, expected %d", r, (int) sizeof(ControlFileData))));
4338+
}
43314339

43324340
close(fd);
43334341

src/common/controldata_utils.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ get_controlfile(char *DataDir, const char *progname)
4141
int fd;
4242
char ControlFilePath[MAXPGPATH];
4343
pg_crc32c crc;
44+
int r;
4445

4546
ControlFile = palloc(sizeof(ControlFileData));
4647
snprintf(ControlFilePath, MAXPGPATH, "%s/global/pg_control", DataDir);
@@ -59,18 +60,34 @@ get_controlfile(char *DataDir, const char *progname)
5960
}
6061
#endif
6162

62-
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
63+
r = read(fd, ControlFile, sizeof(ControlFileData));
64+
if (r != sizeof(ControlFileData))
65+
{
66+
if (r < 0)
6367
#ifndef FRONTEND
64-
ereport(ERROR,
65-
(errcode_for_file_access(),
66-
errmsg("could not read file \"%s\": %m", ControlFilePath)));
68+
ereport(ERROR,
69+
(errcode_for_file_access(),
70+
errmsg("could not read file \"%s\": %m", ControlFilePath)));
6771
#else
68-
{
69-
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
70-
progname, ControlFilePath, strerror(errno));
71-
exit(EXIT_FAILURE);
72-
}
72+
{
73+
fprintf(stderr, _("%s: could not read file \"%s\": %s\n"),
74+
progname, ControlFilePath, strerror(errno));
75+
exit(EXIT_FAILURE);
76+
}
7377
#endif
78+
else
79+
#ifndef FRONTEND
80+
ereport(ERROR,
81+
(errmsg("could not read file \"%s\": read %d bytes, expected %d",
82+
ControlFilePath, r, (int) sizeof(ControlFileData))));
83+
#else
84+
{
85+
fprintf(stderr, _("%s: could not read file \"%s\": read %d bytes, expected %d\n"),
86+
progname, ControlFilePath, r, (int) sizeof(ControlFileData));
87+
exit(EXIT_FAILURE);
88+
}
89+
#endif
90+
}
7491

7592
close(fd);
7693

0 commit comments

Comments
 (0)