]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
install: add more error handling
authorHarald Hoyer <harald@redhat.com>
Fri, 29 Aug 2014 11:22:11 +0000 (13:22 +0200)
committerHarald Hoyer <harald@redhat.com>
Fri, 29 Aug 2014 11:37:40 +0000 (13:37 +0200)
also limit local variable scopes
and remove bogus checks to negativity of unsigned vars

install/dracut-install.c
install/log.c
install/macro.h
install/strv.c
install/strv.h
install/util.c

index c1ed4b546903ddc76052793e76d251aa440ea78e..a23dd195c6605cf44cd95cec21edf213ebf06611 100644 (file)
@@ -222,7 +222,8 @@ static int cp(const char *src, const char *dst)
                 if (ret == 0) {
                         struct timeval tv[2];
                         if (fchown(dest_desc, sb.st_uid, sb.st_gid) != 0)
-                                fchown(dest_desc, (__uid_t) - 1, sb.st_gid);
+                                if(fchown(dest_desc, (__uid_t) - 1, sb.st_gid) != 0)
+                                    log_error("Failed to chown %s: %m", dst);
                         tv[0].tv_sec = sb.st_atime;
                         tv[0].tv_usec = 0;
                         tv[1].tv_sec = sb.st_mtime;
@@ -328,18 +329,25 @@ static int resolve_deps(const char *src)
 {
         int ret = 0;
 
-        _cleanup_free_ char *buf = malloc(LINE_MAX);
+        _cleanup_free_ char *buf = NULL;
         size_t linesize = LINE_MAX;
         _cleanup_pclose_ FILE *fptr = NULL;
         _cleanup_free_ char *cmd = NULL;
 
+       buf = malloc(LINE_MAX);
+       if (buf == NULL)
+               return -errno;
+
         if (strstr(src, ".so") == 0) {
                 _cleanup_close_ int fd = -1;
                 fd = open(src, O_RDONLY | O_CLOEXEC);
                 if (fd < 0)
                         return -errno;
 
-                read(fd, buf, LINE_MAX);
+                ret = read(fd, buf, LINE_MAX);
+                if (ret == -1)
+                        return -errno;
+
                 buf[LINE_MAX - 1] = '\0';
                 if (buf[0] == '#' && buf[1] == '!') {
                         /* we have a shebang */
@@ -367,7 +375,7 @@ static int resolve_deps(const char *src)
         fptr = popen(cmd, "r");
 
         while (!feof(fptr)) {
-                char *p, *q;
+                char *p;
 
                 if (getline(&buf, &linesize, fptr) <= 0)
                         continue;
@@ -401,6 +409,8 @@ static int resolve_deps(const char *src)
 
                 p = strchr(p, '/');
                 if (p) {
+                        char *q;
+
                         for (q = p; *q && *q != ' ' && *q != '\n'; q++) ;
                         *q = '\0';
 
@@ -488,7 +498,6 @@ void mark_hostonly(const char *path)
         }
 
         fprintf(f, "%s\n", path);
-
 }
 
 static int dracut_install(const char *src, const char *dst, bool isdir, bool resolvedeps, bool hashdst)
@@ -878,9 +887,9 @@ static int install_one(const char *src, const char *dst)
         int ret = 0;
 
         if (strchr(src, '/') == NULL) {
-                char **q = NULL;
                 char **p = find_binary(src);
                 if (p) {
+                       char **q = NULL;
                         STRV_FOREACH(q, p) {
                                 char *newsrc = *q;
                                 log_debug("dracut_install '%s' '%s'", newsrc, dst);
@@ -914,9 +923,9 @@ static int install_all(int argc, char **argv)
                 log_debug("Handle '%s'", argv[i]);
 
                 if (strchr(argv[i], '/') == NULL) {
-                        char **q = NULL;
                         char **p = find_binary(argv[i]);
                         if (p) {
+                               char **q = NULL;
                                 STRV_FOREACH(q, p) {
                                         char *newsrc = *q;
                                         log_debug("dracut_install '%s'", newsrc);
index 127774f6ae692d569062b9c0000be1b7e6d8ebeb..df0e7e2f7471051327a0097cc3529ba136dab930 100644 (file)
@@ -103,11 +103,10 @@ void log_set_facility(int facility) {
 static int write_to_console(
                 int level,
                 const char*file,
-                int line,
+                unsigned int line,
                 const char *func,
                 const char *buffer) {
 
-        char location[64];
         struct iovec iovec[5];
         unsigned n = 0;
 
@@ -119,7 +118,9 @@ static int write_to_console(
         IOVEC_SET_STRING(iovec[n++], "dracut-install: ");
 
         if (show_location) {
-                snprintf(location, sizeof(location), "(%s:%u) ", file, line);
+                char location[64];
+                if (snprintf(location, sizeof(location), "(%s:%u) ", file, line) <= 0)
+                        return -errno;
                 IOVEC_SET_STRING(iovec[n++], location);
         }
 
index ac61b495883dc754f124010428e2b5e36302db78..cffaab4811be33c155b1f8cbce2e1fcf1aeea60e 100644 (file)
@@ -207,7 +207,7 @@ static inline size_t IOVEC_INCREMENT(struct iovec *i, unsigned n, size_t k) {
         for (j = 0; j < n; j++) {
                 size_t sub;
 
-                if (_unlikely_(k <= 0))
+                if (_unlikely_(k == 0))
                         break;
 
                 sub = MIN(i[j].iov_len, k);
index 152b70c17fc8bfef792c394543339850a9fdefe4..c83e6b78f91cebe9892ae0b3d4103926dba91a00 100644 (file)
@@ -84,7 +84,7 @@ char **strv_copy(char * const *l) {
         return r;
 }
 
-unsigned strv_length(char * const *l) {
+unsigned int strv_length(char * const *l) {
         unsigned n = 0;
 
         if (!l)
@@ -299,7 +299,7 @@ char **strv_split_quoted(const char *s) {
 
 char **strv_split_newlines(const char *s) {
         char **l;
-        unsigned n;
+        unsigned int n;
 
         assert(s);
 
@@ -311,7 +311,7 @@ char **strv_split_newlines(const char *s) {
                 return NULL;
 
         n = strv_length(l);
-        if (n <= 0)
+        if (n == 0)
                 return l;
 
         if (isempty(l[n-1])) {
@@ -491,9 +491,9 @@ char **strv_parse_nulstr(const char *s, size_t l) {
         unsigned c = 0, i = 0;
         char **v;
 
-        assert(s || l <= 0);
+        assert(s || l == 0);
 
-        if (l <= 0)
+        if (l == 0)
                 return new0(char*, 1);
 
         for (p = s; p < s + l; p++)
index 193f1dd1789c67b646112b23cf0166978ee1d91f..72485c1b3ce6fe54476c5b0cb51dc02c238c3501 100644 (file)
@@ -34,7 +34,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
 #define _cleanup_strv_free_ _cleanup_(strv_freep)
 
 char **strv_copy(char * const *l);
-unsigned strv_length(char * const *l) _pure_;
+unsigned int strv_length(char * const *l) _pure_;
 
 char **strv_merge(char **a, char **b);
 char **strv_merge_concat(char **a, char **b, const char *suffix);
index c28db764a97337106ebf4e6d6a0c4b4f8f66ec85..2ec95e3b4efebcf1430cff1072886ae537f0d701 100644 (file)
@@ -224,7 +224,7 @@ char *strappend(const char *s, const char *suffix) {
 char *strjoin(const char *x, ...) {
         va_list ap;
         size_t l;
-        char *r, *p;
+        char *r;
 
         va_start(ap, x);
 
@@ -257,6 +257,8 @@ char *strjoin(const char *x, ...) {
                 return NULL;
 
         if (x) {
+                char *p;
+
                 p = stpcpy(r, x);
 
                 va_start(ap, x);