From b65b205fbcabbb02463e31df17f5cabf7556f892 Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Wed, 27 Apr 2016 11:51:01 -0300 Subject: libio: Fix fmemopen append mode failure (BZ# 20012) The fmemopen implementation does not account the file position correctly in append mode. The following example shows the failure: === int main () { char buf[10] = "test"; FILE *fp = fmemopen (buf, 10, "a+"); fseek (fp, 0, SEEK_SET); int gr; if ((gr = getc (fp)) != 't' || (gr = getc (fp)) != 'e' || (gr = getc (fp)) != 's' || (gr = getc (fp)) != 't' || (gr = getc (fp)) != EOF) { printf ("%s: getc failed returned %i\n", __FUNCTION__, gr); return 1; } return 0; } === This is due both how read and write operation update the buffer position, taking in consideration buffer lenght instead of maximum position defined by the open mode. This patch fixes it and also fixes fseek not returning EINVAL for invalid whence modes. Tested on x86_64 and i686. [BZ #20012] * libio/fmemopen.c (fmemopen_read): Use buffer maximum position, not length to calculate the buffer to read. (fmemopen_write): Set the buffer position based on bytes written. (fmemopen_seek): Return EINVAL for invalid whence modes. --- stdio-common/tst-fmemopen3.c | 118 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 1 deletion(-) (limited to 'stdio-common') diff --git a/stdio-common/tst-fmemopen3.c b/stdio-common/tst-fmemopen3.c index 250f9ecc75..054b0697b3 100644 --- a/stdio-common/tst-fmemopen3.c +++ b/stdio-common/tst-fmemopen3.c @@ -25,8 +25,13 @@ static void print_buffer (const char *s, size_t n) { size_t i; + printf ("{"); for (i=0; i