From fd6bcc84d1684ad65676e0c38fcb5d0bef959b45 Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <mahi6run@gmail.com>
Date: Mon, 24 Mar 2025 23:05:47 +0530
Subject: [PATCH] report fatal, if database name has \n\r

if dbname has \n or \r, then report fatal and add hint to rename
db name.
---
 src/bin/pg_dump/pg_dumpall.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/src/bin/pg_dump/pg_dumpall.c b/src/bin/pg_dump/pg_dumpall.c
index 2935cac2c46..559708b1950 100644
--- a/src/bin/pg_dump/pg_dumpall.c
+++ b/src/bin/pg_dump/pg_dumpall.c
@@ -81,6 +81,7 @@ static void executeCommand(PGconn *conn, const char *query);
 static void expand_dbname_patterns(PGconn *conn, SimpleStringList *patterns,
 								   SimpleStringList *names);
 static void read_dumpall_filters(const char *filename, SimpleStringList *pattern);
+static bool is_name_contain_lfcr(char *name);
 
 static char pg_dump_bin[MAXPGPATH];
 static const char *progname;
@@ -1596,6 +1597,13 @@ dumpDatabases(PGconn *conn)
 		if (strcmp(dbname, "template0") == 0)
 			continue;
 
+		/* Report fatal if database name have \n\r */
+		if (is_name_contain_lfcr(dbname))
+		{
+			pg_fatal("database name has newline or carriage return so stoping dump. To fix, rename dbname.");
+			continue;
+		}
+
 		/* Skip any explicitly excluded database */
 		if (simple_string_list_member(&database_exclude_names, dbname))
 		{
@@ -2068,3 +2076,22 @@ read_dumpall_filters(const char *filename, SimpleStringList *pattern)
 
 	filter_free(&fstate);
 }
+
+/*
+ * is_name_contain_lfcr
+ *
+ * If dbame has \n or \r in the name, then will return true.
+ */
+static bool
+is_name_contain_lfcr(char *name)
+{
+	const char *p;
+
+	for (p = name; *p; p++)
+	{
+		if (*p == '\n' || *p == '\r')
+			return true;
+	}
+
+	return false;
+}
-- 
2.39.3

