[PATCH v2] libio: Fix fmemopen_write on appending condition

Rocket Ma marocketbd@gmail.com
Wed Mar 25 05:38:07 GMT 2026


I also tried to write bytes in two batches: first write 0x800, then
0x1800. Source code
is attached below. I forgot to allocate a buffer in previous mail as
if setvbuf recv a
NULL buffer, it will allocates a fixed size buffer, not respecting the
size we set.

The result is:

fwrite 0x800 = 0x800 Success
fflush = 0 Success
fwrite 0x1800 = 0x1800 Success
fclose = 0xffffffffffffffff No space left on device

$ wc -c tmpfs/1
4096 tmpfs/1

So we indeed has no way to probe how many bytes glibc failed to
write into the underlying fd if the FILE is buffered.

---

int main(void) {
    FILE *fp = fopen("tmpfs/1", "wb");
    char *buffer = malloc(0x2200);
    setvbuf(fp, buffer, _IOFBF, 0x2200);
    char *spc = malloc(0x2000);
    memset(spc, 'A', 0x2000);
    long rc = fwrite(spc, 1, 0x800, fp);
    printf("fwrite 0x800 = %#lx %m\n", rc);
    rc = fflush(fp);

    printf("fflush = %#lx %m\n", rc);
    rc = fwrite(spc, 1, 0x1800, fp);
    printf("fwrite 0x1800 = %#lx %m\n", rc);
    rc = fclose(fp);
    printf("fclose = %#lx %m\n", rc);

    return 0;
}


More information about the Libc-alpha mailing list