aboutsummaryrefslogtreecommitdiff
path: root/libio
diff options
context:
space:
mode:
Diffstat (limited to 'libio')
-rw-r--r--libio/Makefile2
-rw-r--r--libio/bug-fseek.c94
-rw-r--r--libio/fileops.c10
3 files changed, 105 insertions, 1 deletions
diff --git a/libio/Makefile b/libio/Makefile
index d10d6c1648..132634608f 100644
--- a/libio/Makefile
+++ b/libio/Makefile
@@ -50,7 +50,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \
tst_wprintf2 tst-widetext test-fmemopen tst-ext tst-fopenloc \
tst-fgetws tst-ungetwc1 tst-ungetwc2 tst-swscanf tst-sscanf \
tst-mmap-setvbuf bug-ungetwc1 bug-ungetwc2 tst-atime tst-eof \
- tst-freopen bug-rewind bug-ungetc
+ tst-freopen bug-rewind bug-ungetc bug-fseek
test-srcs = test-freopen
all: # Make this the default target; it will be defined in Rules.
diff --git a/libio/bug-fseek.c b/libio/bug-fseek.c
new file mode 100644
index 0000000000..d8cd712f6a
--- /dev/null
+++ b/libio/bug-fseek.c
@@ -0,0 +1,94 @@
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+static char *fname;
+
+
+static void do_prepare (void);
+#define PREPARE(argc, argv) do_prepare ()
+static int do_test (void);
+#define TEST_FUNCTION do_test ()
+#include <test-skeleton.c>
+
+
+static void
+do_prepare (void)
+{
+ static const char pattern[] = "12345678901234567890";
+ int fd = create_temp_file ("bug-fseek.", &fname);
+ if (fd == -1)
+ {
+ printf ("cannot create temporary file: %m\n");
+ exit (1);
+ }
+
+ if (write (fd, pattern, sizeof (pattern)) != sizeof (pattern))
+ {
+ perror ("short write");
+ abort ();
+ }
+ close (fd);
+}
+
+
+
+static int
+do_test (void)
+{
+ FILE *f;
+ int result = 0;
+ char buf[10];
+
+
+ if ((f = fopen (fname, "r")) == (FILE *) NULL)
+ {
+ perror ("fopen(\"r\")");
+ }
+
+ fread (buf, 3, 1, f);
+ errno = 0;
+ if (fseek (f, -10, SEEK_CUR) == 0)
+ {
+ printf ("fseek() for r to before start of file worked!\n");
+ result = 1;
+ }
+ else if (errno != EINVAL)
+ {
+ printf ("\
+fseek() for r to before start of file did not set errno to EINVAL. \
+Got %d instead\n",
+ errno);
+ result = 1;
+ }
+
+ fclose (f);
+
+
+ if ((f = fopen (fname, "r+")) == (FILE *) NULL)
+ {
+ perror ("fopen(\"r+\")");
+ }
+
+ fread (buf, 3, 1, f);
+ errno = 0;
+ if (fseek (f, -10, SEEK_CUR) == 0)
+ {
+ printf ("fseek() for r+ to before start of file worked!\n");
+ result = 1;
+ }
+ else if (errno != EINVAL)
+ {
+ printf ("\
+fseek() for r+ to before start of file did not set errno to EINVAL. \
+Got %d instead\n",
+ errno);
+ result = 1;
+ }
+
+ fclose (f);
+
+ return result;
+}
diff --git a/libio/fileops.c b/libio/fileops.c
index fa4c8e0fa8..1794dce57f 100644
--- a/libio/fileops.c
+++ b/libio/fileops.c
@@ -778,6 +778,11 @@ _IO_new_file_seekoff (fp, offset, dir, mode)
goto dumb;
/* Make offset absolute, assuming current pointer is file_ptr(). */
offset += fp->_offset;
+ if (offset < 0)
+ {
+ __set_errno (EINVAL);
+ return EOF;
+ }
dir = _IO_seek_set;
break;
@@ -934,6 +939,11 @@ _IO_file_seekoff_mmap (fp, offset, dir, mode)
case _IO_seek_cur:
/* Adjust for read-ahead (bytes is buffer). */
offset += fp->_IO_read_ptr - fp->_IO_read_base;
+ if (offset < 0)
+ {
+ __set_errno (EINVAL);
+ return EOF;
+ }
break;
case _IO_seek_set:
break;