Skip to content

Commit 8485a25

Browse files
committed
Fix assorted infelicities in new SetWALSegSize() function.
* Failure to check for malloc failure (ok, pretty unlikely here, but that's not an excuse). * Leakage of open fd on read error, and of malloc'd buffer always. * Incorrect assumption that a short read would set errno to zero. * Failure to adhere to message style conventions (in particular, not reporting errno where relevant; using "couldn't open" rather than "could not open" is not really in line with project style either). * Missing newlines on some messages. Coverity spotted the leak problems; I noticed the rest while fixing the leaks.
1 parent 6dda099 commit 8485a25

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

contrib/pg_standby/pg_standby.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,21 @@ SetWALSegSize(void)
408408
{
409409
bool ret_val = false;
410410
int fd;
411-
char *buf = (char *) malloc(XLOG_BLCKSZ);
411+
412+
/* malloc this buffer to ensure sufficient alignment: */
413+
char *buf = (char *) pg_malloc(XLOG_BLCKSZ);
412414

413415
Assert(WalSegSz == -1);
414416

415417
if ((fd = open(WALFilePath, O_RDWR, 0)) < 0)
416418
{
417-
fprintf(stderr, "%s: couldn't open WAL file \"%s\"\n",
418-
progname, WALFilePath);
419+
fprintf(stderr, "%s: could not open WAL file \"%s\": %s\n",
420+
progname, WALFilePath, strerror(errno));
421+
pg_free(buf);
419422
return false;
420423
}
424+
425+
errno = 0;
421426
if (read(fd, buf, XLOG_BLCKSZ) == XLOG_BLCKSZ)
422427
{
423428
XLogLongPageHeader longhdr = (XLogLongPageHeader) buf;
@@ -433,7 +438,6 @@ SetWALSegSize(void)
433438
fprintf(stderr,
434439
"%s: WAL segment size must be a power of two between 1MB and 1GB, but the WAL file header specifies %d bytes\n",
435440
progname, WalSegSz);
436-
close(fd);
437441
}
438442
else
439443
{
@@ -444,17 +448,21 @@ SetWALSegSize(void)
444448
if (errno != 0)
445449
{
446450
if (debug)
447-
fprintf(stderr, "could not read file \"%s\": %s",
451+
fprintf(stderr, "could not read file \"%s\": %s\n",
448452
WALFilePath, strerror(errno));
449453
}
450454
else
451455
{
452456
if (debug)
453-
fprintf(stderr, "not enough data in file \"%s\"", WALFilePath);
457+
fprintf(stderr, "not enough data in file \"%s\"\n",
458+
WALFilePath);
454459
}
455460
}
456461

457462
fflush(stderr);
463+
464+
close(fd);
465+
pg_free(buf);
458466
return ret_val;
459467
}
460468

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy