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);
