To reproduce:

1. initdb -D data
2. cat /dev/null > data/postgresql.conf
3. pg_ctl -w -D data start

I attached a quick patch that seems to do the trick. It appears that
fgets() will always return non-NULL if the size passed in is 1 (i.e.
maxlength in the caller is 0).

The patch also changes the same readfile() function in initdb.c. I
assume it's not a practical problem there, but it should be fixed.

Thanks to Corry Haines (chaines at truviso dot com) for reporting the
problem.

Regards,
        Jeff Davis
diff --git a/src/bin/initdb/initdb.c b/src/bin/initdb/initdb.c
index 4b0b723..e544e3e 100644
--- a/src/bin/initdb/initdb.c
+++ b/src/bin/initdb/initdb.c
@@ -409,11 +409,10 @@ readfile(char *path)
 
 	rewind(infile);
 	nlines = 0;
-	while (fgets(buffer, maxlength + 1, infile) != NULL)
-	{
-		result[nlines] = xstrdup(buffer);
-		nlines++;
-	}
+
+	if (maxlength > 0)
+		while (fgets(buffer, maxlength + 1, infile) != NULL)
+			result[nlines++] = xstrdup(buffer);
 
 	fclose(infile);
 	free(buffer);
diff --git a/src/bin/pg_ctl/pg_ctl.c b/src/bin/pg_ctl/pg_ctl.c
index 08e38e7..ede6e5b 100644
--- a/src/bin/pg_ctl/pg_ctl.c
+++ b/src/bin/pg_ctl/pg_ctl.c
@@ -329,8 +329,10 @@ readfile(const char *path)
 	/* now reprocess the file and store the lines */
 	rewind(infile);
 	nlines = 0;
-	while (fgets(buffer, maxlength + 1, infile) != NULL)
-		result[nlines++] = xstrdup(buffer);
+
+	if (maxlength > 0)
+		while (fgets(buffer, maxlength + 1, infile) != NULL)
+			result[nlines++] = xstrdup(buffer);
 
 	fclose(infile);
 	free(buffer);
-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Reply via email to