shared: use nm_secret_mem_try_realloc_take() in nm_utils_fd_get_contents()
This commit is contained in:
@@ -51,32 +51,6 @@ _get_contents_error (GError **error, int errsv, int *out_errsv, const char *form
|
|||||||
_get_contents_error (error, _errsv, out_errsv, __VA_ARGS__); \
|
_get_contents_error (error, _errsv, out_errsv, __VA_ARGS__); \
|
||||||
})
|
})
|
||||||
|
|
||||||
static char *
|
|
||||||
_mem_realloc (char *old, gboolean do_bzero_mem, gsize cur_len, gsize new_len)
|
|
||||||
{
|
|
||||||
char *new;
|
|
||||||
|
|
||||||
/* re-allocating to zero bytes is an odd case. We don't need it
|
|
||||||
* and it's not supported. */
|
|
||||||
nm_assert (new_len > 0);
|
|
||||||
|
|
||||||
/* regardless of success/failure, @old will always be freed/consumed. */
|
|
||||||
|
|
||||||
if (do_bzero_mem && cur_len > 0) {
|
|
||||||
new = g_try_malloc (new_len);
|
|
||||||
if (new)
|
|
||||||
memcpy (new, old, NM_MIN (cur_len, new_len));
|
|
||||||
nm_explicit_bzero (old, cur_len);
|
|
||||||
g_free (old);
|
|
||||||
} else {
|
|
||||||
new = g_try_realloc (old, new_len);
|
|
||||||
if (!new)
|
|
||||||
g_free (old);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* nm_utils_fd_get_contents:
|
* nm_utils_fd_get_contents:
|
||||||
* @fd: open file descriptor to read. The fd will not be closed,
|
* @fd: open file descriptor to read. The fd will not be closed,
|
||||||
@@ -161,7 +135,7 @@ nm_utils_fd_get_contents (int fd,
|
|||||||
str[n_read] = '\0';
|
str[n_read] = '\0';
|
||||||
|
|
||||||
if (n_read < n_stat) {
|
if (n_read < n_stat) {
|
||||||
if (!(str = _mem_realloc (str, do_bzero_mem, n_stat + 1, n_read + 1)))
|
if (!(str = nm_secret_mem_try_realloc_take (str, do_bzero_mem, n_stat + 1, n_read + 1)))
|
||||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to reallocate buffer with %zu bytes", n_read + 1);
|
return _get_contents_error (error, ENOMEM, out_errsv, "failure to reallocate buffer with %zu bytes", n_read + 1);
|
||||||
}
|
}
|
||||||
NM_SET_OUT (length, n_read);
|
NM_SET_OUT (length, n_read);
|
||||||
@@ -222,7 +196,7 @@ nm_utils_fd_get_contents (int fd,
|
|||||||
n_alloc = NM_MIN (n_read + 1, sizeof (buf));
|
n_alloc = NM_MIN (n_read + 1, sizeof (buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(str = _mem_realloc (str, do_bzero_mem, old_n_alloc, n_alloc))) {
|
if (!(str = nm_secret_mem_try_realloc_take (str, do_bzero_mem, old_n_alloc, n_alloc))) {
|
||||||
if (do_bzero_mem)
|
if (do_bzero_mem)
|
||||||
nm_explicit_bzero (buf, sizeof (buf));
|
nm_explicit_bzero (buf, sizeof (buf));
|
||||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to allocate buffer of %zu bytes", n_alloc);
|
return _get_contents_error (error, ENOMEM, out_errsv, "failure to allocate buffer of %zu bytes", n_alloc);
|
||||||
@@ -241,7 +215,7 @@ nm_utils_fd_get_contents (int fd,
|
|||||||
else {
|
else {
|
||||||
str[n_have] = '\0';
|
str[n_have] = '\0';
|
||||||
if (n_have + 1 < n_alloc) {
|
if (n_have + 1 < n_alloc) {
|
||||||
if (!(str = _mem_realloc (str, do_bzero_mem, n_alloc, n_have + 1)))
|
if (!(str = nm_secret_mem_try_realloc_take (str, do_bzero_mem, n_alloc, n_have + 1)))
|
||||||
return _get_contents_error (error, ENOMEM, out_errsv, "failure to truncate buffer to %zu bytes", n_have + 1);
|
return _get_contents_error (error, ENOMEM, out_errsv, "failure to truncate buffer to %zu bytes", n_have + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user