Skip to content

Commit

Permalink
libkmod: Fix memory leak on error path
Browse files Browse the repository at this point in the history
If realloc fails, do not override the still valid pointer with NULL.
Otherwise freeing the iterator won't free the previously allocated
buffer.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Reviewed-by: Emil Velikov <[email protected]>
Link: #82
Signed-off-by: Lucas De Marchi <[email protected]>
  • Loading branch information
stoeckmann authored and lucasdemarchi committed Aug 22, 2024
1 parent 84eff55 commit fbe86ce
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions libkmod/libkmod-builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,18 @@ static off_t get_string(struct kmod_builtin_iter *iter, off_t offset,
offset += (off_t) partsz;

if (iter->bufsz < linesz + partsz) {
iter->bufsz = linesz + partsz;
iter->buf = realloc(iter->buf, iter->bufsz);
void *tmp;
size_t s;

if (!iter->buf) {
s = linesz + partsz;
tmp = realloc(iter->buf, s);

if (!tmp) {
sv_errno = errno;
goto fail;
}
iter->bufsz = s;
iter->buf = tmp;
}

strncpy(iter->buf + linesz, buf, partsz);
Expand Down

0 comments on commit fbe86ce

Please sign in to comment.