Bug 18659 - libio: large read after a write fails to flush buffer
Summary: libio: large read after a write fails to flush buffer
Status: RESOLVED DUPLICATE of bug 17063
Alias: None
Product: glibc
Classification: Unclassified
Component: stdio (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Siddhesh Poyarekar
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-11 07:51 UTC by Siddhesh Poyarekar
Modified: 2015-07-24 12:37 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Siddhesh Poyarekar 2015-07-11 07:51:53 UTC
When a program calls fread on a file immediately after writing a small amount of data, it may fail to flush the written data if the read size is greater than or equal to the FILE buffer size.  The following test demonstrates this:

#include <stdio.h>
#include <assert.h>

void main()
{
    FILE *f;
    char foo[4096];
    int n;

    f = fopen("/tmp/test", "a+"); /* qemu does fdopen */
    assert(f);
    fwrite("Hello World!\n", 1, 13, f);

    n = fread(foo, 1, READ_SIZE, f);
    fprintf(stderr, "eof?%d %d", feof(f), n);

    fseek(f, 0, SEEK_SET);
    n = fread(foo, 1, sizeof(foo), f);
    fprintf(stderr, "eof?%d %d", feof(f), n);
}

1) gcc test.c -DREAD_SIZE=4096 -o test (qemu-ga tries to read QGA_READ_COUNT_DEFAULT=4096 by default)
./test
first read: 0
read after seek 0: 0

2) gcc test.c -DREAD_SIZE=13 -o test
./test
first read: 0
read after seek 0: 13

Patch coming up.
Comment 1 Andreas Schwab 2015-07-11 08:10:55 UTC
However, the application shall ensure that output is not directly followed by input without an intervening call to fflush() or to a file positioning function (fseek(), fsetpos(), or rewind()),
Comment 2 Andreas Schwab 2015-07-11 08:25:13 UTC
.

*** This bug has been marked as a duplicate of bug 17063 ***