Document CVE-2024-2961
[glibc.git] / libio / bug-fseek.c
blob19d5e2429e36f615d8404bf097dfed5ff8a5e8eb
1 #include <errno.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
6 #include <support/xstdio.h>
8 static char *fname;
11 static void do_prepare (void);
12 #define PREPARE(argc, argv) do_prepare ()
13 static int do_test (void);
14 #define TEST_FUNCTION do_test ()
15 #include <test-skeleton.c>
18 static void
19 do_prepare (void)
21 static const char pattern[] = "12345678901234567890";
22 int fd = create_temp_file ("bug-fseek.", &fname);
23 if (fd == -1)
25 printf ("cannot create temporary file: %m\n");
26 exit (1);
29 if (write (fd, pattern, sizeof (pattern)) != sizeof (pattern))
31 perror ("short write");
32 abort ();
34 close (fd);
39 static int
40 do_test (void)
42 FILE *f;
43 int result = 0;
44 char buf[10];
47 if ((f = fopen (fname, "r")) == (FILE *) NULL)
49 perror ("fopen(\"r\")");
52 xfread (buf, 3, 1, f);
53 errno = 0;
54 if (fseek (f, -10, SEEK_CUR) == 0)
56 printf ("fseek() for r to before start of file worked!\n");
57 result = 1;
59 else if (errno != EINVAL)
61 printf ("\
62 fseek() for r to before start of file did not set errno to EINVAL. \
63 Got %d instead\n",
64 errno);
65 result = 1;
68 fclose (f);
71 if ((f = fopen (fname, "r+")) == (FILE *) NULL)
73 perror ("fopen(\"r+\")");
76 xfread (buf, 3, 1, f);
77 errno = 0;
78 if (fseek (f, -10, SEEK_CUR) == 0)
80 printf ("fseek() for r+ to before start of file worked!\n");
81 result = 1;
83 else if (errno != EINVAL)
85 printf ("\
86 fseek() for r+ to before start of file did not set errno to EINVAL. \
87 Got %d instead\n",
88 errno);
89 result = 1;
92 fclose (f);
95 if ((f = fopen (fname, "r+")) == (FILE *) NULL)
97 perror ("fopen(\"r+\")");
100 xfread (buf, 3, 1, f);
101 if (ftell (f) != 3)
103 puts ("ftell failed");
104 return 1;
106 errno = 0;
107 if (fseek (f, -10, SEEK_CUR) == 0)
109 printf ("fseek() for r+ to before start of file worked!\n");
110 result = 1;
112 else if (errno != EINVAL)
114 printf ("\
115 fseek() for r+ to before start of file did not set errno to EINVAL. \
116 Got %d instead\n",
117 errno);
118 result = 1;
121 fclose (f);
123 return result;