Hide Forgot
VSX-PCTS has found a SUS-conformance defect in our implementation of the fwrite() interface. According to the SUS: ====================================================================== RETURN VALUE The fwrite() function returns the number of members successfully written, which may be less than nitems if a write error is encountered. If size or nitems is 0, fwrite() returns 0 and the state of the stream remains unchanged. Otherwise, if a write error occurs, the error indicator for the stream is set and errno is set to indicate the error. ====================================================================== Yet when the following test program is run, the combination of: (a) element size of 0 (b) element count of 1 yields fwrite() returning 1 -- SUS says above it should return 0 (when *either* size or nitems is 0). [snip] #include <stdio.h> #include <string.h> #define FILENAME "./testfile" #define STRDATA "This is a test\n" main() { FILE *fp; int val, len = strlen(STRDATA); if ((fp = fopen(FILENAME, "w")) == (FILE *) NULL) { perror(FILENAME); exit(1); } printf("fopen(\"%s\", \"w\") returns successfully\n", FILENAME); if ((val = fwrite(STRDATA, len, 1, fp)) != 1) { perror("fwrite(size != 0, count != 0)"); fprintf(stderr, "fwrite() returned %d, expected 1\n", val); exit(1); } printf("fwrite(): [size != 0, count != 0] returns %d (success)\n", val); if ((val = fwrite(STRDATA, len, 0, fp)) != 0) { perror("fwrite(size != 0, count == 0)"); fprintf(stderr, "fwrite() returned %d, expected 0\n", val); exit(1); } printf("fwrite(): [size != 0, count == 0] returns %d (success)\n", val); if ((val = fwrite(STRDATA, 0, 1, fp)) != 0) { perror("fwrite(size == 0, count != 0)"); fprintf(stderr, "fwrite() returned %d, expected 0\n", val); exit(1); } printf("fwrite(): [size == 0, count != 0] returns %d (success)\n", val); (void) fclose(fp); exit(0); }
This is really a very deliberate thing so that glibc will conform to the ANSI/ISO C9X standards It is an intentional non-compliance
assign to jakub
Actually, this returns 0 in glibc 2.1.9x, probably the contradicting standards were adjusted to match SUS.