From e7ba1392e180eb6f5e19dfd28a340a98cf09a3cd Mon Sep 17 00:00:00 2001 From: Harald Hoyer Date: Fri, 29 Aug 2014 13:22:11 +0200 Subject: [PATCH] install: add more error handling also limit local variable scopes and remove bogus checks to negativity of unsigned vars --- install/dracut-install.c | 23 ++++++++++++++++------- install/log.c | 7 ++++--- install/macro.h | 2 +- install/strv.c | 10 +++++----- install/strv.h | 2 +- install/util.c | 4 +++- 6 files changed, 30 insertions(+), 18 deletions(-) diff --git a/install/dracut-install.c b/install/dracut-install.c index c1ed4b546..a23dd195c 100644 --- a/install/dracut-install.c +++ b/install/dracut-install.c @@ -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); diff --git a/install/log.c b/install/log.c index 127774f6a..df0e7e2f7 100644 --- a/install/log.c +++ b/install/log.c @@ -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); } diff --git a/install/macro.h b/install/macro.h index ac61b4958..cffaab481 100644 --- a/install/macro.h +++ b/install/macro.h @@ -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); diff --git a/install/strv.c b/install/strv.c index 152b70c17..c83e6b78f 100644 --- a/install/strv.c +++ b/install/strv.c @@ -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++) diff --git a/install/strv.h b/install/strv.h index 193f1dd17..72485c1b3 100644 --- a/install/strv.h +++ b/install/strv.h @@ -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); diff --git a/install/util.c b/install/util.c index c28db764a..2ec95e3b4 100644 --- a/install/util.c +++ b/install/util.c @@ -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); -- 2.47.3