From e0f01e5b67dfbe41ff810f466e2173390f8d37a7 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Sat, 19 Jul 2014 13:27:32 -0400 Subject: Be more careful when checking for empty files If we want to avoid reading a totally empty file, it seems better to check after we have opened the file, not before. Signed-off-by: Anthony G. Basile --- src/libudev/util.c | 11 +++++++++++ src/libudev/util.h | 1 + src/udev/udev-rules.c | 18 +++++++++++------- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/libudev/util.c b/src/libudev/util.c index af7477ba6f..116d723a0f 100644 --- a/src/libudev/util.c +++ b/src/libudev/util.c @@ -658,6 +658,17 @@ int null_or_empty_path(const char *fn) { return null_or_empty(&st); } +int null_or_empty_fd(int fd) { + struct stat st; + + assert(fd >= 0); + + if (fstat(fd, &st) < 0) + return -errno; + + return null_or_empty(&st); +} + bool dirent_is_file_with_suffix(const struct dirent *de, const char *suffix) { assert(de); diff --git a/src/libudev/util.h b/src/libudev/util.h index bde7b91e6f..a38e8e61c5 100644 --- a/src/libudev/util.h +++ b/src/libudev/util.h @@ -222,6 +222,7 @@ int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid); bool null_or_empty(struct stat *st) _pure_; int null_or_empty_path(const char *fn); +int null_or_empty_fd(int fd); int execute_command(const char *command, char *const argv[]); diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index 652ac411e1..c47d025846 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -1538,15 +1538,19 @@ static int parse_file(struct udev_rules *rules, const char *filename) int line_nr = 0; unsigned int i; - if (null_or_empty_path(filename)) { - log_debug("skip empty file: %s", filename); - return 0; + f = fopen(filename, "re"); + if (!f) { + if (errno == ENOENT) + return 0; + else + return -errno; } - log_debug("read rules file: %s", filename); - f = fopen(filename, "re"); - if (f == NULL) - return -1; + if (null_or_empty_fd(fileno(f))) { + log_debug("Skipping empty file: %s", filename); + return 0; + } else + log_debug("Reading rules file: %s", filename); first_token = rules->token_cur; filename_off = rules_add_string(rules, filename); -- cgit v1.2.3-54-g00ecf