initdb fails to initialize data directory

Started by Nikhil Sontakkeover 7 years ago5 messages
#1Nikhil Sontakke
nikhils@2ndquadrant.com
1 attachment(s)

Hi,

Encountered the following behavior with initdb on one of our test
builds while using latest master head:

initdb -D $DATADIR

The program "postgres" was found by "/Users/nikhils/install/bin/initdb"
but was not the same version as initdb.
Check your installation.

Intrigued, on digging down further, this is happening because we are
not using a long enough buffer to accept the output of "postgres -V"
in the find_other_exec() function. In our case, we had used
--with-extra-version option with configure which caused the output of
"postgres -V" to go a little beyond the current "line" variable size.
This caused the strcmp to fail leading to initdb refusing to
initialize any data directories at all.

PFA, a patch which uses MAXPGPATH for the variable size.

Regards,
Nikhils
--
Nikhil Sontakke http://www.2ndQuadrant.com/
PostgreSQL/Postgres-XL Development, 24x7 Support, Training & Services

Attachments:

initdb_path_variable.patchapplication/octet-stream; name=initdb_path_variable.patchDownload
diff --git a/src/common/exec.c b/src/common/exec.c
index e3e81c1db8..4df16cd64b 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -308,7 +308,7 @@ find_other_exec(const char *argv0, const char *target,
 				const char *versionstr, char *retpath)
 {
 	char		cmd[MAXPGPATH];
-	char		line[100];
+	char		line[MAXPGPATH];
 
 	if (find_my_exec(argv0, retpath) < 0)
 		return -1;
#2Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Nikhil Sontakke (#1)
Re: initdb fails to initialize data directory

Nikhil Sontakke wrote:

Intrigued, on digging down further, this is happening because we are
not using a long enough buffer to accept the output of "postgres -V"
in the find_other_exec() function. In our case, we had used
--with-extra-version option with configure which caused the output of
"postgres -V" to go a little beyond the current "line" variable size.
This caused the strcmp to fail leading to initdb refusing to
initialize any data directories at all.

Wow, that seems pretty silly nowadays.

Will push in a jiffy.

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#3Heikki Linnakangas
hlinnaka@iki.fi
In reply to: Alvaro Herrera (#2)
Re: initdb fails to initialize data directory

On 19/04/18 09:38, Alvaro Herrera wrote:

Nikhil Sontakke wrote:

Intrigued, on digging down further, this is happening because we are
not using a long enough buffer to accept the output of "postgres -V"
in the find_other_exec() function. In our case, we had used
--with-extra-version option with configure which caused the output of
"postgres -V" to go a little beyond the current "line" variable size.
This caused the strcmp to fail leading to initdb refusing to
initialize any data directories at all.

Wow, that seems pretty silly nowadays.

Agreed.

Nitpick: using MAXPGPATH seems for the buffer size seems to wrong to me.
We're not storing a path here. MAXPGPATH is 1024 by default, which seems
fine, but I would've spelled it out directly as "line[1000]".

- Heikki

#4Alvaro Herrera
alvherre@alvh.no-ip.org
In reply to: Heikki Linnakangas (#3)
Re: initdb fails to initialize data directory

Heikki Linnakangas wrote:

Nitpick: using MAXPGPATH seems for the buffer size seems to wrong to me.
We're not storing a path here. MAXPGPATH is 1024 by default, which seems
fine, but I would've spelled it out directly as "line[1000]".

Hmm ... yeah, kinda. Do you care about it strongly enough for me to fix
it in all branches?

--
�lvaro Herrera https://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

#5Michael Paquier
michael@paquier.xyz
In reply to: Alvaro Herrera (#4)
Re: initdb fails to initialize data directory

On Thu, Apr 19, 2018 at 04:59:44PM -0300, Alvaro Herrera wrote:

Heikki Linnakangas wrote:

Nitpick: using MAXPGPATH seems for the buffer size seems to wrong to me.
We're not storing a path here. MAXPGPATH is 1024 by default, which seems
fine, but I would've spelled it out directly as "line[1000]".

Hmm ... yeah, kinda. Do you care about it strongly enough for me to fix
it in all branches?

Perhaps that's not worth bothering, but using MAXPGPATH which is for
file and folder paths for a line read ffrom a command output is
disturbing. So like Heikki I would suggest to just remove the reference
and use a hardcoded, independent, size (could add a comment as well).
--
Michael