bugfix: binary_storage: check before writing a key with an empty value. Added test.
authorFelipe Barriga Richards <spam@felipebarriga.cl>
Mon, 22 May 2017 01:14:24 +0000 (21:14 -0400)
committerFelipe Barriga Richards <spam@felipebarriga.cl>
Mon, 22 May 2017 01:23:03 +0000 (21:23 -0400)
binary_storage.c
test/tests.py

index e3d620218cfb3927c8267cf98f2c3246d86497d5..5e8743619744b6cfbf4c787cef51f545241f9509 100644 (file)
@@ -197,7 +197,7 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_
 
 #ifdef DEBUG
     char *sanitized_value = sanitize_value(value, value_size);
-    debug_print("name=%s sanitized_value=%s value_size=%zu\n", name, sanitized_value, value_size);
+    debug_print("name='%s' name_size=%zu sanitized_value='%s' value_size=%zu\n", name, name_size, sanitized_value, value_size);
     free(sanitized_value);
 #endif
 
@@ -213,8 +213,11 @@ int __write_to_file(FILE *file, const char *name, const char *value, const size_
     if (fwrite(&value_size, sizeof(size_t), 1, file) != 1) {
         return -1;
     }
-    if (fwrite(value, value_size, 1, file) != 1) {
-        return -1;
+    // write value content only if we have something to write.
+    if (value_size > 0) {
+        if (fwrite(value, value_size, 1, file) != 1) {
+            return -1;
+        }
     }
 
     return 0;
index 38452e5c5a65d947d95e0445d3e1caeab0635aec..304676f2f17c0484a5bd630b4564e8303baba33a 100755 (executable)
@@ -120,6 +120,14 @@ class TestXAttrs(unittest.TestCase):
         read_value = xattr.getxattr(self.randomFile, key)
         self.assertEqual(value, read_value.decode(enc))
 
+    def test_xattr_set_empty(self):
+        enc = "utf-8"
+        key = "user.foo"
+        value = ""
+        xattr.setxattr(self.randomFile, key, bytes(value, enc))
+        read_value = xattr.getxattr(self.randomFile, key)
+        self.assertEqual(value, read_value.decode(enc))
+
     def test_xattr_set_override(self):
         enc = "utf-8"
         key = "user.foo"