Skip to content

Commit

Permalink
utils: reduce memory consumption in safe_readlinkat
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Sjölund <[email protected]>
  • Loading branch information
eriksjolund committed Feb 1, 2025
1 parent a029af2 commit b548479
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/libcrun/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ safe_openat (int dirfd, const char *rootfs, size_t rootfs_len, const char *path,
ssize_t
safe_readlinkat (int dfd, const char *name, char **buffer, ssize_t hint, libcrun_error_t *err)
{
/* Add 1 to make room for the NUL terminator. */
ssize_t buf_size = hint > 0 ? hint + 1 : 512;
cleanup_free char *tmp_buf = NULL;
ssize_t size;
Expand All @@ -429,8 +430,7 @@ safe_readlinkat (int dfd, const char *name, char **buffer, ssize_t hint, libcrun
if (tmp_buf != NULL)
buf_size += 256;

/* Allocate an extra byte so the buffer can be NUL terminated. */
tmp_buf = xrealloc (tmp_buf, buf_size + 1);
tmp_buf = xrealloc (tmp_buf, buf_size);

size = readlinkat (dfd, name, tmp_buf, buf_size);
if (UNLIKELY (size < 0))
Expand Down

0 comments on commit b548479

Please sign in to comment.