mirror of
https://github.com/systemd/systemd.git
synced 2025-07-14 16:59:12 +03:00
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.
This commit is contained in:
@ -495,11 +495,6 @@ static int netdev_load_one(Manager *manager, const char *filename) {
|
|||||||
assert(manager);
|
assert(manager);
|
||||||
assert(filename);
|
assert(filename);
|
||||||
|
|
||||||
if (null_or_empty_path(filename)) {
|
|
||||||
log_debug("skipping empty file: %s", filename);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
file = fopen(filename, "re");
|
file = fopen(filename, "re");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
if (errno == ENOENT)
|
if (errno == ENOENT)
|
||||||
@ -508,6 +503,11 @@ static int netdev_load_one(Manager *manager, const char *filename) {
|
|||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (null_or_empty_fd(fileno(file))) {
|
||||||
|
log_debug("Skipping empty file: %s", filename);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
netdev = new0(NetDev, 1);
|
netdev = new0(NetDev, 1);
|
||||||
if (!netdev)
|
if (!netdev)
|
||||||
return log_oom();
|
return log_oom();
|
||||||
|
@ -48,8 +48,8 @@ static int network_load_one(Manager *manager, const char *filename) {
|
|||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null_or_empty_path(filename)) {
|
if (null_or_empty_fd(fileno(file))) {
|
||||||
log_debug("skipping empty file: %s", filename);
|
log_debug("Skipping empty file: %s", filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3604,6 +3604,17 @@ int null_or_empty_path(const char *fn) {
|
|||||||
return null_or_empty(&st);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
DIR *xopendirat(int fd, const char *name, int flags) {
|
DIR *xopendirat(int fd, const char *name, int flags) {
|
||||||
int nfd;
|
int nfd;
|
||||||
DIR *d;
|
DIR *d;
|
||||||
|
@ -499,6 +499,7 @@ noreturn void freeze(void);
|
|||||||
|
|
||||||
bool null_or_empty(struct stat *st) _pure_;
|
bool null_or_empty(struct stat *st) _pure_;
|
||||||
int null_or_empty_path(const char *fn);
|
int null_or_empty_path(const char *fn);
|
||||||
|
int null_or_empty_fd(int fd);
|
||||||
|
|
||||||
DIR *xopendirat(int dirfd, const char *name, int flags);
|
DIR *xopendirat(int dirfd, const char *name, int flags);
|
||||||
|
|
||||||
|
@ -153,11 +153,6 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
|
|||||||
assert(ctx);
|
assert(ctx);
|
||||||
assert(filename);
|
assert(filename);
|
||||||
|
|
||||||
if (null_or_empty_path(filename)) {
|
|
||||||
log_debug("skipping empty file: %s", filename);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
file = fopen(filename, "re");
|
file = fopen(filename, "re");
|
||||||
if (!file) {
|
if (!file) {
|
||||||
if (errno == ENOENT)
|
if (errno == ENOENT)
|
||||||
@ -166,6 +161,11 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
|
|||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (null_or_empty_fd(fileno(file))) {
|
||||||
|
log_debug("Skipping empty file: %s", filename);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
link = new0(link_config, 1);
|
link = new0(link_config, 1);
|
||||||
if (!link)
|
if (!link)
|
||||||
return log_oom();
|
return log_oom();
|
||||||
|
@ -1533,15 +1533,19 @@ static int parse_file(struct udev_rules *rules, const char *filename)
|
|||||||
int line_nr = 0;
|
int line_nr = 0;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
if (null_or_empty_path(filename)) {
|
|
||||||
log_debug("skip empty file: %s", filename);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
log_debug("read rules file: %s", filename);
|
|
||||||
|
|
||||||
f = fopen(filename, "re");
|
f = fopen(filename, "re");
|
||||||
if (f == NULL)
|
if (!f) {
|
||||||
return -1;
|
if (errno == ENOENT)
|
||||||
|
return 0;
|
||||||
|
else
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
first_token = rules->token_cur;
|
||||||
filename_off = rules_add_string(rules, filename);
|
filename_off = rules_add_string(rules, filename);
|
||||||
|
Reference in New Issue
Block a user