Fix access after end of search string in regex matcher
[platform/upstream/glibc.git] / posix / tst-truncate.c
1 /* Tests for ftruncate and truncate.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 2000.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <errno.h>
22 #include <error.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26
27
28 /* Allow testing of the 64-bit versions as well.  */
29 #ifndef TRUNCATE
30 # define TRUNCATE truncate
31 # define FTRUNCATE ftruncate
32 #endif
33
34 #define STRINGIFY(s) STRINGIFY2 (s)
35 #define STRINGIFY2(s) #s
36
37 /* Prototype for our test function.  */
38 extern void do_prepare (int argc, char *argv[]);
39 extern int do_test (int argc, char *argv[]);
40
41 /* We have a preparation function.  */
42 #define PREPARE do_prepare
43
44 /* We might need a bit longer timeout.  */
45 #define TIMEOUT 20 /* sec */
46
47 /* This defines the `main' function and some more.  */
48 #include <test-skeleton.c>
49
50 /* These are for the temporary file we generate.  */
51 char *name;
52 int fd;
53
54 void
55 do_prepare (int argc, char *argv[])
56 {
57    size_t name_len;
58
59 #define FNAME FNAME2(TRUNCATE)
60 #define FNAME2(s) "/" STRINGIFY(s) "XXXXXX"
61
62    name_len = strlen (test_dir);
63    name = malloc (name_len + sizeof (FNAME));
64    mempcpy (mempcpy (name, test_dir, name_len), FNAME, sizeof (FNAME));
65    add_temp_file (name);
66
67    /* Open our test file.   */
68    fd = mkstemp (name);
69    if (fd == -1)
70      error (EXIT_FAILURE, errno, "cannot open test file `%s'", name);
71 }
72
73
74 int
75 do_test (int argc, char *argv[])
76 {
77   struct stat st;
78   char buf[1000];
79
80   memset (buf, '\0', sizeof (buf));
81
82   if (write (fd, buf, sizeof (buf)) != sizeof (buf))
83     error (EXIT_FAILURE, errno, "during write");
84
85   if (fstat (fd, &st) < 0 || st.st_size != sizeof (buf))
86     error (EXIT_FAILURE, 0, "initial size wrong");
87
88
89   if (FTRUNCATE (fd, 800) < 0)
90     error (EXIT_FAILURE, errno, "size reduction with %s failed",
91            STRINGIFY (FTRUNCATE));
92
93   if (fstat (fd, &st) < 0 || st.st_size != 800)
94     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
95            STRINGIFY (FTRUNCATE));
96
97   /* The following test covers more than POSIX.  POSIX does not require
98      that ftruncate() can increase the file size.  But we are testing
99      Unix systems.  */
100   if (FTRUNCATE (fd, 1200) < 0)
101     error (EXIT_FAILURE, errno, "size increase with %s failed",
102            STRINGIFY (FTRUNCATE));
103
104   if (fstat (fd, &st) < 0 || st.st_size != 1200)
105     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
106            STRINGIFY (FTRUNCATE));
107
108
109   if (TRUNCATE (name, 800) < 0)
110     error (EXIT_FAILURE, errno, "size reduction with %s failed",
111            STRINGIFY (TRUNCATE));
112
113   if (fstat (fd, &st) < 0 || st.st_size != 800)
114     error (EXIT_FAILURE, 0, "size after reduction with %s incorrect",
115            STRINGIFY (TRUNCATE));
116
117   /* The following test covers more than POSIX.  POSIX does not require
118      that truncate() can increase the file size.  But we are testing
119      Unix systems.  */
120   if (TRUNCATE (name, 1200) < 0)
121     error (EXIT_FAILURE, errno, "size increase with %s failed",
122            STRINGIFY (TRUNCATE));
123
124   if (fstat (fd, &st) < 0 || st.st_size != 1200)
125     error (EXIT_FAILURE, 0, "size after increase with %s incorrect",
126            STRINGIFY (TRUNCATE));
127
128
129   close (fd);
130   unlink (name);
131
132   return 0;
133 }