Skip to content

Commit cfb758b

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 a194106 commit cfb758b

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
@@ -4486,6 +4486,7 @@ ReadControlFile(void)
44864486
pg_crc32c crc;
44874487
int fd;
44884488
static char wal_segsz_str[20];
4489+
int r;
44894490

44904491
/*
44914492
* Read data...
@@ -4499,10 +4500,17 @@ ReadControlFile(void)
44994500
XLOG_CONTROL_FILE)));
45004501

45014502
pgstat_report_wait_start(WAIT_EVENT_CONTROL_FILE_READ);
4502-
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
4503-
ereport(PANIC,
4504-
(errcode_for_file_access(),
4505-
errmsg("could not read from control file: %m")));
4503+
r = read(fd, ControlFile, sizeof(ControlFileData));
4504+
if (r != sizeof(ControlFileData))
4505+
{
4506+
if (r < 0)
4507+
ereport(PANIC,
4508+
(errcode_for_file_access(),
4509+
errmsg("could not read from control file: %m")));
4510+
else
4511+
ereport(PANIC,
4512+
(errmsg("could not read from control file: read %d bytes, expected %d", r, (int) sizeof(ControlFileData))));
4513+
}
45064514
pgstat_report_wait_end();
45074515

45084516
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy