From: Felipe Barriga Richards Date: Mon, 22 May 2017 01:14:24 +0000 (-0400) Subject: bugfix: binary_storage: check before writing a key with an empty value. Added test. X-Git-Url: https://git.rrq.au/?a=commitdiff_plain;h=da45e404b42ba943141067d86215890bc7c05775;p=rrq%2Ffuse_xattrs.git bugfix: binary_storage: check before writing a key with an empty value. Added test. --- diff --git a/binary_storage.c b/binary_storage.c index e3d6202..5e87436 100644 --- a/binary_storage.c +++ b/binary_storage.c @@ -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; diff --git a/test/tests.py b/test/tests.py index 38452e5..304676f 100755 --- a/test/tests.py +++ b/test/tests.py @@ -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"