fgetxattr no longer errors with ERANGE on undersized buffer; now truncates response

Originator:gautamg
Number:rdar://44533931 Date Originated:09/17/2018
Status: Resolved:
Product:macOS + SDK Product Version:10.13.6
Classification: Reproducible:Always
 
Summary: The man page for `fgetxattr(2)` reports that errno will be set to `ERANGE` when the caller-provided buffer is not large enough to hold the requested xattr; however, on APFS, a truncated result is returned with no indication that it was truncated.

Steps to Reproduce: Test program in C on APFS:
```
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/xattr.h>

int main(int argc, char** argv) {
    if (argc != 2) {
        printf("usage: %s <filename>\n", argv[0]);
        exit(1);
    }
    int fd = open(argv[1], O_RDWR | O_CREAT);
    if (fd < 0) {
        perror("Failed to open file");
        exit(1);
    }
    const char* xattr_name = "com.test.xattr";
    int ret = fsetxattr(fd, xattr_name, "1234567890", 10, 0, 0);
    if (ret < 0) {
        perror("Failed to set xattr");
        exit(1);
    }
    char buf[1];
    ret = fgetxattr(fd, xattr_name, &buf, 1, 0, 0);
    if (ret < 0) {
        perror("Failed to read xattr");
        exit(1);
    } else {
        puts("Unexpectedly read only 1 byte of xattr without an error\n");
    }
}
```

Expected Results:
Exit with "Failed to read xattr" and "Result too large", indicating that the caller must resize the buffer and try again. 

Actual Results:
Exit with "Unexpectedly read only 1 byte of xattr without an error", indicating that the read was truncated.

Version/Build: 10.13.6 (17G65)


Configuration: APFS
This bug does not reproduce on HFS+.

Comments

Closed as duplicate of rdar://39173408

Apple closed this as a duplicate of rdar://39173408 which is not public.


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!