Skip to content

Commit

Permalink
Merge pull request #556 from cgzones/alloc
Browse files Browse the repository at this point in the history
Allocation improvements
  • Loading branch information
smcv authored Feb 15, 2024
2 parents 65a19f2 + 84dc66c commit 7fe0996
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
8 changes: 4 additions & 4 deletions bind-mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ parse_mountinfo (int proc_fd,
die_with_error ("Can't open /proc/self/mountinfo");

n_lines = count_lines (mountinfo);
lines = xcalloc (n_lines * sizeof (MountInfoLine));
lines = xcalloc (n_lines, sizeof (MountInfoLine));

max_id = 0;
line = mountinfo;
Expand Down Expand Up @@ -310,11 +310,11 @@ parse_mountinfo (int proc_fd,

if (root == -1)
{
mount_tab = xcalloc (sizeof (MountInfo) * (1));
mount_tab = xcalloc (1, sizeof (MountInfo));
return steal_pointer (&mount_tab);
}

by_id = xcalloc ((max_id + 1) * sizeof (MountInfoLine*));
by_id = xcalloc (max_id + 1, sizeof (MountInfoLine*));
for (i = 0; i < n_lines; i++)
by_id[lines[i].id] = &lines[i];

Expand Down Expand Up @@ -366,7 +366,7 @@ parse_mountinfo (int proc_fd,
}

n_mounts = count_mounts (&lines[root]);
mount_tab = xcalloc (sizeof (MountInfo) * (n_mounts + 1));
mount_tab = xcalloc (n_mounts + 1, sizeof (MountInfo));

end_tab = collect_mounts (&mount_tab[0], &lines[root]);
assert (end_tab == &mount_tab[n_mounts]);
Expand Down
4 changes: 2 additions & 2 deletions bubblewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ static Type *last_ ## name = NULL; \
static inline Type * \
_ ## name ## _append_new (void) \
{ \
Type *self = xcalloc (sizeof (Type)); \
Type *self = xcalloc (1, sizeof (Type)); \
\
if (last_ ## name != NULL) \
last_ ## name ->next = self; \
Expand Down Expand Up @@ -1720,7 +1720,7 @@ parse_args_recurse (int *argcp,
p++;
}

data_argv = xcalloc (sizeof (char *) * (data_argc + 1));
data_argv = xcalloc (data_argc + 1, sizeof (char *));

i = 0;
p = opt_args_data;
Expand Down
21 changes: 17 additions & 4 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "config.h"

#include "utils.h"
#include <stdint.h>
#include <sys/syscall.h>
#include <sys/socket.h>
#ifdef HAVE_SELINUX
Expand Down Expand Up @@ -143,9 +144,9 @@ xmalloc (size_t size)
}

void *
xcalloc (size_t size)
xcalloc (size_t nmemb, size_t size)
{
void *res = calloc (1, size);
void *res = calloc (nmemb, size);

if (res == NULL)
die_oom ();
Expand All @@ -155,9 +156,13 @@ xcalloc (size_t size)
void *
xrealloc (void *ptr, size_t size)
{
void *res = realloc (ptr, size);
void *res;

if (size != 0 && res == NULL)
assert (size != 0);

res = realloc (ptr, size);

if (res == NULL)
die_oom ();
return res;
}
Expand Down Expand Up @@ -594,6 +599,12 @@ load_file_data (int fd,
{
if (data_len == data_read + 1)
{
if (data_len > SIZE_MAX / 2)
{
errno = EFBIG;
return NULL;
}

data_len *= 2;
data = xrealloc (data, data_len);
}
Expand Down Expand Up @@ -820,6 +831,8 @@ readlink_malloc (const char *pathname)

do
{
if (size > SIZE_MAX / 2)
die ("Symbolic link target pathname too long");
size *= 2;
value = xrealloc (value, size);
n = readlink (pathname, value, size - 1);
Expand Down
2 changes: 1 addition & 1 deletion utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void die_unless_label_valid (const char *label);
void fork_intermediate_child (void);

void *xmalloc (size_t size);
void *xcalloc (size_t size);
void *xcalloc (size_t nmemb, size_t size);
void *xrealloc (void *ptr,
size_t size);
char *xstrdup (const char *str);
Expand Down

0 comments on commit 7fe0996

Please sign in to comment.