change libxattr xattr_get() to return null-terminated strings

Sun, 25 Jun 2023 11:50:50 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 25 Jun 2023 11:50:50 +0200
changeset 765
305ce525ad4a
parent 764
a41b18c67d95
child 766
c4993f0991e4

change libxattr xattr_get() to return null-terminated strings

dav/libxattr.c file | annotate | diff | comparison | revisions
--- a/dav/libxattr.c	Sun Jun 25 11:25:59 2023 +0200
+++ b/dav/libxattr.c	Sun Jun 25 11:50:50 2023 +0200
@@ -135,7 +135,7 @@
                 ssize_t attrlen = getxattr(path, attrname, NULL, 0);
                 if(attrlen > 0) {
                     free(buf);
-                    buf = malloc(attrlen);
+                    buf = malloc(attrlen + 1);
                     vlen = getxattr(path, attrname, buf, attrlen);
                     if(vlen > 0) {
                         break;
@@ -152,6 +152,7 @@
     }
     
     free(attrname);
+    buf[vlen] = 0;
     *len = vlen;
     return buf;
 }
@@ -238,7 +239,7 @@
         return NULL;
     }
     
-    char *buf = malloc(attrlen);
+    char *buf = malloc(attrlen + 1);
     ssize_t vlen = getxattr(path, attr, buf, attrlen, 0, 0);
     if(vlen < 0) {
         *len = -1;
@@ -246,6 +247,7 @@
         return NULL;
     }
     
+    buf[attrlen] = 0;
     *len = vlen;
     return buf;
 }
@@ -327,7 +329,8 @@
     }
     
     size_t bufsize = (size_t)s.st_size;
-    char *buf = malloc(bufsize);
+    char *buf = malloc(bufsize + 1);
+    buf[bufsize] = 0;
     
     char *b = buf;
     size_t cur = 0;
@@ -396,7 +399,6 @@
     size_t arraypos = 0;
     char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
     
-    char *name = buf;
     for(int i=0;i<length;i++) {
         char namelen = buf[i];
         char *name = buf + i + 1;
@@ -446,14 +448,14 @@
         return NULL;
     }
     
-    char *buf = malloc(attrlen);
+    char *buf = malloc(attrlen + 1);
     ssize_t vlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, buf, attrlen);
     if(vlen < 0) {
         *len = -1;
         free(buf);
         return NULL;
     }
-    
+    buf[attrlen] = 0;
     *len = vlen;
     return buf;
 }

mercurial