Merge "init: fix memory leaks"

This commit is contained in:
Treehugger Robot 2016-11-16 03:25:33 +00:00 committed by Gerrit Code Review
commit a73ba879b1
2 changed files with 16 additions and 8 deletions

View file

@ -111,13 +111,18 @@ int add_dev_perms(const char *name, const char *attr,
return -ENOMEM;
node->dp.name = strdup(name);
if (!node->dp.name)
if (!node->dp.name) {
free(node);
return -ENOMEM;
}
if (attr) {
node->dp.attr = strdup(attr);
if (!node->dp.attr)
if (!node->dp.attr) {
free(node->dp.name);
free(node);
return -ENOMEM;
}
}
node->dp.perm = perm;

View file

@ -95,7 +95,6 @@ void set_device_permission(int nargs, char **args)
int prefix = 0;
int wildcard = 0;
char *endptr;
char *tmp = 0;
if (nargs == 0)
return;
@ -129,14 +128,12 @@ void set_device_permission(int nargs, char **args)
perm = strtol(args[1], &endptr, 8);
if (!endptr || *endptr != '\0') {
LOG(ERROR) << "invalid mode '" << args[1] << "'";
free(tmp);
return;
}
struct passwd* pwd = getpwnam(args[2]);
if (!pwd) {
LOG(ERROR) << "invalid uid '" << args[2] << "'";
free(tmp);
return;
}
uid = pwd->pw_uid;
@ -144,11 +141,17 @@ void set_device_permission(int nargs, char **args)
struct group* grp = getgrnam(args[3]);
if (!grp) {
LOG(ERROR) << "invalid gid '" << args[3] << "'";
free(tmp);
return;
}
gid = grp->gr_gid;
add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard);
free(tmp);
if (add_dev_perms(name, attr, perm, uid, gid, prefix, wildcard) != 0) {
PLOG(ERROR) << "add_dev_perms(name=" << name <<
", attr=" << attr <<
", perm=" << std::oct << perm << std::dec <<
", uid=" << uid << ", gid=" << gid <<
", prefix=" << prefix << ", wildcard=" << wildcard <<
")";
return;
}
}