]> git.ipfire.org Git - thirdparty/glibc.git/commit
libio: Fix fmemopen append mode failure (BZ# 20012)
authorAdhemerval Zanella <adhemerval.zanella@linaro.org>
Wed, 27 Apr 2016 14:51:01 +0000 (11:51 -0300)
committerMike Frysinger <vapier@gentoo.org>
Sat, 12 Nov 2016 05:44:35 +0000 (00:44 -0500)
commit52affab7cc2787033713b73bbc2a9c412469012f
tree2ab113be22f0fadfc7abd4e571f57cea18890a1f
parent4a003be3ae533aedb3f5c79424ba046f3bb0af77
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.

This is a backport of b65b205fbcabbb02463e31df17f5cabf7556f892.

[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.

(cherry picked from commit 321e1cef26ccbece949b16622ef74c203bd8ecc6)
libio/fmemopen.c
stdio-common/tst-fmemopen3.c