Blame 0055-Always-check-the-return-number-of-asprintf.patch

Harald Hoyer 75f812
From d9eff33ce203a9010067a15ddf1d279132abf437 Mon Sep 17 00:00:00 2001
Harald Hoyer 75f812
From: Kamil Rytarowski <n54@gmx.com>
Harald Hoyer 75f812
Date: Sat, 11 May 2013 14:40:19 +0200
Harald Hoyer 75f812
Subject: [PATCH] Always check the return number of asprintf
Harald Hoyer 75f812
Harald Hoyer 75f812
asprintf prints to an allocated string. When  successful,  the
Harald Hoyer 75f812
functions return the number of bytes printed. If memory allocation
Harald Hoyer 75f812
wasn't possible, or some other error occurs, the function will return
Harald Hoyer 75f812
-1.
Harald Hoyer 75f812
Harald Hoyer 75f812
Don't check strp as a result of asprintf, it's content may be undefined.
Harald Hoyer 75f812
Harald Hoyer 75f812
man 3 asprintf
Harald Hoyer 75f812
---
Harald Hoyer 75f812
 install/dracut-install.c | 59 +++++++++++++++++++++++++++++++++++++++---------
Harald Hoyer 75f812
 1 file changed, 48 insertions(+), 11 deletions(-)
Harald Hoyer 75f812
Harald Hoyer 75f812
diff --git a/install/dracut-install.c b/install/dracut-install.c
Harald Hoyer 75f812
index 0b9502e..c8328b0 100644
Harald Hoyer 75f812
--- a/install/dracut-install.c
Harald Hoyer 75f812
+++ b/install/dracut-install.c
Harald Hoyer 75f812
@@ -83,6 +83,7 @@ static char *convert_abs_rel(const char *from, const char *target)
Harald Hoyer 75f812
         size_t level = 0, fromlevel = 0, targetlevel = 0;
Harald Hoyer 75f812
         int l;
Harald Hoyer 75f812
         size_t i, rl, dirlen;
Harald Hoyer 75f812
+        int ret;
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         target_dir_p = strdup(target);
Harald Hoyer 75f812
         if (!target_dir_p)
Harald Hoyer 75f812
@@ -103,7 +104,11 @@ static char *convert_abs_rel(const char *from, const char *target)
Harald Hoyer 75f812
         for (i = dirlen+1; i < rl; ++i)
Harald Hoyer 75f812
             if (target_dir_p[i] != '/')
Harald Hoyer 75f812
                 break;
Harald Hoyer 75f812
-        asprintf(&realtarget, "%s/%s", realpath_p, &target_dir_p[i]);
Harald Hoyer 75f812
+        ret = asprintf(&realtarget, "%s/%s", realpath_p, &target_dir_p[i]);
Harald Hoyer 75f812
+        if (ret < 0) {
Harald Hoyer 75f812
+                log_error("Out of memory!");
Harald Hoyer 75f812
+                exit(EXIT_FAILURE);
Harald Hoyer 75f812
+        }
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         /* now calculate the relative path from <from> to <target> and
Harald Hoyer 75f812
            store it in <relative_from>
Harald Hoyer 75f812
@@ -282,8 +287,11 @@ static int resolve_deps(const char *src)
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         /* run ldd */
Harald Hoyer 75f812
         ret = asprintf(&cmd, "ldd %s 2>&1", src);
Harald Hoyer 75f812
-        if (ret < 0)
Harald Hoyer 75f812
-                return ret;
Harald Hoyer 75f812
+        if (ret < 0) {
Harald Hoyer 75f812
+                log_error("Out of memory!");
Harald Hoyer 75f812
+                exit(EXIT_FAILURE);
Harald Hoyer 75f812
+        }
Harald Hoyer 75f812
+
Harald Hoyer 75f812
         ret = 0;
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         fptr = popen(cmd, "r");
Harald Hoyer 75f812
@@ -352,6 +360,7 @@ static int hmac_install(const char *src, const char *dst, const char *hmacpath)
Harald Hoyer 75f812
         _cleanup_free_ char *dstpath = strdup(dst);
Harald Hoyer 75f812
         _cleanup_free_ char *srchmacname = NULL;
Harald Hoyer 75f812
         _cleanup_free_ char *dsthmacname = NULL;
Harald Hoyer 75f812
+        int ret;
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         if (!(srcpath && dstpath))
Harald Hoyer 75f812
                 return -ENOMEM;
Harald Hoyer 75f812
@@ -371,11 +380,29 @@ static int hmac_install(const char *src, const char *dst, const char *hmacpath)
Harald Hoyer 75f812
         srcpath[dlen] = '\0';
Harald Hoyer 75f812
         dstpath[dir_len(dst)] = '\0';
Harald Hoyer 75f812
         if (hmacpath) {
Harald Hoyer 75f812
-                asprintf(&srchmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
Harald Hoyer 75f812
-                asprintf(&dsthmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                ret = asprintf(&srchmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                if (ret < 0) {
Harald Hoyer 75f812
+                        log_error("Out of memory!");
Harald Hoyer 75f812
+                        exit(EXIT_FAILURE);
Harald Hoyer 75f812
+                }
Harald Hoyer 75f812
+
Harald Hoyer 75f812
+                ret = asprintf(&dsthmacname, "%s/%s.hmac", hmacpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                if (ret < 0) {
Harald Hoyer 75f812
+                        log_error("Out of memory!");
Harald Hoyer 75f812
+                        exit(EXIT_FAILURE);
Harald Hoyer 75f812
+                }
Harald Hoyer 75f812
         } else {
Harald Hoyer 75f812
-                asprintf(&srchmacname, "%s/.%s.hmac", srcpath, &src[dlen + 1]);
Harald Hoyer 75f812
-                asprintf(&dsthmacname, "%s/.%s.hmac", dstpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                ret = asprintf(&srchmacname, "%s/.%s.hmac", srcpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                if (ret < 0) {
Harald Hoyer 75f812
+                        log_error("Out of memory!");
Harald Hoyer 75f812
+                        exit(EXIT_FAILURE);
Harald Hoyer 75f812
+                }
Harald Hoyer 75f812
+
Harald Hoyer 75f812
+                ret = asprintf(&dsthmacname, "%s/.%s.hmac", dstpath, &src[dlen + 1]);
Harald Hoyer 75f812
+                if (ret < 0) {
Harald Hoyer 75f812
+                        log_error("Out of memory!");
Harald Hoyer 75f812
+                        exit(EXIT_FAILURE);
Harald Hoyer 75f812
+                }
Harald Hoyer 75f812
         }
Harald Hoyer 75f812
         log_debug("hmac cp '%s' '%s')", srchmacname, dsthmacname);
Harald Hoyer 75f812
         dracut_install(srchmacname, dsthmacname, false, false, true);
Harald Hoyer 75f812
@@ -428,7 +455,11 @@ static int dracut_install(const char *src, const char *dst, bool isdir, bool res
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         hashmap_put(items, i, i);
Harald Hoyer 75f812
 
Harald Hoyer 75f812
-        asprintf(&fulldstpath, "%s%s", destrootdir, dst);
Harald Hoyer 75f812
+        ret = asprintf(&fulldstpath, "%s%s", destrootdir, dst);
Harald Hoyer 75f812
+        if (ret < 0) {
Harald Hoyer 75f812
+                log_error("Out of memory!");
Harald Hoyer 75f812
+                exit(EXIT_FAILURE);
Harald Hoyer 75f812
+        }
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         ret = stat(fulldstpath, &sb);
Harald Hoyer 75f812
 
Harald Hoyer 75f812
@@ -511,7 +542,11 @@ static int dracut_install(const char *src, const char *dst, bool isdir, bool res
Harald Hoyer 75f812
                 if (lstat(fulldstpath, &sb) != 0) {
Harald Hoyer 75f812
                         _cleanup_free_ char *absdestpath = NULL;
Harald Hoyer 75f812
 
Harald Hoyer 75f812
-                        asprintf(&absdestpath, "%s%s", destrootdir, abspath);
Harald Hoyer 75f812
+                        ret = asprintf(&absdestpath, "%s%s", destrootdir, abspath);
Harald Hoyer 75f812
+                        if (ret < 0) {
Harald Hoyer 75f812
+                                log_error("Out of memory!");
Harald Hoyer 75f812
+                                exit(EXIT_FAILURE);
Harald Hoyer 75f812
+                        }
Harald Hoyer 75f812
 
Harald Hoyer 75f812
                         ln_r(absdestpath, fulldstpath);
Harald Hoyer 75f812
                 }
Harald Hoyer 75f812
@@ -704,6 +739,8 @@ static char *find_binary(const char *src)
Harald Hoyer 75f812
         char *p, *q;
Harald Hoyer 75f812
         bool end = false;
Harald Hoyer 75f812
         char *newsrc = NULL;
Harald Hoyer 75f812
+        int ret;
Harald Hoyer 75f812
+
Harald Hoyer 75f812
         path = getenv("PATH");
Harald Hoyer 75f812
 
Harald Hoyer 75f812
         if (path == NULL) {
Harald Hoyer 75f812
@@ -730,8 +767,8 @@ static char *find_binary(const char *src)
Harald Hoyer 75f812
                 else
Harald Hoyer 75f812
                         *q = '\0';
Harald Hoyer 75f812
 
Harald Hoyer 75f812
-                asprintf(&newsrc, "%s/%s", p, src);
Harald Hoyer 75f812
-                if (newsrc == NULL) {
Harald Hoyer 75f812
+                ret = asprintf(&newsrc, "%s/%s", p, src);
Harald Hoyer 75f812
+                if (ret < 0) {
Harald Hoyer 75f812
                         log_error("Out of memory!");
Harald Hoyer 75f812
                         exit(EXIT_FAILURE);
Harald Hoyer 75f812
                 }