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:
committed by
Serge Hallyn
parent
6e58c12752
commit
efbbcade43
+6
-4
@@ -17,6 +17,8 @@
|
||||
#include <ctype.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "alloc.h"
|
||||
|
||||
#define ID_SIZE 31
|
||||
|
||||
/*
|
||||
@@ -32,7 +34,7 @@ static /*@null@*/ /*@only@*/void *subordinate_dup (const void *ent)
|
||||
const struct subordinate_range *rangeent = ent;
|
||||
struct subordinate_range *range;
|
||||
|
||||
range = (struct subordinate_range *) malloc (sizeof *range);
|
||||
range = MALLOC (struct subordinate_range);
|
||||
if (NULL == range) {
|
||||
return NULL;
|
||||
}
|
||||
@@ -314,12 +316,12 @@ static bool have_range(struct commonio_db *db,
|
||||
static bool append_range(struct subid_range **ranges, const struct subordinate_range *new, int n)
|
||||
{
|
||||
if (!*ranges) {
|
||||
*ranges = malloc(sizeof(struct subid_range));
|
||||
*ranges = MALLOC(struct subid_range);
|
||||
if (!*ranges)
|
||||
return false;
|
||||
} else {
|
||||
struct subid_range *alloced;
|
||||
alloced = reallocarray(*ranges, n + 1, sizeof(struct subid_range));
|
||||
alloced = REALLOCARRAY(*ranges, n + 1, struct subid_range);
|
||||
if (!alloced)
|
||||
return false;
|
||||
*ranges = alloced;
|
||||
@@ -911,7 +913,7 @@ static int append_uids(uid_t **uids, const char *owner, int n)
|
||||
return n;
|
||||
}
|
||||
|
||||
ret = reallocarray(*uids, n + 1, sizeof(uid_t));
|
||||
ret = REALLOCARRAY(*uids, n + 1, uid_t);
|
||||
if (!ret) {
|
||||
free(*uids);
|
||||
return -1;
|
||||
|
||||
Reference in New Issue
Block a user