On Tue, 3 Mar 2026 at 14:55, Peter Eisentraut <[email protected]> wrote:
>
> I noticed this cast in the committed code:
>
> > + num_total_db = get_dbname_oid_list_from_mfile((char *) inputFileSpec,
> > &dbname_oid_list);
>
> The cast drops the const qualifier from inputFileSpec.
> get_dbname_oid_list_from_mfile() writes into the space pointed to by its
> argument, so it's really not "const".  (And inputFileSpec points into
> argv, so this ends up writing directly into argv.)
>
> Please see if you can clean this up.  It might be best if
> get_dbname_oid_list_from_mfile() made a copy of its argument that it can
> write into, and then the argument can be "const".
>

Thanks Peter.

Here, I am attaching a patch to fix this issue.

-- 
Thanks and Regards
Mahendra Singh Thalor
EnterpriseDB: http://www.enterprisedb.com
From cb744c481ff045a95f11d2a0217aba928ede22db Mon Sep 17 00:00:00 2001
From: Mahendra Singh Thalor <[email protected]>
Date: Tue, 3 Mar 2026 16:43:33 +0530
Subject: [PATCH] pg_restore: don't edit inputfile name, instead use local copy

In get_dbname_oid_list_from_mfile, we are editing filename by passing is
char *, instead of const char*. Fixed this by adding local copy.
---
 src/bin/pg_dump/pg_restore.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/bin/pg_dump/pg_restore.c b/src/bin/pg_dump/pg_restore.c
index 14d886fc86e..54c8b9d48b0 100644
--- a/src/bin/pg_dump/pg_restore.c
+++ b/src/bin/pg_dump/pg_restore.c
@@ -68,7 +68,7 @@ static int	restore_all_databases(const char *inputFileSpec,
 static int	get_dbnames_list_to_restore(PGconn *conn,
 										SimplePtrList *dbname_oid_list,
 										SimpleStringList db_exclude_patterns);
-static int	get_dbname_oid_list_from_mfile(char *dumpdirpath,
+static int	get_dbname_oid_list_from_mfile(const char *dumpdirpatharg,
 										   SimplePtrList *dbname_oid_list);
 
 /*
@@ -1082,14 +1082,18 @@ get_dbnames_list_to_restore(PGconn *conn,
  * Returns, total number of database names in map.dat file.
  */
 static int
-get_dbname_oid_list_from_mfile(char *dumpdirpath, SimplePtrList *dbname_oid_list)
+get_dbname_oid_list_from_mfile(const char *dumpdirpatharg, SimplePtrList *dbname_oid_list)
 {
 	StringInfoData linebuf;
 	FILE	   *pfile;
 	char		map_file_path[MAXPGPATH];
 	int			count = 0;
 	int			len;
+	char			*dumpdirpath;
 
+	len = strlen(dumpdirpatharg);
+	dumpdirpath = pg_malloc0(len + 1);
+	memcpy(dumpdirpath, dumpdirpatharg, len);
 
 	/*
 	 * If there is no map.dat file in dump, then return from here as there is
@@ -1206,7 +1210,7 @@ restore_all_databases(const char *inputFileSpec,
 	if (opts->cparams.dbname)
 		connected_db = opts->cparams.dbname;
 
-	num_total_db = get_dbname_oid_list_from_mfile((char *) inputFileSpec, &dbname_oid_list);
+	num_total_db = get_dbname_oid_list_from_mfile(inputFileSpec, &dbname_oid_list);
 
 	pg_log_info(ngettext("found %d database name in \"%s\"",
 						 "found %d database names in \"%s\"",
-- 
2.52.0

Reply via email to