mirror of
https://github.com/dracutdevs/dracut.git
synced 2024-10-27 00:55:34 +03:00
install: add more error handling
also limit local variable scopes and remove bogus checks to negativity of unsigned vars
This commit is contained in:
parent
c23d6ce6ea
commit
e7ba1392e1
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
@ -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++)
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user