Use safer allocation macros

Use of these macros, apart from the benefits mentioned in the commit
that adds the macros, has some other good side effects:

-  Consistency in getting the size of the object from sizeof(type),
   instead of a mix of sizeof(type) sometimes and sizeof(*p) other
   times.

-  More readable code: no casts, and no sizeof(), so also shorter lines
   that we don't need to cut.

-  Consistency in using array allocation calls for allocations of arrays
   of objects, even when the object size is 1.

Cc: Valentin V. Bartenev <vbartenev@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This commit is contained in:
Alejandro Colomar
2023-02-04 22:41:18 +01:00
committed by Serge Hallyn
parent 6e58c12752
commit efbbcade43
44 changed files with 196 additions and 118 deletions

View File

@@ -20,6 +20,8 @@
#include <netdb.h>
#include <stdio.h>
#include "alloc.h"
#ident "$Id$"
@@ -93,7 +95,7 @@ static bool is_my_tty (const char *tty)
}
if (NULL != ut) {
ret = (struct utmp *) xmalloc (sizeof (*ret));
ret = XMALLOC (struct utmp);
memcpy (ret, ut, sizeof (*ret));
}
@@ -158,12 +160,12 @@ static void updwtmp (const char *filename, const struct utmp *ut)
if ( (NULL != host)
&& ('\0' != host[0])) {
hostname = (char *) xmalloc (strlen (host) + 1);
hostname = XMALLOCARRAY (strlen (host) + 1, char);
strcpy (hostname, host);
#ifdef HAVE_STRUCT_UTMP_UT_HOST
} else if ( (NULL != ut)
&& ('\0' != ut->ut_host[0])) {
hostname = (char *) xmalloc (sizeof (ut->ut_host) + 1);
hostname = XMALLOCARRAY (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 */
@@ -174,7 +176,7 @@ static void updwtmp (const char *filename, const struct utmp *ut)
}
utent = (struct utmp *) xcalloc (1, sizeof (*utent));
utent = XCALLOC (1, struct utmp);
#ifdef HAVE_STRUCT_UTMP_UT_TYPE