(allocbuf): do not leak memory on failure and zero re-used memory,

from Markus Friedl <markus@openbsd.org>


git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@10657 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
Assar Westerlund
2001-09-04 14:35:58 +00:00
parent c9ca422060
commit e706d29cca

View File

@@ -136,6 +136,7 @@ allocbuf(bp, fd, blksize)
{
struct stat stb;
size_t size;
char *p;
if (fstat(fd, &stb) < 0) {
run_err("fstat: %s", strerror(errno));
@@ -146,11 +147,16 @@ allocbuf(bp, fd, blksize)
size = blksize;
if (bp->cnt >= size)
return (bp);
if ((bp->buf = realloc(bp->buf, size)) == NULL) {
if ((p = realloc(bp->buf, size)) == NULL) {
if (bp->buf)
free(bp->buf);
bp->buf = NULL;
bp->cnt = 0;
run_err("%s", strerror(errno));
return (0);
}
memset(p, 0, size);
bp->buf = p;
bp->cnt = size;
return (bp);
}