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-06-08 09:05:39 -05:00
committed by Serge Hallyn
parent 065a752b42
commit 09775d3718
40 changed files with 115 additions and 120 deletions
+1 -1
View File
@@ -836,7 +836,7 @@ static void get_group (struct group *gr)
sg->sg_mem = dup_list (gr->gr_mem);
sg->sg_adm = XMALLOCARRAY (2, char *);
sg->sg_adm = XMALLOC(2, char *);
#ifdef FIRST_MEMBER_IS_ADMIN
if (sg->sg_mem[0]) {
sg->sg_adm[0] = xstrdup (sg->sg_mem[0]);
+4 -4
View File
@@ -127,7 +127,7 @@ static void add_user (const char *user,
static struct sgrp sgrent;
sgrent.sg_name = xstrdup (newgrp->gr_name);
sgrent.sg_mem = dup_list (newgrp->gr_mem);
sgrent.sg_adm = XMALLOC (char *);
sgrent.sg_adm = XMALLOC(1, char *);
#ifdef FIRST_MEMBER_IS_ADMIN
if (sgrent.sg_mem[0]) {
sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
@@ -210,7 +210,7 @@ static void remove_user (const char *user,
static struct sgrp sgrent;
sgrent.sg_name = xstrdup (newgrp->gr_name);
sgrent.sg_mem = dup_list (newgrp->gr_mem);
sgrent.sg_adm = XMALLOC (char *);
sgrent.sg_adm = XMALLOC(1, char *);
#ifdef FIRST_MEMBER_IS_ADMIN
if (sgrent.sg_mem[0]) {
sgrent.sg_adm[0] = xstrdup (sgrent.sg_mem[0]);
@@ -283,9 +283,9 @@ static void purge_members (const struct group *grp)
/* Create a shadow group based on this group */
static struct sgrp sgrent;
sgrent.sg_name = xstrdup (newgrp->gr_name);
sgrent.sg_mem = XMALLOC (char *);
sgrent.sg_mem = XMALLOC(1, char *);
sgrent.sg_mem[0] = NULL;
sgrent.sg_adm = XMALLOC (char *);
sgrent.sg_adm = XMALLOC(1, char *);
sgrent.sg_adm[0] = NULL;
/* Move any password to gshadow */
+4 -4
View File
@@ -251,7 +251,7 @@ static void grp_update (void)
// requested to replace the existing groups
if (NULL != grp.gr_mem[0])
gr_free_members(&grp);
grp.gr_mem = XMALLOC(char *);
grp.gr_mem = XMALLOC(1, char *);
grp.gr_mem[0] = NULL;
} else {
// append to existing groups
@@ -559,15 +559,15 @@ static void prepare_failure_reports (void)
#endif
info_passwd.name = group_name;
gr = XMALLOCARRAY(512, char);
gr = XMALLOC(512, char);
info_group.audit_msg = gr;
gr_end = gr + 512;
#ifdef SHADOWGRP
sgr = XMALLOCARRAY(512, char);
sgr = XMALLOC(512, char);
info_gshadow.audit_msg = sgr;
sgr_end = sgr + 512;
#endif
pw = XMALLOCARRAY(512, char);
pw = XMALLOC(512, char);
info_passwd.audit_msg = pw;
pw_end = pw + 512;
+1 -1
View File
@@ -91,7 +91,7 @@ int main (int argc, char **argv)
GETGROUPS_T *groups;
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
groups = MALLOCARRAY (sys_ngroups, GETGROUPS_T);
groups = MALLOC(sys_ngroups, GETGROUPS_T);
(void) setlocale (LC_ALL, "");
(void) bindtextdomain (PACKAGE, LOCALEDIR);
+1 -1
View File
@@ -66,7 +66,7 @@ static void usage (void)
* work if the system library is recompiled.
*/
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
groups = MALLOCARRAY (sys_ngroups, GETGROUPS_T);
groups = MALLOC(sys_ngroups, GETGROUPS_T);
/*
* See if the -a flag has been given to print out the concurrent
+2 -2
View File
@@ -591,7 +591,7 @@ int main (int argc, char **argv)
#ifdef RLOGIN
if (rflg) {
assert (NULL == username);
username = XMALLOCARRAY (USER_NAME_MAX_LENGTH + 1, char);
username = XMALLOC(USER_NAME_MAX_LENGTH + 1, char);
username[USER_NAME_MAX_LENGTH] = '\0';
if (do_rlogin (hostname, username, USER_NAME_MAX_LENGTH, term, sizeof term)) {
preauth_flag = true;
@@ -908,7 +908,7 @@ int main (int argc, char **argv)
exit (1);
}
preauth_flag = false;
username = XMALLOCARRAY (USER_NAME_MAX_LENGTH + 1, char);
username = XMALLOC(USER_NAME_MAX_LENGTH + 1, char);
username[USER_NAME_MAX_LENGTH] = '\0';
login_prompt (username, USER_NAME_MAX_LENGTH);
+1 -1
View File
@@ -536,7 +536,7 @@ int main (int argc, char **argv)
/* don't use getgroups(0, 0) - it doesn't work on some systems */
i = 16;
for (;;) {
grouplist = XMALLOCARRAY (i, GETGROUPS_T);
grouplist = XMALLOC(i, GETGROUPS_T);
ngroups = getgroups (i, grouplist);
if (i > ngroups && !(ngroups == -1 && errno == EINVAL)) {
break;
+3 -3
View File
@@ -1196,9 +1196,9 @@ int main (int argc, char **argv)
#ifdef USE_PAM
/* keep the list of user/password for later update by PAM */
nusers++;
lines = REALLOCARRAYF(lines, nusers, int);
usernames = REALLOCARRAYF(usernames, nusers, char *);
passwords = REALLOCARRAYF(passwords, nusers, char *);
lines = REALLOCF(lines, nusers, int);
usernames = REALLOCF(usernames, nusers, char *);
passwords = REALLOCF(passwords, nusers, char *);
if (lines == NULL || usernames == NULL || passwords == NULL) {
fprintf (stderr,
_("%s: line %d: %s\n"),
+1 -1
View File
@@ -526,7 +526,7 @@ static char *update_crypt_pw (char *cp)
}
if (lflg && *cp != '!') {
char *newpw = XMALLOCARRAY (strlen (cp) + 2, char);
char *newpw = XMALLOC(strlen(cp) + 2, char);
strcpy (newpw, "!");
strcat (newpw, cp);
+2 -2
View File
@@ -241,7 +241,7 @@ static void execve_shell (const char *shellname,
while (NULL != args[n_args]) {
n_args++;
}
targs = XMALLOCARRAY (n_args + 3, char *);
targs = XMALLOC(n_args + 3, char *);
targs[0] = "sh";
targs[1] = "-";
targs[2] = xstrdup (shellname);
@@ -1200,7 +1200,7 @@ int main (int argc, char **argv)
cp = Basename (shellstr);
}
arg0 = XMALLOCARRAY (strlen (cp) + 2, char);
arg0 = XMALLOC(strlen(cp) + 2, char);
arg0[0] = '-';
strcpy (arg0 + 1, cp);
cp = arg0;
+9 -9
View File
@@ -359,7 +359,7 @@ static void get_defaults (void)
int wlen;
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = MALLOCARRAY(len, char);
default_file = MALLOC(len, char);
if (default_file == NULL)
return;
wlen = snprintf(default_file, len, "%s/%s", prefix, USER_DEFAULTS_FILE);
@@ -472,7 +472,7 @@ static void get_defaults (void)
char* _def_template; /* avoid const warning */
len = strlen(prefix) + strlen(cp) + 2;
_def_template = XMALLOCARRAY(len, char);
_def_template = XMALLOC(len, char);
wlen = snprintf(_def_template, len, "%s/%s", prefix, cp);
assert (wlen == (int) len -1);
def_template = _def_template;
@@ -496,7 +496,7 @@ static void get_defaults (void)
char* _def_usrtemplate; /* avoid const warning */
len = strlen(prefix) + strlen(cp) + 2;
_def_usrtemplate = XMALLOCARRAY(len, char);
_def_usrtemplate = XMALLOC(len, char);
wlen = snprintf(_def_usrtemplate, len, "%s/%s", prefix, cp);
assert (wlen == (int) len -1);
def_usrtemplate = _def_usrtemplate;
@@ -586,7 +586,7 @@ static int set_defaults (void)
len = strlen(prefix) + strlen(NEW_USER_FILE) + 2;
new_file = MALLOCARRAY(len, char);
new_file = MALLOC(len, char);
if (new_file == NULL) {
fprintf (stderr,
_("%s: cannot create new defaults file: %s\n"),
@@ -598,7 +598,7 @@ static int set_defaults (void)
if (prefix[0]) {
len = strlen(prefix) + strlen(USER_DEFAULTS_FILE) + 2;
default_file = MALLOCARRAY(len, char);
default_file = MALLOC(len, char);
if (default_file == NULL) {
fprintf (stderr,
_("%s: cannot create new defaults file: %s\n"),
@@ -1627,7 +1627,7 @@ static void process_flags (int argc, char **argv)
size_t len = strlen (def_home) + strlen (user_name) + 2;
int wlen;
uh = XMALLOCARRAY (len, char);
uh = XMALLOC(len, char);
wlen = snprintf (uh, len, "%s/%s", def_home, user_name);
assert (wlen == (int) len -1);
@@ -1637,7 +1637,7 @@ static void process_flags (int argc, char **argv)
size_t len = strlen(prefix) + strlen(user_home) + 2;
int wlen;
char* _prefix_user_home; /* to avoid const warning */
_prefix_user_home = XMALLOCARRAY(len, char);
_prefix_user_home = XMALLOC(len, char);
wlen = snprintf(_prefix_user_home, len, "%s/%s", prefix, user_home);
assert (wlen == (int) len -1);
prefix_user_home = _prefix_user_home;
@@ -2451,7 +2451,7 @@ static void create_mail (void)
return;
}
size = strlen(prefix) + strlen(spool) + strlen(user_name) + 3;
file = XMALLOCARRAY(size, char);
file = XMALLOC(size, char);
if (prefix[0])
sprintf (file, "%s/%s/%s", prefix, spool, user_name);
else
@@ -2563,7 +2563,7 @@ int main (int argc, char **argv)
#endif
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
user_groups = XMALLOCARRAY (1 + sys_ngroups, char *);
user_groups = XMALLOC(1 + sys_ngroups, char *);
/*
* Initialize the list to be empty
*/
+3 -3
View File
@@ -805,7 +805,7 @@ static int remove_mailbox (void)
}
len = strlen (prefix) + strlen (maildir) + strlen (user_name) + 2;
mailfile = XMALLOCARRAY (len, char);
mailfile = XMALLOC(len, char);
if (prefix[0]) {
(void) snprintf (mailfile, len, "%s/%s/%s",
@@ -919,7 +919,7 @@ static int remove_tcbdir (const char *user_name, uid_t user_id)
return 0;
}
buf = MALLOCARRAY (buflen, char);
buf = MALLOC(buflen, char);
if (NULL == buf) {
fprintf (stderr, _("%s: Can't allocate memory, "
"tcb entry for %s not removed.\n"),
@@ -1131,7 +1131,7 @@ int main (int argc, char **argv)
size_t len = strlen(prefix) + strlen(pwd->pw_dir) + 2;
int wlen;
user_home = XMALLOCARRAY(len, char);
user_home = XMALLOC(len, char);
wlen = snprintf(user_home, len, "%s/%s", prefix, pwd->pw_dir);
assert (wlen == (int) len -1);
}
+7 -7
View File
@@ -345,7 +345,7 @@ static int prepend_range(const char *str, struct ulong_range_list_entry **head)
if (range.first > range.last)
return 0;
entry = MALLOC(struct ulong_range_list_entry);
entry = MALLOC(1, struct ulong_range_list_entry);
if (!entry) {
fprintf (stderr,
_("%s: failed to allocate memory: %s\n"),
@@ -419,7 +419,7 @@ usage (int status)
static char *new_pw_passwd (char *pw_pass)
{
if (Lflg && ('!' != pw_pass[0])) {
char *buf = XMALLOCARRAY (strlen (pw_pass) + 2, char);
char *buf = XMALLOC(strlen(pw_pass) + 2, char);
#ifdef WITH_AUDIT
audit_logger (AUDIT_USER_CHAUTHTOK, Prog,
@@ -1260,12 +1260,12 @@ static void process_flags (int argc, char **argv)
if (prefix[0]) {
size_t len = strlen(prefix) + strlen(user_home) + 2;
int wlen;
prefix_user_home = XMALLOCARRAY(len, char);
prefix_user_home = XMALLOC(len, char);
wlen = snprintf(prefix_user_home, len, "%s/%s", prefix, user_home);
assert (wlen == (int) len -1);
if (user_newhome) {
len = strlen(prefix) + strlen(user_newhome) + 2;
prefix_user_newhome = XMALLOCARRAY(len, char);
prefix_user_newhome = XMALLOC(len, char);
wlen = snprintf(prefix_user_newhome, len, "%s/%s", prefix, user_newhome);
assert (wlen == (int) len -1);
}
@@ -2048,7 +2048,7 @@ static void move_mailbox (void)
return;
}
size = strlen(prefix) + strlen(maildir) + strlen(user_name) + 3;
mailfile = XMALLOCARRAY(size, char);
mailfile = XMALLOC(size, char);
/*
* O_NONBLOCK is to make sure open won't hang on mandatory locks.
@@ -2108,7 +2108,7 @@ static void move_mailbox (void)
size_t newsize;
newsize = strlen(prefix) + strlen(maildir) + strlen(user_newname) + 3;
newmailfile = XMALLOCARRAY(newsize, char);
newmailfile = XMALLOC(newsize, char);
if (prefix[0]) {
(void) snprintf (newmailfile, newsize, "%s/%s/%s",
prefix, maildir, user_newname);
@@ -2168,7 +2168,7 @@ int main (int argc, char **argv)
#endif
sys_ngroups = sysconf (_SC_NGROUPS_MAX);
user_groups = MALLOCARRAY (sys_ngroups + 1, char *);
user_groups = MALLOC(sys_ngroups + 1, char *);
user_groups[0] = NULL;
is_shadow_pwd = spw_file_present ();
+2 -2
View File
@@ -304,7 +304,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
continue;
}
buf = MALLOCARRAY(strlen(editor) + strlen(fileedit) + 2, char);
buf = MALLOC(strlen(editor) + strlen(fileedit) + 2, char);
snprintf (buf, strlen (editor) + strlen (fileedit) + 2,
"%s %s", editor, fileedit);
status = system (buf);
@@ -420,7 +420,7 @@ vipwedit (const char *file, int (*file_lock) (void), int (*file_unlock) (void))
if (stat (file, &st1) != 0) {
vipwexit (_("failed to stat edited file"), errno, 1);
}
to_rename = MALLOCARRAY (strlen (file) + 2, char);
to_rename = MALLOC(strlen(file) + 2, char);
if (NULL == to_rename) {
vipwexit (_("failed to allocate memory"), errno, 1);
}