From d1c575230d49929f82c6d91e9b3070a9f4018718 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Sun, 4 Jan 2015 15:35:46 +0100
Subject: [PATCH] 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.
---
 contrib/pg_xlogdump/pg_xlogdump.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/contrib/pg_xlogdump/pg_xlogdump.c b/contrib/pg_xlogdump/pg_xlogdump.c
index 9f05e254a86..03fbe46fc46 100644
--- a/contrib/pg_xlogdump/pg_xlogdump.c
+++ b/contrib/pg_xlogdump/pg_xlogdump.c
@@ -171,7 +171,7 @@ fuzzy_open_file(const char *directory, const char *fname)
 		fd = open(fname, O_RDONLY | PG_BINARY, 0);
 		if (fd < 0 && errno != ENOENT)
 			return -1;
-		else if (fd > 0)
+		else if (fd >= 0)
 			return fd;
 
 		/* XLOGDIR / fname */
@@ -180,7 +180,7 @@ fuzzy_open_file(const char *directory, const char *fname)
 		fd = open(fpath, O_RDONLY | PG_BINARY, 0);
 		if (fd < 0 && errno != ENOENT)
 			return -1;
-		else if (fd > 0)
+		else if (fd >= 0)
 			return fd;
 
 		datadir = getenv("PGDATA");
@@ -192,7 +192,7 @@ fuzzy_open_file(const char *directory, const char *fname)
 			fd = open(fpath, O_RDONLY | PG_BINARY, 0);
 			if (fd < 0 && errno != ENOENT)
 				return -1;
-			else if (fd > 0)
+			else if (fd >= 0)
 				return fd;
 		}
 	}
@@ -204,7 +204,7 @@ fuzzy_open_file(const char *directory, const char *fname)
 		fd = open(fpath, O_RDONLY | PG_BINARY, 0);
 		if (fd < 0 && errno != ENOENT)
 			return -1;
-		else if (fd > 0)
+		else if (fd >= 0)
 			return fd;
 
 		/* directory / XLOGDIR / fname */
@@ -213,7 +213,7 @@ fuzzy_open_file(const char *directory, const char *fname)
 		fd = open(fpath, O_RDONLY | PG_BINARY, 0);
 		if (fd < 0 && errno != ENOENT)
 			return -1;
-		else if (fd > 0)
+		else if (fd >= 0)
 			return fd;
 	}
 	return -1;
-- 
GitLab