1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-16 16:42:29 +03:00

Refactor pipe_read_line to return the full line

Commit 5b2f4afffe refactored find_other_exec() and in the process
created pipe_read_line() into a static routine for reading a single
line of output, aimed at reading version numbers.  Commit a7e8ece41
later exposed it externally in order to read a postgresql.conf GUC
using "postgres -C ..".  Further, f06b1c598 also made use of it for
reading a version string much like find_other_exec().  The internal
variable remained "pgver", even when used for other purposes.

Since the function requires passing a buffer and its size, and at
most size - 1 bytes will be read via fgets(), there is a truncation
risk when using this for reading GUCs (like how pg_rewind does,
though the risk in this case is marginal).

To keep this as generic functionality for reading a line from a pipe,
this refactors pipe_read_line() into returning an allocated buffer
containing all of the line to remove the risk of silent truncation.

Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi>
Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org>
Reviewed-by: John Naylor <johncnaylorls@gmail.com>
Discussion: https://postgr.es/m/DEDF73CE-D528-49A3-9089-B3592FD671A9@yesql.se
This commit is contained in:
Daniel Gustafsson
2024-02-09 15:03:16 +01:00
parent c01f6ef46c
commit 5c7038d70b
4 changed files with 36 additions and 25 deletions

View File

@@ -1055,8 +1055,7 @@ static void
getRestoreCommand(const char *argv0)
{
int rc;
char postgres_exec_path[MAXPGPATH],
cmd_output[MAXPGPATH];
char postgres_exec_path[MAXPGPATH];
PQExpBuffer postgres_cmd;
if (!restore_wal)
@@ -1105,16 +1104,15 @@ getRestoreCommand(const char *argv0)
/* add -C switch, for restore_command */
appendPQExpBufferStr(postgres_cmd, " -C restore_command");
if (!pipe_read_line(postgres_cmd->data, cmd_output, sizeof(cmd_output)))
exit(1);
restore_command = pipe_read_line(postgres_cmd->data);
if (restore_command == NULL)
pg_fatal("unable to read restore_command from target cluster");
(void) pg_strip_crlf(cmd_output);
(void) pg_strip_crlf(restore_command);
if (strcmp(cmd_output, "") == 0)
if (strcmp(restore_command, "") == 0)
pg_fatal("restore_command is not set in the target cluster");
restore_command = pg_strdup(cmd_output);
pg_log_debug("using for rewind restore_command = \'%s\'",
restore_command);