From b82f5cfeb2a88bdfcb8d2b27c8183c163e9d3bd8 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 8 Mar 2021 14:09:43 -0800 Subject: [PATCH] Improve coverage. Also fix a comment copy & paste mistake and some formatting. Test: treehugger Change-Id: I0af3ab2eb4f180f86b0ab7d2af260f0f30692fdd --- libc/bionic/fgetxattr.cpp | 2 +- libc/bionic/flistxattr.cpp | 4 ++-- tests/sys_xattr_test.cpp | 13 ++++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/libc/bionic/fgetxattr.cpp b/libc/bionic/fgetxattr.cpp index c7532355b..136d41f0a 100644 --- a/libc/bionic/fgetxattr.cpp +++ b/libc/bionic/fgetxattr.cpp @@ -37,7 +37,7 @@ extern "C" ssize_t __fgetxattr(int, const char*, void*, size_t); -ssize_t fgetxattr(int fd, const char *name, void *value, size_t size) { +ssize_t fgetxattr(int fd, const char* name, void* value, size_t size) { int saved_errno = errno; ssize_t result = __fgetxattr(fd, name, value, size); diff --git a/libc/bionic/flistxattr.cpp b/libc/bionic/flistxattr.cpp index 3bad3835d..e42419f13 100644 --- a/libc/bionic/flistxattr.cpp +++ b/libc/bionic/flistxattr.cpp @@ -37,7 +37,7 @@ extern "C" ssize_t __flistxattr(int, char*, size_t); -ssize_t flistxattr(int fd, char *list, size_t size) { +ssize_t flistxattr(int fd, char* list, size_t size) { int saved_errno = errno; ssize_t result = __flistxattr(fd, list, size); if (result != -1 || errno != EBADF) { @@ -45,7 +45,7 @@ ssize_t flistxattr(int fd, char *list, size_t size) { } // fd could be an O_PATH file descriptor, and the kernel - // may not directly support fgetxattr() on such a file descriptor. + // may not directly support flistxattr() on such a file descriptor. // Use /proc/self/fd instead to emulate this support. int fd_flag = fcntl(fd, F_GETFL); if (fd_flag == -1 || (fd_flag & O_PATH) == 0) { diff --git a/tests/sys_xattr_test.cpp b/tests/sys_xattr_test.cpp index 8f4a33684..45cf3799a 100644 --- a/tests/sys_xattr_test.cpp +++ b/tests/sys_xattr_test.cpp @@ -55,13 +55,13 @@ TEST(sys_xattr, fsetxattr_toosmallbuf) { ASSERT_EQ(ERANGE, errno); } -TEST(sys_xattr, fsetxattr_invalidfd) { +TEST(sys_xattr, fsetxattr_invalid_fd) { char buf[10]; errno = 0; - ASSERT_EQ(-1, fsetxattr(65535, "user.foo", "0123", 5, 0)); + ASSERT_EQ(-1, fsetxattr(-1, "user.foo", "0123", 5, 0)); ASSERT_EQ(EBADF, errno); errno = 0; - ASSERT_EQ(-1, fgetxattr(65535, "user.foo", buf, sizeof(buf))); + ASSERT_EQ(-1, fgetxattr(-1, "user.foo", buf, sizeof(buf))); ASSERT_EQ(EBADF, errno); } @@ -127,3 +127,10 @@ TEST(sys_xattr, flistattr_opath) { #endif close(fd); } + +TEST(sys_xattr, flistattr_invalid_fd) { + char buf[65536]; // 64kB is max possible xattr list size. See "man 7 xattr". + errno = 0; + ASSERT_EQ(-1, flistxattr(-1, buf, sizeof(buf))); + ASSERT_EQ(EBADF, errno); +}