apps/system/hexed/src/bfile.c: Fix memory leak

This commit is contained in:
zhuyanlin 2018-11-07 11:32:07 -06:00 committed by Gregory Nutt
parent 2db0252e61
commit 3de5798ff4

View File

@ -51,6 +51,8 @@
static FAR void *bfallocbuf(FAR struct bfile_s *bf, long sz) static FAR void *bfallocbuf(FAR struct bfile_s *bf, long sz)
{ {
FAR char *buf;
if (bf == NULL) if (bf == NULL)
{ {
return NULL; return NULL;
@ -59,11 +61,13 @@ static FAR void *bfallocbuf(FAR struct bfile_s *bf, long sz)
/* Allocate buffer */ /* Allocate buffer */
sz = BFILE_BUF_ALIGN(sz); sz = BFILE_BUF_ALIGN(sz);
if ((bf->buf = realloc(bf->buf, sz)) == NULL) if ((buf = realloc(bf->buf, sz)) == NULL)
{ {
return NULL; return NULL;
} }
bf->buf = buf;
/* Clear new memory */ /* Clear new memory */
if (sz > bf->bufsz) if (sz > bf->bufsz)
@ -229,6 +233,7 @@ FAR struct bfile_s *bfopen(char *name, char *mode)
bf->size = fsize(bf->fp); bf->size = fsize(bf->fp);
if (bfallocbuf(bf, bf->size) == NULL) if (bfallocbuf(bf, bf->size) == NULL)
{ {
bfclose(bf);
return NULL; return NULL;
} }
@ -329,7 +334,10 @@ long bfinsert(FAR struct bfile_s *bf, long off, void *mem, long sz)
if (bf->bufsz < (bf->size + off + sz)) if (bf->bufsz < (bf->size + off + sz))
{ {
bfallocbuf(bf, bf->size + off + sz); if (bfallocbuf(bf, bf->size + off + sz) == NULL)
{
return EOF;
}
} }
/* Move data */ /* Move data */
@ -510,7 +518,10 @@ long bfwrite(FAR struct bfile_s *bf, long off, void *mem, long sz)
if (bf->bufsz < off + sz) if (bf->bufsz < off + sz)
{ {
bfallocbuf(bf, off + sz); if (bfallocbuf(bf, off + sz) == NULL)
{
return EOF;
}
} }
memmove(bf->buf + off, mem, sz); memmove(bf->buf + off, mem, sz);