Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 2b0f8ae

Browse files
committed
Fix pg_dump crashes caused by bogus use of va_start/va_end (only seen
on some platforms, which is not too surprising considering how platform specific these macros must be).
1 parent b25e60d commit 2b0f8ae

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

src/bin/pg_dump/pg_backup_archiver.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -872,21 +872,21 @@ int archprintf(Archive* AH, const char *fmt, ...)
872872
int bSize = strlen(fmt) + 256;
873873
int cnt = -1;
874874

875-
va_start(ap, fmt);
876-
877875
/* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */
878876
/* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */
879-
while (cnt < 0 || cnt >= (bSize-1) ) {
880-
if (p != NULL) free(p);
881-
bSize *= 2;
882-
if ((p = malloc(bSize)) == NULL)
877+
while (cnt < 0 || cnt >= (bSize-1) )
883878
{
884-
va_end(ap);
885-
exit_horribly(AH, "%s: could not allocate buffer for archprintf\n", progname);
886-
}
887-
cnt = vsnprintf(p, bSize, fmt, ap);
879+
if (p != NULL) free(p);
880+
bSize *= 2;
881+
p = (char*)malloc(bSize);
882+
if (p == NULL)
883+
{
884+
exit_horribly(AH, "%s: could not allocate buffer for archprintf\n", progname);
885+
}
886+
va_start(ap, fmt);
887+
cnt = vsnprintf(p, bSize, fmt, ap);
888+
va_end(ap);
888889
}
889-
va_end(ap);
890890
WriteData(AH, p, cnt);
891891
free(p);
892892
return cnt;
@@ -977,21 +977,21 @@ int ahprintf(ArchiveHandle* AH, const char *fmt, ...)
977977
int bSize = strlen(fmt) + 256; /* Should be enough */
978978
int cnt = -1;
979979

980-
va_start(ap, fmt);
981980
/* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */
982981
/* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */
983-
while (cnt < 0 || cnt >= (bSize - 1) ) {
982+
while (cnt < 0 || cnt >= (bSize - 1) )
983+
{
984984
if (p != NULL) free(p);
985985
bSize *= 2;
986986
p = (char*)malloc(bSize);
987987
if (p == NULL)
988988
{
989-
va_end(ap);
990989
die_horribly(AH, "%s: could not allocate buffer for ahprintf\n", progname);
991990
}
991+
va_start(ap, fmt);
992992
cnt = vsnprintf(p, bSize, fmt, ap);
993+
va_end(ap);
993994
}
994-
va_end(ap);
995995
ahwrite(p, 1, cnt, AH);
996996
free(p);
997997
return cnt;

src/bin/pg_dump/pg_backup_tar.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -899,24 +899,22 @@ static int tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt, ...)
899899
int bSize = strlen(fmt) + 256; /* Should be enough */
900900
int cnt = -1;
901901

902-
va_start(ap, fmt);
903902
/* This is paranoid: deal with the possibility that vsnprintf is willing to ignore trailing null */
904903
/* or returns > 0 even if string does not fit. It may be the case that it returns cnt = bufsize */
905-
while (cnt < 0 || cnt >= (bSize - 1) ) {
904+
while (cnt < 0 || cnt >= (bSize - 1) )
905+
{
906906
if (p != NULL) free(p);
907907
bSize *= 2;
908908
p = (char*)malloc(bSize);
909909
if (p == NULL)
910910
{
911-
va_end(ap);
912-
die_horribly(AH, "%s: could not allocate buffer for ahprintf\n", progname);
911+
die_horribly(AH, "%s: could not allocate buffer for tarPrintf\n", progname);
913912
}
913+
va_start(ap, fmt);
914914
cnt = vsnprintf(p, bSize, fmt, ap);
915+
va_end(ap);
915916
}
916-
va_end(ap);
917-
918917
cnt = tarWrite(p, cnt, th);
919-
920918
free(p);
921919
return cnt;
922920
}

0 commit comments

Comments
 (0)