add more xattr malloc checks

Sat, 20 Apr 2024 13:01:58 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 20 Apr 2024 13:01:58 +0200
changeset 815
1f40ca07ae1b
parent 814
0ce45bdc48bc
child 816
839fefbdedc7
child 817
22257f6d06a3

add more xattr malloc checks

dav/libxattr.c file | annotate | diff | comparison | revisions
--- a/dav/libxattr.c	Sat Apr 20 12:50:38 2024 +0200
+++ b/dav/libxattr.c	Sat Apr 20 13:01:58 2024 +0200
@@ -73,6 +73,10 @@
     size_t arraylen = LIST_ARRAY_LEN;
     size_t arraypos = 0;
     char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
+    if(!array) {
+        *nelm = -1;
+        return NULL;
+    }
     
     char *begin = buf;
     char *name = NULL;
@@ -82,6 +86,11 @@
         }
         if(buf[i] == '\0') {
             char *attrname = strdup(name);
+            if(!attrname) {
+                strarray_free(array, arraypos);
+                *nelm = -1;
+                return NULL;
+            }
             if(strarray_add(&array, &arraypos, &arraylen, attrname)) {
                 strarray_free(array, arraypos);
                 *nelm = -1;
@@ -146,6 +155,9 @@
     // add the 'user' namespace to the name
     size_t namelen = strlen(name);
     char *attrname = malloc(8 + namelen);
+    if(!attrname) {
+        return NULL;
+    }
     memcpy(attrname, "user.", 5);
     memcpy(attrname+5, name, namelen + 1);
     return attrname;
@@ -153,10 +165,15 @@
 
 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
     char *attrname = name2nsname(attr);
+    if(!attrname) {
+        *len = -1;
+        return NULL;
+    }
     
     char *buf = malloc(ATTR_BUF_LEN);
     if(!buf) {
         *len = -1;
+        free(attrname);
         return NULL;
     }
     ssize_t vlen = getxattr(path, attrname, buf, ATTR_BUF_LEN - 1);
@@ -195,6 +212,9 @@
 
 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
     char *attrname = name2nsname(name);
+    if(!attrname) {
+        return 1;
+    }
     int ret = setxattr(path, attrname, value, len, 0);
     free(attrname);
     return ret;
@@ -202,6 +222,9 @@
 
 int xattr_remove(const char *path, const char *name) {
     char *attrname = name2nsname(name);
+    if(!attrname) {
+        return 1;
+    }
     int ret = removexattr(path, attrname);
     free(attrname);
     return ret;

mercurial