Re: [PATCH v2] recordmcount: Fix memory leaks in the uwrite function

From: Steven Rostedt
Date: Tue Apr 25 2023 - 13:04:42 EST


On Tue, 25 Apr 2023 10:46:03 +0800
Hao Zeng <zenghao@xxxxxxxxxx> wrote:

> @@ -117,7 +118,13 @@ static ssize_t uwrite(void const *const buf, size_t const count)
> off_t aoffset = (file_ptr + count) - file_end;
>
> if (aoffset > file_append_size) {
> - file_append = realloc(file_append, aoffset);
> + p = realloc(file_append, aoffset);
> + if (!p) {
> + free(file_append);
> + file_append = NULL;
> + } else {
> + file_append = p;
> + }

You can simplify the above with:

if (!p)
free(file_append);

file_append = p;

-- Steve

> file_append_size = aoffset;
> }
> if (!file_append) {