]> ruderich.org/simon Gitweb - nsscash/nsscash.git/commitdiff
nss: simplify initialization of struct file in map_file()
authorSimon Ruderich <simon@ruderich.org>
Mon, 10 Jun 2019 20:14:50 +0000 (22:14 +0200)
committerSimon Ruderich <simon@ruderich.org>
Mon, 10 Jun 2019 20:14:50 +0000 (22:14 +0200)
It's not necessary to initialize ->fd at the beginning of map_file() as
the call to open(2) will overwrite it anyway.

Also adapt a check for a valid file descriptor. Although POSIX states
that open(2) returns -1 on error, there is no reason to hard-code this
constant. Instead, check for any negative value.

nss/file.c

index 30ea4f9b5f96910f0cd0ba8d353cfa7dbccea076..6da0fd656663a6402db230d0f57a11b4abeb88d8 100644 (file)
 
 bool map_file(const char *path, struct file *f) {
     // Fully initialize the struct for unmap_file() and other users
 
 bool map_file(const char *path, struct file *f) {
     // Fully initialize the struct for unmap_file() and other users
-    f->fd = -1;
-    f->size = 0;
-    f->next_index = 0;
-    f->header = NULL;
+    memset(f, 0, sizeof(*f));
 
     f->fd = open(path, O_RDONLY | O_CLOEXEC);
     if (f->fd < 0) {
 
     f->fd = open(path, O_RDONLY | O_CLOEXEC);
     if (f->fd < 0) {
@@ -81,7 +78,7 @@ void unmap_file(struct file *f) {
         munmap((void *)f->header, f->size);
         f->header = NULL;
     }
         munmap((void *)f->header, f->size);
         f->header = NULL;
     }
-    if (f->fd != -1) {
+    if (f->fd >= 0) {
         close(f->fd);
         f->fd = -1;
     }
         close(f->fd);
         f->fd = -1;
     }