Improve <sys/xattr.h> coverage.

Also fix a comment copy & paste mistake and some formatting.

Test: treehugger
Change-Id: I0af3ab2eb4f180f86b0ab7d2af260f0f30692fdd
This commit is contained in:
Elliott Hughes 2021-03-08 14:09:43 -08:00
parent 1ef8d418be
commit b82f5cfeb2
3 changed files with 13 additions and 6 deletions

View file

@ -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);

View file

@ -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) {

View file

@ -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);
}