[libglnx] listxattr: Don't assume that first call to listxattr gives correct size



commit 6bf55255e8a688997973d9d40755fa999eda3791
Author: William Manley <will williammanley net>
Date:   Tue Jan 10 15:13:56 2017 +0000

    listxattr: Don't assume that first call to listxattr gives correct size
    
    To get the right sized buffer to pass to `flistattr` and `llistattr` we
    first call them with a zero byte buffer.  They then return the number of
    bytes they'll actually need to operate.  We would `malloc` and then call
    again assuming that the size we got originally was correct.
    
    On my computer at least this isn't always the case.  I've seen instances
    where the first call returns 23B, but then on the second one returns no
    data at all.  Getting these non-existant xattrs would then cause ostree
    to fail.
    
    I'm not sure why it's behaving this way on my machine.  I suspect its some
    interaction with overlayfs but I haven't proven this.

 glnx-xattrs.c |   32 ++++++++++++++++++++------------
 1 files changed, 20 insertions(+), 12 deletions(-)
---
diff --git a/glnx-xattrs.c b/glnx-xattrs.c
index b5a5922..d50b3c2 100644
--- a/glnx-xattrs.c
+++ b/glnx-xattrs.c
@@ -135,7 +135,7 @@ get_xattrs_impl (const char      *path,
                  GError        **error)
 {
   gboolean ret = FALSE;
-  ssize_t bytes_read;
+  ssize_t bytes_read, real_size;
   glnx_free char *xattr_names = NULL;
   glnx_free char *xattr_names_canonical = NULL;
   GVariantBuilder builder;
@@ -158,15 +158,19 @@ get_xattrs_impl (const char      *path,
   else if (bytes_read > 0)
     {
       xattr_names = g_malloc (bytes_read);
-      if (llistxattr (path, xattr_names, bytes_read) < 0)
+      real_size = llistxattr (path, xattr_names, bytes_read);
+      if (real_size < 0)
         {
           glnx_set_prefix_error_from_errno (error, "%s", "llistxattr");
           goto out;
         }
-      xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read);
-      
-      if (!read_xattr_name_array (path, -1, xattr_names_canonical, bytes_read, &builder, error))
-        goto out;
+      else if (real_size > 0)
+        {
+          xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
+
+          if (!read_xattr_name_array (path, -1, xattr_names_canonical, real_size, &builder, error))
+            goto out;
+        }
     }
 
   ret_xattrs = g_variant_builder_end (&builder);
@@ -202,7 +206,7 @@ glnx_fd_get_all_xattrs (int            fd,
                         GError       **error)
 {
   gboolean ret = FALSE;
-  ssize_t bytes_read;
+  ssize_t bytes_read, real_size;
   glnx_free char *xattr_names = NULL;
   glnx_free char *xattr_names_canonical = NULL;
   GVariantBuilder builder;
@@ -225,15 +229,19 @@ glnx_fd_get_all_xattrs (int            fd,
   else if (bytes_read > 0)
     {
       xattr_names = g_malloc (bytes_read);
-      if (flistxattr (fd, xattr_names, bytes_read) < 0)
+      real_size = flistxattr (fd, xattr_names, bytes_read);
+      if (real_size < 0)
         {
           glnx_set_prefix_error_from_errno (error, "%s", "flistxattr");
           goto out;
         }
-      xattr_names_canonical = canonicalize_xattrs (xattr_names, bytes_read);
-      
-      if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, bytes_read, &builder, error))
-        goto out;
+      else if (real_size > 0)
+        {
+          xattr_names_canonical = canonicalize_xattrs (xattr_names, real_size);
+
+          if (!read_xattr_name_array (NULL, fd, xattr_names_canonical, real_size, &builder, error))
+            goto out;
+        }
     }
 
   ret_xattrs = g_variant_builder_end (&builder);


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