Simplify allocation APIs

If we consider simple objects as arrays of size 1, we can considerably
simplify these APIs, merging the *ARRAY and the non-array variants.

That will produce more readable code, since lines will be shorter (by
not having ARRAY in the macro names, as all macros will consistently
handle arrays), and the allocated size will be also more explicit.

The syntax will now be of the form:

    p = MALLOC(42, foo_t);  // allocate 42 elements of type foo_t.
    p = MALLOC(1, bar_t);   // allocate 1 element of type foo_t.

The _array() allocation functions should _never_ be called directly, and
instead these macros should be used.

The non-array functions (e.g., malloc(3)) still have their place, but
are limited to allocating structures with flexible array members.  For
any other uses, the macros should be used.

Thus, we don't use any array or ARRAY variants in any code any more, and
they are only used as implementation details of these macros.

Link: <https://software.codidact.com/posts/285898/288023#answer-288023>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-04-05 21:17:38 +02:00
committed by Serge Hallyn
parent 065a752b42
commit 09775d3718
40 changed files with 115 additions and 120 deletions
+2 -2
View File
@@ -48,7 +48,7 @@ int add_groups (const char *list)
i = 16;
for (;;) {
grouplist = MALLOCARRAY (i, GETGROUPS_T);
grouplist = MALLOC(i, GETGROUPS_T);
if (NULL == grouplist) {
return -1;
}
@@ -90,7 +90,7 @@ int add_groups (const char *list)
fputs (_("Warning: too many groups\n"), shadow_logfd);
break;
}
grouplist = REALLOCARRAYF(grouplist, (size_t) ngroups + 1, GETGROUPS_T);
grouplist = REALLOCF(grouplist, (size_t) ngroups + 1, GETGROUPS_T);
if (grouplist == NULL) {
return -1;
}
+1 -1
View File
@@ -102,7 +102,7 @@ agetpass(const char *prompt)
* Let's add one more byte, and if the password uses it, it
* means the introduced password was longer than PASS_MAX.
*/
pass = MALLOCARRAY(PASS_MAX + 2, char);
pass = MALLOC(PASS_MAX + 2, char);
if (pass == NULL)
return NULL;
+6 -6
View File
@@ -228,7 +228,7 @@ static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, c
return NULL;
}
lp = XMALLOC (struct link_name);
lp = XMALLOC(1, struct link_name);
src_len = strlen (src_orig);
dst_len = strlen (dst_orig);
name_len = strlen (name);
@@ -236,7 +236,7 @@ static /*@exposed@*/ /*@null@*/struct link_name *check_link (const char *name, c
lp->ln_ino = sb->st_ino;
lp->ln_count = sb->st_nlink;
len = name_len - src_len + dst_len + 1;
lp->ln_name = XMALLOCARRAY (len, char);
lp->ln_name = XMALLOC(len, char);
(void) snprintf (lp->ln_name, len, "%s%s", dst_orig, name + src_len);
lp->ln_next = links;
links = lp;
@@ -326,8 +326,8 @@ static int copy_tree_impl (const struct path_info *src, const struct path_info *
src_len += strlen (src->full_path);
dst_len += strlen (dst->full_path);
src_name = MALLOCARRAY (src_len, char);
dst_name = MALLOCARRAY (dst_len, char);
src_name = MALLOC(src_len, char);
dst_name = MALLOC(dst_len, char);
if ((NULL == src_name) || (NULL == dst_name)) {
err = -1;
@@ -561,7 +561,7 @@ static /*@null@*/char *readlink_malloc (const char *filename)
while (true) {
ssize_t nchars;
char *buffer = MALLOCARRAY (size, char);
char *buffer = MALLOC(size, char);
if (NULL == buffer) {
return NULL;
}
@@ -626,7 +626,7 @@ static int copy_symlink (const struct path_info *src, const struct path_info *ds
*/
if (strncmp (oldlink, src_orig, strlen (src_orig)) == 0) {
size_t len = strlen (dst_orig) + strlen (oldlink) - strlen (src_orig) + 1;
char *dummy = XMALLOCARRAY (len, char);
char *dummy = XMALLOC(len, char);
(void) snprintf (dummy, len, "%s%s",
dst_orig,
oldlink + strlen (src_orig));
+3 -3
View File
@@ -60,7 +60,7 @@ static const char *const noslash[] = {
*/
void initenv (void)
{
newenvp = XMALLOCARRAY (NEWENVP_STEP, char *);
newenvp = XMALLOC(NEWENVP_STEP, char *);
*newenvp = NULL;
}
@@ -74,7 +74,7 @@ void addenv (const char *string, /*@null@*/const char *value)
if (NULL != value) {
size_t len = strlen (string) + strlen (value) + 2;
int wlen;
newstring = XMALLOCARRAY (len, char);
newstring = XMALLOC(len, char);
wlen = snprintf (newstring, len, "%s=%s", string, value);
assert (wlen == (int) len -1);
} else {
@@ -137,7 +137,7 @@ void addenv (const char *string, /*@null@*/const char *value)
*/
update_environ = (environ == newenvp);
__newenvp = REALLOCARRAY(newenvp, newenvc + NEWENVP_STEP, char *);
__newenvp = REALLOC(newenvp, newenvc + NEWENVP_STEP, char *);
if (NULL != __newenvp) {
/*
+1 -1
View File
@@ -232,7 +232,7 @@ int find_new_uid(bool sys_user,
*/
/* Create an array to hold all of the discovered UIDs */
used_uids = MALLOCARRAY (uid_max + 1, bool);
used_uids = MALLOC(uid_max + 1, bool);
if (NULL == used_uids) {
fprintf (log_get_logfd(),
_("%s: failed to allocate memory: %s\n"),
+1 -1
View File
@@ -191,7 +191,7 @@ void write_mapping(int proc_dir_fd, int ranges, const struct map_range *mappings
#endif
bufsize = ranges * ((ULONG_DIGITS + 1) * 3);
pos = buf = XMALLOCARRAY(bufsize, char);
pos = buf = XMALLOC(bufsize, char);
end = buf + bufsize;
/* Build the mapping command */
+4 -4
View File
@@ -46,7 +46,7 @@
* old entries, and the new entries as well.
*/
tmp = XMALLOCARRAY (i + 2, char *);
tmp = XMALLOC(i + 2, char *);
/*
* Copy the original list to the new list, then append the
@@ -100,7 +100,7 @@
* old entries.
*/
tmp = XMALLOCARRAY (j + 1, char *);
tmp = XMALLOC(j + 1, char *);
/*
* Copy the original list except the deleted members to the
@@ -135,7 +135,7 @@
for (i = 0; NULL != list[i]; i++);
tmp = XMALLOCARRAY (i + 1, char *);
tmp = XMALLOC(i + 1, char *);
i = 0;
while (NULL != *list) {
@@ -212,7 +212,7 @@ bool is_on_list (char *const *list, const char *member)
* Allocate the array we're going to store the pointers into.
*/
array = XMALLOCARRAY (i, char *);
array = XMALLOC(i, char *);
/*
* Empty list is special - 0 members, not 1 empty member. --marekm
+1 -1
View File
@@ -39,7 +39,7 @@ void mailcheck (void)
size_t len = strlen (mailbox) + 5;
int wlen;
newmail = XMALLOCARRAY (len, char);
newmail = XMALLOC(len, char);
wlen = snprintf (newmail, len, "%s/new", mailbox);
assert (wlen == (int) len - 1);
+1 -1
View File
@@ -109,7 +109,7 @@ static /*@observer@*//*@null@*/const char *password_check (
newmono = str_lower (xstrdup (new));
oldmono = str_lower (xstrdup (old));
wrapped = XMALLOCARRAY (strlen (oldmono) * 2 + 1, char);
wrapped = XMALLOC(strlen(oldmono) * 2 + 1, char);
strcpy (wrapped, oldmono);
strcat (wrapped, oldmono);
+7 -7
View File
@@ -106,18 +106,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
}
size_t len;
len = strlen(prefix) + strlen(PASSWD_FILE) + 2;
passwd_db_file = XMALLOCARRAY(len, char);
passwd_db_file = XMALLOC(len, char);
snprintf(passwd_db_file, len, "%s/%s", prefix, PASSWD_FILE);
pw_setdbname(passwd_db_file);
len = strlen(prefix) + strlen(GROUP_FILE) + 2;
group_db_file = XMALLOCARRAY(len, char);
group_db_file = XMALLOC(len, char);
snprintf(group_db_file, len, "%s/%s", prefix, GROUP_FILE);
gr_setdbname(group_db_file);
#ifdef SHADOWGRP
len = strlen(prefix) + strlen(SGROUP_FILE) + 2;
sgroup_db_file = XMALLOCARRAY(len, char);
sgroup_db_file = XMALLOC(len, char);
snprintf(sgroup_db_file, len, "%s/%s", prefix, SGROUP_FILE);
sgr_setdbname(sgroup_db_file);
#endif
@@ -126,18 +126,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
#endif
len = strlen(prefix) + strlen(SHADOW_FILE) + 2;
spw_db_file = XMALLOCARRAY(len, char);
spw_db_file = XMALLOC(len, char);
snprintf(spw_db_file, len, "%s/%s", prefix, SHADOW_FILE);
spw_setdbname(spw_db_file);
#ifdef ENABLE_SUBIDS
len = strlen(prefix) + strlen("/etc/subuid") + 2;
suid_db_file = XMALLOCARRAY(len, char);
suid_db_file = XMALLOC(len, char);
snprintf(suid_db_file, len, "%s/%s", prefix, "/etc/subuid");
sub_uid_setdbname(suid_db_file);
len = strlen(prefix) + strlen("/etc/subgid") + 2;
sgid_db_file = XMALLOCARRAY(len, char);
sgid_db_file = XMALLOC(len, char);
snprintf(sgid_db_file, len, "%s/%s", prefix, "/etc/subgid");
sub_gid_setdbname(sgid_db_file);
#endif
@@ -146,7 +146,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
setdef_config_file(prefix);
#else
len = strlen(prefix) + strlen("/etc/login.defs") + 2;
def_conf_file = XMALLOCARRAY(len, char);
def_conf_file = XMALLOC(len, char);
snprintf(def_conf_file, len, "%s/%s", prefix, "/etc/login.defs");
setdef_config_file(def_conf_file);
#endif
+1 -1
View File
@@ -36,7 +36,7 @@ addenv_path (const char *varname, const char *dirname, const char *filename)
size_t len = strlen (dirname) + strlen (filename) + 2;
int wlen;
buf = XMALLOCARRAY (len, char);
buf = XMALLOC(len, char);
wlen = snprintf (buf, len, "%s/%s", dirname, filename);
assert (wlen == (int) len - 1);
+3 -3
View File
@@ -92,7 +92,7 @@ static bool is_my_tty (const char tty[UT_LINESIZE])
}
if (NULL != ut) {
ret = XMALLOC (struct utmp);
ret = XMALLOC(1, struct utmp);
memcpy (ret, ut, sizeof (*ret));
}
@@ -157,12 +157,12 @@ static void updwtmp (const char *filename, const struct utmp *ut)
if ( (NULL != host)
&& ('\0' != host[0])) {
hostname = XMALLOCARRAY (strlen (host) + 1, char);
hostname = XMALLOC(strlen(host) + 1, char);
strcpy (hostname, host);
#ifdef HAVE_STRUCT_UTMP_UT_HOST
} else if ( (NULL != ut)
&& ('\0' != ut->ut_host[0])) {
hostname = XMALLOCARRAY (sizeof (ut->ut_host) + 1, char);
hostname = XMALLOC(sizeof(ut->ut_host) + 1, char);
strncpy (hostname, ut->ut_host, sizeof (ut->ut_host));
hostname[sizeof (ut->ut_host)] = '\0';
#endif /* HAVE_STRUCT_UTMP_UT_HOST */
+2 -2
View File
@@ -52,7 +52,7 @@
/* we have to start with something */
size_t length = 0x100;
result = MALLOC(LOOKUP_TYPE);
result = MALLOC(1, LOOKUP_TYPE);
if (NULL == result) {
goto oom;
}
@@ -60,7 +60,7 @@
while (true) {
int status;
LOOKUP_TYPE *resbuf = NULL;
buffer = XREALLOCARRAY (buffer, length, char);
buffer = XREALLOC(buffer, length, char);
status = REENTRANT_NAME(ARG_NAME, result, buffer,
length, &resbuf);
if ((0 == status) && (resbuf == result)) {