Skip to content

Commit f0e2770

Browse files
committed
Fix off-by-one in pg_xlogdump's fuzzy_open_file().
In the unlikely case of stdin (fd 0) being closed, the off-by-one would lead to pg_xlogdump failing to open files. Spotted by Coverity. Backpatch to 9.3 where pg_xlogdump was introduced.
1 parent d33f36f commit f0e2770

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

contrib/pg_xlogdump/pg_xlogdump.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fuzzy_open_file(const char *directory, const char *fname)
151151
fd = open(fname, O_RDONLY | PG_BINARY, 0);
152152
if (fd < 0 && errno != ENOENT)
153153
return -1;
154-
else if (fd > 0)
154+
else if (fd >= 0)
155155
return fd;
156156

157157
/* XLOGDIR / fname */
@@ -160,7 +160,7 @@ fuzzy_open_file(const char *directory, const char *fname)
160160
fd = open(fpath, O_RDONLY | PG_BINARY, 0);
161161
if (fd < 0 && errno != ENOENT)
162162
return -1;
163-
else if (fd > 0)
163+
else if (fd >= 0)
164164
return fd;
165165

166166
datadir = getenv("PGDATA");
@@ -172,7 +172,7 @@ fuzzy_open_file(const char *directory, const char *fname)
172172
fd = open(fpath, O_RDONLY | PG_BINARY, 0);
173173
if (fd < 0 && errno != ENOENT)
174174
return -1;
175-
else if (fd > 0)
175+
else if (fd >= 0)
176176
return fd;
177177
}
178178
}
@@ -184,7 +184,7 @@ fuzzy_open_file(const char *directory, const char *fname)
184184
fd = open(fpath, O_RDONLY | PG_BINARY, 0);
185185
if (fd < 0 && errno != ENOENT)
186186
return -1;
187-
else if (fd > 0)
187+
else if (fd >= 0)
188188
return fd;
189189

190190
/* directory / XLOGDIR / fname */
@@ -193,7 +193,7 @@ fuzzy_open_file(const char *directory, const char *fname)
193193
fd = open(fpath, O_RDONLY | PG_BINARY, 0);
194194
if (fd < 0 && errno != ENOENT)
195195
return -1;
196-
else if (fd > 0)
196+
else if (fd >= 0)
197197
return fd;
198198
}
199199
return -1;

0 commit comments

Comments
 (0)