re_search_internal: Avoid overflow in computing re_malloc buffer size
[glibc.git] / libio / bug-fseek.c
blob1b60580b53cef07a9de7c735133e4cb207fdcb1f
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
7 static char *fname;
10 static void do_prepare (void);
11 #define PREPARE(argc, argv) do_prepare ()
12 static int do_test (void);
13 #define TEST_FUNCTION do_test ()
14 #include <test-skeleton.c>
17 static void
18 do_prepare (void)
20 static const char pattern[] = "12345678901234567890";
21 int fd = create_temp_file ("bug-fseek.", &fname);
22 if (fd == -1)
24 printf ("cannot create temporary file: %m\n");
25 exit (1);
28 if (write (fd, pattern, sizeof (pattern)) != sizeof (pattern))
30 perror ("short write");
31 abort ();
33 close (fd);
38 static int
39 do_test (void)
41 FILE *f;
42 int result = 0;
43 char buf[10];
46 if ((f = fopen (fname, "r")) == (FILE *) NULL)
48 perror ("fopen(\"r\")");
51 fread (buf, 3, 1, f);
52 errno = 0;
53 if (fseek (f, -10, SEEK_CUR) == 0)
55 printf ("fseek() for r to before start of file worked!\n");
56 result = 1;
58 else if (errno != EINVAL)
60 printf ("\
61 fseek() for r to before start of file did not set errno to EINVAL. \
62 Got %d instead\n",
63 errno);
64 result = 1;
67 fclose (f);
70 if ((f = fopen (fname, "r+")) == (FILE *) NULL)
72 perror ("fopen(\"r+\")");
75 fread (buf, 3, 1, f);
76 errno = 0;
77 if (fseek (f, -10, SEEK_CUR) == 0)
79 printf ("fseek() for r+ to before start of file worked!\n");
80 result = 1;
82 else if (errno != EINVAL)
84 printf ("\
85 fseek() for r+ to before start of file did not set errno to EINVAL. \
86 Got %d instead\n",
87 errno);
88 result = 1;
91 fclose (f);
94 if ((f = fopen (fname, "r+")) == (FILE *) NULL)
96 perror ("fopen(\"r+\")");
99 fread (buf, 3, 1, f);
100 if (ftell (f) != 3)
102 puts ("ftell failed");
103 return 1;
105 errno = 0;
106 if (fseek (f, -10, SEEK_CUR) == 0)
108 printf ("fseek() for r+ to before start of file worked!\n");
109 result = 1;
111 else if (errno != EINVAL)
113 printf ("\
114 fseek() for r+ to before start of file did not set errno to EINVAL. \
115 Got %d instead\n",
116 errno);
117 result = 1;
120 fclose (f);
122 return result;