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
@@ -11,7 +11,9 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "alloc.h"
|
||||
#include "prototypes.h"
|
||||
/*@-exitarg@*/
|
||||
#include "exitcodes.h"
|
||||
@@ -95,18 +97,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
|
||||
}
|
||||
size_t len;
|
||||
len = strlen(prefix) + strlen(PASSWD_FILE) + 2;
|
||||
passwd_db_file = xmalloc(len);
|
||||
passwd_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(passwd_db_file, len, "%s/%s", prefix, PASSWD_FILE);
|
||||
pw_setdbname(passwd_db_file);
|
||||
|
||||
len = strlen(prefix) + strlen(GROUP_FILE) + 2;
|
||||
group_db_file = xmalloc(len);
|
||||
group_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(group_db_file, len, "%s/%s", prefix, GROUP_FILE);
|
||||
gr_setdbname(group_db_file);
|
||||
|
||||
#ifdef SHADOWGRP
|
||||
len = strlen(prefix) + strlen(SGROUP_FILE) + 2;
|
||||
sgroup_db_file = xmalloc(len);
|
||||
sgroup_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(sgroup_db_file, len, "%s/%s", prefix, SGROUP_FILE);
|
||||
sgr_setdbname(sgroup_db_file);
|
||||
#endif
|
||||
@@ -115,18 +117,18 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
|
||||
#endif
|
||||
|
||||
len = strlen(prefix) + strlen(SHADOW_FILE) + 2;
|
||||
spw_db_file = xmalloc(len);
|
||||
spw_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(spw_db_file, len, "%s/%s", prefix, SHADOW_FILE);
|
||||
spw_setdbname(spw_db_file);
|
||||
|
||||
#ifdef ENABLE_SUBIDS
|
||||
len = strlen(prefix) + strlen("/etc/subuid") + 2;
|
||||
suid_db_file = xmalloc(len);
|
||||
suid_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(suid_db_file, len, "%s/%s", prefix, "/etc/subuid");
|
||||
sub_uid_setdbname(suid_db_file);
|
||||
|
||||
len = strlen(prefix) + strlen("/etc/subgid") + 2;
|
||||
sgid_db_file = xmalloc(len);
|
||||
sgid_db_file = XMALLOCARRAY(len, char);
|
||||
snprintf(sgid_db_file, len, "%s/%s", prefix, "/etc/subgid");
|
||||
sub_gid_setdbname(sgid_db_file);
|
||||
#endif
|
||||
@@ -135,7 +137,7 @@ extern const char* process_prefix_flag (const char* short_opt, int argc, char **
|
||||
setdef_config_file(prefix);
|
||||
#else
|
||||
len = strlen(prefix) + strlen("/etc/login.defs") + 2;
|
||||
def_conf_file = xmalloc(len);
|
||||
def_conf_file = XMALLOCARRAY(len, char);
|
||||
snprintf(def_conf_file, len, "%s/%s", prefix, "/etc/login.defs");
|
||||
setdef_config_file(def_conf_file);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user