From 7de80bfe2e61d5818601ccfddbadad3b7703ed70 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 25 Jul 2014 15:38:31 +0200 Subject: 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. --- src/journal/coredump.c | 6 +++--- src/journal/journalctl.c | 16 +++++++++++----- 2 files changed, 14 insertions(+), 8 deletions(-) (limited to 'src/journal') diff --git a/src/journal/coredump.c b/src/journal/coredump.c index 182c2b1ba..fee0a909d 100644 --- a/src/journal/coredump.c +++ b/src/journal/coredump.c @@ -591,9 +591,9 @@ int main(int argc, char* argv[]) { } if (sd_pid_get_owner_uid(pid, &owner_uid) >= 0) { - asprintf(&core_owner_uid, "COREDUMP_OWNER_UID=" UID_FMT, owner_uid); - - if (core_owner_uid) + r = asprintf(&core_owner_uid, + "COREDUMP_OWNER_UID=" UID_FMT, owner_uid); + if (r > 0) IOVEC_SET_STRING(iovec[j++], core_owner_uid); } diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 7aedbf0c5..5a59a3ac8 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -746,11 +746,17 @@ static int add_matches(sd_journal *j, char **args) { } } else t = strappend("_EXE=", path); - } else if (S_ISCHR(st.st_mode)) - asprintf(&t, "_KERNEL_DEVICE=c%u:%u", major(st.st_rdev), minor(st.st_rdev)); - else if (S_ISBLK(st.st_mode)) - asprintf(&t, "_KERNEL_DEVICE=b%u:%u", major(st.st_rdev), minor(st.st_rdev)); - else { + } else if (S_ISCHR(st.st_mode)) { + if (asprintf(&t, "_KERNEL_DEVICE=c%u:%u", + major(st.st_rdev), + minor(st.st_rdev)) < 0) + 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); return -EINVAL; } -- cgit v1.2.3