mirror of
https://github.com/postgres/postgres.git
synced 2025-11-25 12:03:53 +03:00
Use improved vsnprintf calling logic in more places.
When we are using a C99-compliant vsnprintf implementation (which should be most places, these days) it is worth the trouble to make use of its report of how large the buffer needs to be to succeed. This patch adjusts stringinfo.c and some miscellaneous usages in pg_dump to do that, relying on the logic recently added in libpgcommon's psprintf.c. Since these places want to know the number of bytes written once we succeed, modify the API of pvsnprintf() to report that. There remains near-duplicate logic in pqexpbuffer.c, but since that code is in libpq, psprintf.c's approach of exit()-on-error isn't appropriate for use there. Also note that I didn't bother touching the multitude of places that call (v)snprintf without any attempt to provide a resizable buffer. Release-note-worthy incompatibility: the API of appendStringInfoVA() changed. If there's any third-party code that's calling that directly, it will need tweaking along the same lines as in this patch. David Rowley and Tom Lane
This commit is contained in:
@@ -996,33 +996,33 @@ _EndBlobs(ArchiveHandle *AH, TocEntry *te)
|
||||
static int
|
||||
tarPrintf(ArchiveHandle *AH, TAR_MEMBER *th, const char *fmt,...)
|
||||
{
|
||||
char *p = NULL;
|
||||
va_list ap;
|
||||
size_t bSize = strlen(fmt) + 256; /* Should be enough */
|
||||
int cnt = -1;
|
||||
char *p;
|
||||
size_t len = 128; /* initial assumption about buffer size */
|
||||
size_t cnt;
|
||||
|
||||
/*
|
||||
* This is paranoid: deal with the possibility that vsnprintf is willing
|
||||
* to ignore trailing null
|
||||
*/
|
||||
|
||||
/*
|
||||
* or returns > 0 even if string does not fit. It may be the case that it
|
||||
* returns cnt = bufsize
|
||||
*/
|
||||
while (cnt < 0 || cnt >= (bSize - 1))
|
||||
for (;;)
|
||||
{
|
||||
if (p != NULL)
|
||||
free(p);
|
||||
bSize *= 2;
|
||||
p = (char *) pg_malloc(bSize);
|
||||
va_start(ap, fmt);
|
||||
cnt = vsnprintf(p, bSize, fmt, ap);
|
||||
va_end(ap);
|
||||
va_list args;
|
||||
|
||||
/* Allocate work buffer. */
|
||||
p = (char *) pg_malloc(len);
|
||||
|
||||
/* Try to format the data. */
|
||||
va_start(args, fmt);
|
||||
cnt = pvsnprintf(p, len, fmt, args);
|
||||
va_end(args);
|
||||
|
||||
if (cnt < len)
|
||||
break; /* success */
|
||||
|
||||
/* Release buffer and loop around to try again with larger len. */
|
||||
free(p);
|
||||
len = cnt;
|
||||
}
|
||||
|
||||
cnt = tarWrite(p, cnt, th);
|
||||
free(p);
|
||||
return cnt;
|
||||
return (int) cnt;
|
||||
}
|
||||
|
||||
bool
|
||||
|
||||
Reference in New Issue
Block a user