Skip to content

Commit 2f397a0

Browse files
committed
Clean up pg_basebackup libpq usage
When using libpq, it's generally preferrable to just use the strings which are in the PQ structures instead of copying them out, so do that instead in BaseBackup(), eliminating the strcpy()'s used there. Also, in ReceiveAndUnpackTarFile(), check the string length for the directory returned by the server for the tablespace path.
1 parent 3cb7a39 commit 2f397a0

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/bin/pg_basebackup/pg_basebackup.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,15 @@ ReceiveAndUnpackTarFile(PGconn *conn, PGresult *res, int rownum)
509509
if (PQgetisnull(res, rownum, 0))
510510
strcpy(current_path, basedir);
511511
else
512+
{
513+
if (PQgetlength(res, rownum, 1) >= MAXPGPATH)
514+
{
515+
fprintf(stderr, _("%s: received invalid directory (too long): %s"),
516+
progname, PQgetvalue(res, rownum, 1));
517+
disconnect_and_exit(1);
518+
}
512519
strcpy(current_path, PQgetvalue(res, rownum, 1));
520+
}
513521

514522
/*
515523
* Make sure we're unpacking into an empty directory
@@ -814,8 +822,6 @@ BaseBackup(void)
814822
char current_path[MAXPGPATH];
815823
char escaped_label[MAXPGPATH];
816824
int i;
817-
char xlogstart[64];
818-
char xlogend[64];
819825
int minServerMajor,
820826
maxServerMajor;
821827
int serverMajor;
@@ -877,11 +883,9 @@ BaseBackup(void)
877883
progname);
878884
disconnect_and_exit(1);
879885
}
880-
strcpy(xlogstart, PQgetvalue(res, 0, 0));
881886
if (verbose && includewal)
882-
fprintf(stderr, "xlog start point: %s\n", xlogstart);
887+
fprintf(stderr, "xlog start point: %s\n", PQgetvalue(res, 0, 0));
883888
PQclear(res);
884-
MemSet(xlogend, 0, sizeof(xlogend));
885889

886890
/*
887891
* Get the header
@@ -962,9 +966,8 @@ BaseBackup(void)
962966
progname);
963967
disconnect_and_exit(1);
964968
}
965-
strcpy(xlogend, PQgetvalue(res, 0, 0));
966969
if (verbose && includewal)
967-
fprintf(stderr, "xlog end point: %s\n", xlogend);
970+
fprintf(stderr, "xlog end point: %s\n", PQgetvalue(res, 0, 0));
968971
PQclear(res);
969972

970973
res = PQgetResult(conn);

0 commit comments

Comments
 (0)