Fix various memory leaks.

Submitted by:	rdivacky
Obtained from:	OpenBSD
MFC after:	1 week
This commit is contained in:
Konstantin Belousov 2007-10-17 09:52:08 +00:00
parent afb6c26e9e
commit 7ce8354a9b

View File

@ -85,6 +85,7 @@ bytes(FILE *fp, off_t off)
} }
if (ferror(fp)) { if (ferror(fp)) {
ierr(); ierr();
free(sp);
return 1; return 1;
} }
@ -119,6 +120,8 @@ bytes(FILE *fp, off_t off)
if (len) if (len)
WR(sp, len); WR(sp, len);
} }
free(sp);
return 0; return 0;
} }
@ -140,7 +143,7 @@ lines(FILE *fp, off_t off)
u_int len; u_int len;
char *l; char *l;
} *llines; } *llines;
int ch; int ch, rc;
char *p, *sp; char *p, *sp;
int blen, cnt, recno, wrap; int blen, cnt, recno, wrap;
@ -149,6 +152,7 @@ lines(FILE *fp, off_t off)
bzero(llines, off * sizeof(*llines)); bzero(llines, off * sizeof(*llines));
sp = NULL; sp = NULL;
blen = cnt = recno = wrap = 0; blen = cnt = recno = wrap = 0;
rc = 0;
while ((ch = getc(fp)) != EOF) { while ((ch = getc(fp)) != EOF) {
if (++cnt > blen) { if (++cnt > blen) {
@ -175,7 +179,8 @@ lines(FILE *fp, off_t off)
} }
if (ferror(fp)) { if (ferror(fp)) {
ierr(); ierr();
return 1; rc = 1;
goto done;
} }
if (cnt) { if (cnt) {
llines[recno].l = sp; llines[recno].l = sp;
@ -199,5 +204,10 @@ lines(FILE *fp, off_t off)
for (cnt = 0; cnt < recno; ++cnt) for (cnt = 0; cnt < recno; ++cnt)
WR(llines[cnt].l, llines[cnt].len); WR(llines[cnt].l, llines[cnt].len);
} }
return 0; done:
for (cnt = 0; cnt < off; cnt++)
free(llines[cnt].l);
free(sp);
free(llines);
return (rc);
} }