diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index ace7387eda..e1dc031c24 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -704,12 +704,15 @@ check_proper_datallowconn(ClusterInfo *cluster)
 	FILE	   *script = NULL;
 	char		output_path[MAXPGPATH];
 	bool		found = false;
+	int			len;
 
 	prep_status("Checking database connection settings");
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir,
-			 "databases_with_datallowconn_false.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir,
+				   "databases_with_datallowconn_false.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	conn_template1 = connectToServer(cluster, "template1");
 
@@ -823,6 +826,7 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
 	FILE	   *script = NULL;
 	bool		found = false;
 	char		output_path[MAXPGPATH];
+	int			len;
 
 	prep_status("Checking for contrib/isn with bigint-passing mismatch");
 
@@ -834,9 +838,11 @@ check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
 		return;
 	}
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir,
-			 "contrib_isn_and_int8_pass_by_value.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir,
+				   "contrib_isn_and_int8_pass_by_value.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
 	{
@@ -909,12 +915,15 @@ check_for_user_defined_postfix_ops(ClusterInfo *cluster)
 	FILE	   *script = NULL;
 	bool		found = false;
 	char		output_path[MAXPGPATH];
+	int			len;
 
 	prep_status("Checking for user-defined postfix operators");
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir,
-			 "postfix_ops.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir,
+				   "postfix_ops.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	/* Find any user defined postfix operators */
 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
@@ -1009,12 +1018,15 @@ check_for_tables_with_oids(ClusterInfo *cluster)
 	FILE	   *script = NULL;
 	bool		found = false;
 	char		output_path[MAXPGPATH];
+	int			len;
 
 	prep_status("Checking for tables WITH OIDS");
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir,
-			 "tables_with_oids.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir,
+				   "tables_with_oids.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	/* Find any tables declared WITH OIDS */
 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
@@ -1265,12 +1277,15 @@ check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
 	FILE	   *script = NULL;
 	bool		found = false;
 	char		output_path[MAXPGPATH];
+	int			len;
 
 	prep_status("Checking for user-defined encoding conversions");
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir,
-			 "encoding_conversions.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir,
+				   "encoding_conversions.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	/* Find any user defined encoding conversions */
 	for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
diff --git a/src/bin/pg_upgrade/function.c b/src/bin/pg_upgrade/function.c
index ea785df771..4f65726ed9 100644
--- a/src/bin/pg_upgrade/function.c
+++ b/src/bin/pg_upgrade/function.c
@@ -125,11 +125,14 @@ check_loadable_libraries(void)
 	FILE	   *script = NULL;
 	bool		found = false;
 	char		output_path[MAXPGPATH];
+	int			len;
 
 	prep_status("Checking for presence of required libraries");
 
-	snprintf(output_path, sizeof(output_path), "%s/%s",
-			 log_opts.basedir, "loadable_libraries.txt");
+	len = snprintf(output_path, sizeof(output_path), "%s/%s",
+				   log_opts.basedir, "loadable_libraries.txt");
+	if (len >= sizeof(output_path))
+		pg_fatal("directory path for new cluster is too long\n");
 
 	/*
 	 * Now we want to sort the library names into order.  This avoids multiple
