mirror of
https://github.com/systemd/systemd.git
synced 2024-10-31 07:51:21 +03:00
Always check asprintf return code
There is a small number of the places in sources where we don't check asprintf() return code and assume that after error the function returns NULL pointer via the first argument. That's wrong, after error the content of pointer is undefined.
This commit is contained in:
parent
6d314eca15
commit
7de80bfe2e
Notes:
Lennart Poettering
2014-08-19 21:16:45 +02:00
Backport: bugfix
@ -182,7 +182,7 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
|
|||||||
char *printed = NULL;
|
char *printed = NULL;
|
||||||
Unit *u = userdata;
|
Unit *u = userdata;
|
||||||
ExecContext *c;
|
ExecContext *c;
|
||||||
int r;
|
int r = 0;
|
||||||
|
|
||||||
assert(u);
|
assert(u);
|
||||||
|
|
||||||
@ -208,7 +208,7 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
|
|||||||
if (r < 0)
|
if (r < 0)
|
||||||
return -ENODATA;
|
return -ENODATA;
|
||||||
|
|
||||||
asprintf(&printed, UID_FMT, uid);
|
r = asprintf(&printed, UID_FMT, uid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -231,10 +231,10 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
|
|||||||
if (specifier == 'u')
|
if (specifier == 'u')
|
||||||
printed = strdup(username);
|
printed = strdup(username);
|
||||||
else
|
else
|
||||||
asprintf(&printed, UID_FMT, uid);
|
r = asprintf(&printed, UID_FMT, uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!printed)
|
if (r < 0 || !printed)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
*ret = printed;
|
*ret = printed;
|
||||||
|
@ -549,13 +549,18 @@ int main(int argc, char *argv[]) {
|
|||||||
description = NULL;
|
description = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
k = 0;
|
||||||
if (mount_point && description)
|
if (mount_point && description)
|
||||||
asprintf(&name_buffer, "%s (%s) on %s", description, argv[2], mount_point);
|
k = asprintf(&name_buffer, "%s (%s) on %s", description, argv[2], mount_point);
|
||||||
else if (mount_point)
|
else if (mount_point)
|
||||||
asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
|
k = asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
|
||||||
else if (description)
|
else if (description)
|
||||||
asprintf(&name_buffer, "%s (%s)", description, argv[2]);
|
k = asprintf(&name_buffer, "%s (%s)", description, argv[2]);
|
||||||
|
|
||||||
|
if (k < 0) {
|
||||||
|
log_oom();
|
||||||
|
goto finish;
|
||||||
|
}
|
||||||
name = name_buffer ? name_buffer : argv[2];
|
name = name_buffer ? name_buffer : argv[2];
|
||||||
|
|
||||||
k = crypt_init(&cd, argv[3]);
|
k = crypt_init(&cd, argv[3]);
|
||||||
|
@ -591,9 +591,9 @@ int main(int argc, char* argv[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
|
if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) {
|
||||||
asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
|
r = asprintf(&core_owner_uid,
|
||||||
|
"COREDUMP_OWNER_UID=" UID_FMT, owner_uid);
|
||||||
if (core_owner_uid)
|
if (r > 0)
|
||||||
IOVEC_SET_STRING(iovec[j++], core_owner_uid);
|
IOVEC_SET_STRING(iovec[j++], core_owner_uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -746,11 +746,17 @@ static int add_matches(sd_journal *j, char **args) {
|
|||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
t = strappend("_EXE=", path);
|
t = strappend("_EXE=", path);
|
||||||
} else if (S_ISCHR(st.st_mode))
|
} else if (S_ISCHR(st.st_mode)) {
|
||||||
asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev));
|
if (asprintf(&t, "_KERNEL_DEVICE=c%u:%u",
|
||||||
else if (S_ISBLK(st.st_mode))
|
major(st.st_rdev),
|
||||||
asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev));
|
minor(st.st_rdev)) < 0)
|
||||||
else {
|
return -ENOMEM;
|
||||||
|
} else if (S_ISBLK(st.st_mode)) {
|
||||||
|
if (asprintf(&t, "_KERNEL_DEVICE=b%u:%u",
|
||||||
|
major(st.st_rdev),
|
||||||
|
minor(st.st_rdev)) < 0)
|
||||||
|
return -ENOMEM;
|
||||||
|
} else {
|
||||||
log_error("File is neither a device node, nor regular file, nor executable: %s", *i);
|
log_error("File is neither a device node, nor regular file, nor executable: %s", *i);
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
@ -335,11 +335,11 @@ static int start_transient_service(
|
|||||||
_cleanup_free_ char *name = NULL;
|
_cleanup_free_ char *name = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
if (arg_unit)
|
if (arg_unit) {
|
||||||
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
|
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
|
||||||
else
|
if (!name)
|
||||||
asprintf(&name, "run-"PID_FMT".service", getpid());
|
return log_oom();
|
||||||
if (!name)
|
} else if (asprintf(&name, "run-"PID_FMT".service", getpid()) < 0)
|
||||||
return log_oom();
|
return log_oom();
|
||||||
|
|
||||||
r = message_start_transient_unit_new(bus, name, &m);
|
r = message_start_transient_unit_new(bus, name, &m);
|
||||||
@ -471,11 +471,11 @@ static int start_transient_scope(
|
|||||||
|
|
||||||
assert(bus);
|
assert(bus);
|
||||||
|
|
||||||
if (arg_unit)
|
if (arg_unit) {
|
||||||
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
|
name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
|
||||||
else
|
if (!name)
|
||||||
asprintf(&name, "run-"PID_FMT".scope", getpid());
|
return log_oom();
|
||||||
if (!name)
|
} else if (asprintf(&name, "run-"PID_FMT".scope", getpid()) < 0)
|
||||||
return log_oom();
|
return log_oom();
|
||||||
|
|
||||||
r = message_start_transient_unit_new(bus, name, &m);
|
r = message_start_transient_unit_new(bus, name, &m);
|
||||||
|
@ -88,13 +88,16 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
|
|||||||
|
|
||||||
case UNIT_FILE_SYSTEM:
|
case UNIT_FILE_SYSTEM:
|
||||||
|
|
||||||
if (root_dir && runtime)
|
if (root_dir && runtime) {
|
||||||
asprintf(&p, "%s/run/systemd/system", root_dir);
|
if (asprintf(&p, "%s/run/systemd/system", root_dir) < 0)
|
||||||
else if (runtime)
|
return -ENOMEM;
|
||||||
|
} else if (runtime)
|
||||||
p = strdup("/run/systemd/system");
|
p = strdup("/run/systemd/system");
|
||||||
else if (root_dir)
|
else if (root_dir) {
|
||||||
asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
|
if (asprintf(&p, "%s/%s", root_dir,
|
||||||
else
|
SYSTEM_CONFIG_UNIT_PATH) < 0)
|
||||||
|
return -ENOMEM;
|
||||||
|
} else
|
||||||
p = strdup(SYSTEM_CONFIG_UNIT_PATH);
|
p = strdup(SYSTEM_CONFIG_UNIT_PATH);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -4999,11 +4999,10 @@ static int enable_sysv_units(const char *verb, char **args) {
|
|||||||
_cleanup_free_ char *path = NULL;
|
_cleanup_free_ char *path = NULL;
|
||||||
|
|
||||||
if (!isempty(arg_root))
|
if (!isempty(arg_root))
|
||||||
asprintf(&path, "%s/%s/%s", arg_root, *k, name);
|
j = asprintf(&path, "%s/%s/%s", arg_root, *k, name);
|
||||||
else
|
else
|
||||||
asprintf(&path, "%s/%s", *k, name);
|
j = asprintf(&path, "%s/%s", *k, name);
|
||||||
|
if (j < 0) {
|
||||||
if (!path) {
|
|
||||||
r = log_oom();
|
r = log_oom();
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
@ -5017,10 +5016,10 @@ static int enable_sysv_units(const char *verb, char **args) {
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (!isempty(arg_root))
|
if (!isempty(arg_root))
|
||||||
asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
|
j = asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
|
||||||
else
|
else
|
||||||
asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
|
j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
|
||||||
if (!p) {
|
if (j < 0) {
|
||||||
r = log_oom();
|
r = log_oom();
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
@ -102,8 +102,9 @@ static int ask_password_plymouth(
|
|||||||
if (accept_cached) {
|
if (accept_cached) {
|
||||||
packet = strdup("c");
|
packet = strdup("c");
|
||||||
n = 1;
|
n = 1;
|
||||||
} else
|
} else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1),
|
||||||
asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n);
|
message, &n) < 0)
|
||||||
|
packet = NULL;
|
||||||
|
|
||||||
if (!packet) {
|
if (!packet) {
|
||||||
r = -ENOMEM;
|
r = -ENOMEM;
|
||||||
|
Loading…
Reference in New Issue
Block a user