Skip to content

Commit aba8943

Browse files
committed
pg_upgrade: Improve error checking in rewriteVisibilityMap.
In the old logic, if read() were to return an error, we'd silently stop rewriting the visibility map at that point in the file. That's safe, but reporting the error is better, so do that instead. Report by Andres Freund. Patch by Masahiko Sawada, with one correction by me.
1 parent 6201a8e commit aba8943

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/bin/pg_upgrade/file.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile, bool force)
165165
int dst_fd = 0;
166166
char buffer[BLCKSZ];
167167
ssize_t bytesRead;
168+
ssize_t totalBytesRead = 0;
168169
ssize_t src_filesize;
169170
int rewriteVmBytesPerPage;
170171
BlockNumber new_blkno = 0;
@@ -200,13 +201,23 @@ rewriteVisibilityMap(const char *fromfile, const char *tofile, bool force)
200201
* page is empty, we skip it, mostly to avoid turning one-page visibility
201202
* maps for small relations into two pages needlessly.
202203
*/
203-
while ((bytesRead = read(src_fd, buffer, BLCKSZ)) == BLCKSZ)
204+
while (totalBytesRead < src_filesize)
204205
{
205206
char *old_cur;
206207
char *old_break;
207208
char *old_blkend;
208209
PageHeaderData pageheader;
209-
bool old_lastblk = ((BLCKSZ * (new_blkno + 1)) == src_filesize);
210+
bool old_lastblk;
211+
212+
if ((bytesRead = read(src_fd, buffer, BLCKSZ)) != BLCKSZ)
213+
{
214+
close(dst_fd);
215+
close(src_fd);
216+
return getErrorText();
217+
}
218+
219+
totalBytesRead += BLCKSZ;
220+
old_lastblk = (totalBytesRead == src_filesize);
210221

211222
/* Save the page header data */
212223
memcpy(&pageheader, buffer, SizeOfPageHeaderData);

0 commit comments

Comments
 (0)