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

@@ -60,7 +60,7 @@ static const char *const noslash[] = {
*/
void initenv (void)
{
newenvp = (char **) xmallocarray (NEWENVP_STEP, sizeof (char *));
newenvp = XMALLOCARRAY (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 = xmalloc (len);
newstring = XMALLOCARRAY (len, char);
wlen = snprintf (newstring, len, "%s=%s", string, value);
assert (wlen == (int) len -1);
} else {
@@ -135,8 +135,7 @@ void addenv (const char *string, /*@null@*/const char *value)
* happily go on, else print a message.
*/
__newenvp = (char **) reallocarray (newenvp, newenvc + NEWENVP_STEP,
sizeof (char *));
__newenvp = REALLOCARRAY(newenvp, newenvc + NEWENVP_STEP, char *);
if (NULL != __newenvp) {
/*