chiark / gitweb /
Always check asprintf return code
authorKarel Zak <kzak@redhat.com>
Fri, 25 Jul 2014 13:38:31 +0000 (15:38 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 26 Jul 2014 19:08:41 +0000 (15:08 -0400)
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/core/unit-printf.c
src/cryptsetup/cryptsetup.c
src/journal/coredump.c
src/journal/journalctl.c
src/run/run.c
src/shared/install.c
src/systemctl/systemctl.c
src/tty-ask-password-agent/tty-ask-password-agent.c

index 5bd30f0bf74a7a4e37b07a0797d1d0514f40ac0e..62599d0813bd2478ace3c0a95f51cbff374f9a7d 100644 (file)
@@ -182,7 +182,7 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
         char *printed = NULL;
         Unit *u = userdata;
         ExecContext *c;
-        int r;
+        int r = 0;
 
         assert(u);
 
@@ -208,7 +208,7 @@ static int specifier_user_name(char specifier, void *data, void *userdata, char
                                 if (r < 0)
                                         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')
                         printed = strdup(username);
                 else
-                        asprintf(&printed, UID_FMT, uid);
+                        r = asprintf(&printed, UID_FMT, uid);
         }
 
-        if (!printed)
+        if (r < 0 || !printed)
                 return -ENOMEM;
 
         *ret = printed;
index a67d85e623c685a0ac96e7d0df3d7c8b39b2713e..67dc88fa515c37a02eef4d366badac4399a35203 100644 (file)
@@ -549,13 +549,18 @@ int main(int argc, char *argv[]) {
                         description = NULL;
                 }
 
+                k = 0;
                 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)
-                        asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
+                        k = asprintf(&name_buffer, "%s on %s", argv[2], mount_point);
                 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];
 
                 k = crypt_init(&cd, argv[3]);
index 182c2b1bad1e0f31d211fa99160067c00b23bc41..fee0a909dc5f804394aace750a66c16d991e26d8 100644 (file)
@@ -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);
         }
 
index 7aedbf0c5793c10fad51af44f0d0912e8dbcdcc3..5a59a3ac83f41ee71c8d1cb84c60d664ec3fe896 100644 (file)
@@ -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;
                         }
index 9d5527b29c6cf209b6e12ac7a0b4dc66d16a4327..b9be1455c42dcc8c8cd38cbcdd1c0581a05fe1a3 100644 (file)
@@ -335,11 +335,11 @@ static int start_transient_service(
         _cleanup_free_ char *name = NULL;
         int r;
 
-        if (arg_unit)
+        if (arg_unit) {
                 name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".service");
-        else
-                asprintf(&name, "run-"PID_FMT".service", getpid());
-        if (!name)
+                if (!name)
+                        return log_oom();
+        } else if (asprintf(&name, "run-"PID_FMT".service", getpid()) < 0)
                 return log_oom();
 
         r = message_start_transient_unit_new(bus, name, &m);
@@ -471,11 +471,11 @@ static int start_transient_scope(
 
         assert(bus);
 
-        if (arg_unit)
+        if (arg_unit) {
                 name = unit_name_mangle_with_suffix(arg_unit, MANGLE_NOGLOB, ".scope");
-        else
-                asprintf(&name, "run-"PID_FMT".scope", getpid());
-        if (!name)
+                if (!name)
+                        return log_oom();
+        } else if (asprintf(&name, "run-"PID_FMT".scope", getpid()) < 0)
                 return log_oom();
 
         r = message_start_transient_unit_new(bus, name, &m);
index a2f84f893e43255f7ce674d2f6e09fc1a193eb9a..e957c333442b1828274959ad1eac5f6c8516a325 100644 (file)
@@ -88,13 +88,16 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
 
         case UNIT_FILE_SYSTEM:
 
-                if (root_dir && runtime)
-                        asprintf(&p, "%s/run/systemd/system", root_dir);
-                else if (runtime)
+                if (root_dir && runtime) {
+                        if (asprintf(&p, "%s/run/systemd/system", root_dir) < 0)
+                                return -ENOMEM;
+                } else if (runtime)
                         p = strdup("/run/systemd/system");
-                else if (root_dir)
-                        asprintf(&p, "%s/%s", root_dir, SYSTEM_CONFIG_UNIT_PATH);
-                else
+                else if (root_dir) {
+                        if (asprintf(&p, "%s/%s", root_dir,
+                                     SYSTEM_CONFIG_UNIT_PATH) < 0)
+                                return -ENOMEM;
+                } else
                         p = strdup(SYSTEM_CONFIG_UNIT_PATH);
 
                 break;
index fc325095ac833f16a89e95e33faa903f1eb72451..f3d7fc8d6e5c0573a5e7442180e1b41ce7c5c0ab 100644 (file)
@@ -4999,11 +4999,10 @@ static int enable_sysv_units(const char *verb, char **args) {
                         _cleanup_free_ char *path = NULL;
 
                         if (!isempty(arg_root))
-                                asprintf(&path, "%s/%s/%s", arg_root, *k, name);
+                                j = asprintf(&path, "%s/%s/%s", arg_root, *k, name);
                         else
-                                asprintf(&path, "%s/%s", *k, name);
-
-                        if (!path) {
+                                j = asprintf(&path, "%s/%s", *k, name);
+                        if (j < 0) {
                                 r = log_oom();
                                 goto finish;
                         }
@@ -5017,10 +5016,10 @@ static int enable_sysv_units(const char *verb, char **args) {
                         continue;
 
                 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
-                        asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
-                if (!p) {
+                        j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
+                if (j < 0) {
                         r = log_oom();
                         goto finish;
                 }
index a7fce518a52a77fd71c457de97c2bb1a64d82710..2c540ba17003ebd2f5176c7ae913803348993084 100644 (file)
@@ -102,8 +102,9 @@ static int ask_password_plymouth(
         if (accept_cached) {
                 packet = strdup("c");
                 n = 1;
-        } else
-                asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n);
+        } else if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1),
+                            message, &n) < 0)
+                packet = NULL;
 
         if (!packet) {
                 r = -ENOMEM;