2 Commits c6a70d3c39 ... c91be88e6d

Author SHA1 Message Date
  Jaidyn Levesque c91be88e6d Fix buffer sizes in xattr_ext.c 1 year ago
  Jaidyn Levesque fbf8ffce43 Simplify pointer->delimited-string-list 1 year ago
2 changed files with 12 additions and 13 deletions
  1. 5 6
      xattr.scm
  2. 7 7
      xattr_ext.c

+ 5 - 6
xattr.scm

@@ -16,7 +16,7 @@
 ;;
 
 (module xattr
-  (get-xattr set-xattr remove-xattr list-xattr split-list)
+  (get-xattr set-xattr remove-xattr list-xattr)
 
 (import (chicken base) (chicken memory) srfi-1 scheme (chicken foreign) srfi-12)
 
@@ -159,13 +159,12 @@
 ;; one-dimensional array with given length and strings separated by a given delimeter.
 ;; This takes that sort of pointer and gives you a nice list of strings.
 (define (pointer->delimited-string-list pointer length delimiter)
-  (let ([is-zero (lambda (num) (eq? num 0))]
-		[map-to-char (lambda (a) (map integer->char a))])
+  (let* ([is-zero (lambda (num) (eq? num 0))]
+		[map-to-char (lambda (a) (map integer->char a))]
+		[byte-list (drop-right (pointer->integers pointer length) 1)])
 	(map list->string
 		 (map map-to-char
-			  (split-list is-zero
-						  (drop-right
-						   (pointer->integers pointer length) 1))))))
+			  (split-list is-zero byte-list)))))
 
 
 ;; Takes a pointer and returns a list of bytes of a given length

+ 7 - 7
xattr_ext.c

@@ -22,15 +22,15 @@
 char*
 get_xattr(const char* path, const char* attr, int* error_code)
 {
-	ssize_t value_length = getxattr(path, attr, NULL, 0);
-	if (value_length == -1) {
+	ssize_t value_size = getxattr(path, attr, NULL, 0);
+	if (value_size == -1) {
 		*error_code = errno;
 		return NULL;
 	}
 
-	char* value = (char*) malloc(value_length);
-	ssize_t new_length = getxattr(path, attr, value, value_length);
-	*error_code = (new_length == -1) ? errno : 0;
+	char* value = (char*) malloc(value_size + 1);
+	ssize_t new_size = getxattr(path, attr, value, value_size + 1);
+	*error_code = (new_size == -1) ? errno : 0;
 
 	return value;
 }
@@ -55,8 +55,8 @@ list_xattr(const char* path, ssize_t* size, int* error_code)
 		return NULL;
 	}
 
-	char* value = (char*) malloc(value_size);
-	*size = llistxattr(path, value, value_size);
+	char* value = (char*) malloc(value_size + 1);
+	*size = llistxattr(path, value, value_size + 1);
 	*error_code = (*size == -1) ? errno : 0;
 	return value;
 }