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

@@ -11,6 +11,8 @@
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include "alloc.h"
#include "prototypes.h"
#include "stpeprintf.h"
#include "idmapping.h"
@@ -44,7 +46,7 @@ struct map_range *get_map_ranges(int ranges, int argc, char **argv)
return NULL;
}
mappings = calloc(ranges, sizeof(*mappings));
mappings = CALLOC(ranges, struct map_range);
if (!mappings) {
fprintf(log_get_logfd(), _( "%s: Memory allocation failure\n"),
log_get_progname());
@@ -189,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 = xmalloc(bufsize);
pos = buf = XMALLOCARRAY(bufsize, char);
end = buf + bufsize;
/* Build the mapping command */