[libglnx] dirfd: Set initialized flag for iters



commit abd37a4790f86f53bfb442e6d80e1710f50bff92
Author: Colin Walters <walters verbum org>
Date:   Mon Oct 24 16:16:06 2016 -0400

    dirfd: Set initialized flag for iters
    
    And use it when deinitializing, to avoid calling `closedir(NULL)`.
    In practice, this doesn't matter, because `closedir` *does* handle `NULL`
    in glibc.
    
    However, I'm playing with the GCC `-fsanitize=undefined`, and it
    aborts because `closedir` is tagged as requiring a non-`NULL` pointer.

 glnx-dirfd.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
---
diff --git a/glnx-dirfd.c b/glnx-dirfd.c
index 95d7fe1..3a02bb0 100644
--- a/glnx-dirfd.c
+++ b/glnx-dirfd.c
@@ -142,6 +142,7 @@ glnx_dirfd_iterator_init_take_fd (int                dfd,
 
   real_dfd_iter->fd = dfd;
   real_dfd_iter->d = d;
+  real_dfd_iter->initialized = TRUE;
 
   ret = TRUE;
  out:
@@ -169,6 +170,7 @@ glnx_dirfd_iterator_next_dent (GLnxDirFdIterator  *dfd_iter,
   GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
 
   g_return_val_if_fail (out_dent, FALSE);
+  g_return_val_if_fail (dfd_iter->initialized, FALSE);
 
   if (g_cancellable_set_error_if_cancelled (cancellable, error))
     goto out;
@@ -250,6 +252,8 @@ glnx_dirfd_iterator_clear (GLnxDirFdIterator *dfd_iter)
 {
   GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter;
   /* fd is owned by dfd_iter */
+  if (!real_dfd_iter->initialized)
+    return;
   (void) closedir (real_dfd_iter->d);
   real_dfd_iter->initialized = FALSE;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]