]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/tst-getdents64.c
24e77e04d8145263e3937be1e14eb23abeed83a4
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / tst-getdents64.c
1 /* Test for reading directories with getdents64.
2 Copyright (C) 2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <dirent.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <limits.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <support/check.h>
28 #include <support/support.h>
29 #include <support/xunistd.h>
30 #include <unistd.h>
31
32 /* Called by large_buffer_checks below. */
33 static void
34 large_buffer_check (int fd, char *large_buffer, size_t large_buffer_size)
35 {
36 xlseek (fd, 0, SEEK_SET);
37 ssize_t ret = getdents64 (fd, large_buffer, large_buffer_size);
38 if (ret < 0)
39 FAIL_EXIT1 ("getdents64 for buffer of %zu bytes failed: %m",
40 large_buffer_size);
41 if (ret < offsetof (struct dirent64, d_name))
42 FAIL_EXIT1 ("getdents64 for buffer of %zu returned small value %zd",
43 large_buffer_size, ret);
44 }
45
46 /* Bug 24740: Make sure that the system call argument is adjusted
47 properly for the int type. A large value should stay a large
48 value, and not wrap around to something small, causing the system
49 call to fail with EINVAL. */
50 static void
51 large_buffer_checks (int fd)
52 {
53 size_t large_buffer_size;
54 if (!__builtin_add_overflow (UINT_MAX, 2, &large_buffer_size))
55 {
56 char *large_buffer = malloc (large_buffer_size);
57 if (large_buffer == NULL)
58 printf ("warning: could not allocate %zu bytes of memory,"
59 " subtests skipped\n", large_buffer_size);
60 else
61 {
62 large_buffer_check (fd, large_buffer, INT_MAX);
63 large_buffer_check (fd, large_buffer, (size_t) INT_MAX + 1);
64 large_buffer_check (fd, large_buffer, (size_t) INT_MAX + 2);
65 large_buffer_check (fd, large_buffer, UINT_MAX);
66 large_buffer_check (fd, large_buffer, (size_t) UINT_MAX + 1);
67 large_buffer_check (fd, large_buffer, (size_t) UINT_MAX + 2);
68 }
69 free (large_buffer);
70 }
71 }
72
73 static int
74 do_test (void)
75 {
76 /* The test compares the iteration order with readdir64. */
77 DIR *reference = opendir (".");
78 TEST_VERIFY_EXIT (reference != NULL);
79
80 int fd = xopen (".", O_RDONLY | O_DIRECTORY, 0);
81 TEST_VERIFY (fd >= 0);
82
83 /* Perform two passes, with a rewind operating between passes. */
84 for (int pass = 0; pass < 2; ++pass)
85 {
86 /* Check that we need to fill the buffer multiple times. */
87 int read_count = 0;
88
89 while (true)
90 {
91 /* Simple way to make sure that the memcpy below does not read
92 non-existing data. */
93 struct
94 {
95 char buffer[1024];
96 struct dirent64 pad;
97 } data;
98
99 ssize_t ret = getdents64 (fd, &data.buffer, sizeof (data.buffer));
100 if (ret < 0)
101 FAIL_EXIT1 ("getdents64: %m");
102 if (ret == 0)
103 break;
104 ++read_count;
105
106 char *current = data.buffer;
107 char *end = data.buffer + ret;
108 while (current != end)
109 {
110 struct dirent64 entry;
111 memcpy (&entry, current, sizeof (entry));
112 /* Truncate overlong strings. */
113 entry.d_name[sizeof (entry.d_name) - 1] = '\0';
114 TEST_VERIFY (strlen (entry.d_name) < sizeof (entry.d_name) - 1);
115
116 errno = 0;
117 struct dirent64 *refentry = readdir64 (reference);
118 if (refentry == NULL && errno == 0)
119 FAIL_EXIT1 ("readdir64 failed too early, at: %s",
120 entry.d_name);
121 else if (refentry == NULL)
122 FAIL_EXIT1 ("readdir64: %m");
123
124 TEST_COMPARE_STRING (entry.d_name, refentry->d_name);
125 TEST_COMPARE (entry.d_ino, refentry->d_ino);
126 TEST_COMPARE (entry.d_off, refentry->d_off);
127 TEST_COMPARE (entry.d_type, refentry->d_type);
128
129 /* Offset zero is reserved for the first entry. */
130 TEST_VERIFY (entry.d_off != 0);
131
132 TEST_VERIFY_EXIT (entry.d_reclen <= end - current);
133 current += entry.d_reclen;
134 }
135 }
136
137 /* We expect to have reached the end of the stream. */
138 errno = 0;
139 TEST_VERIFY (readdir64 (reference) == NULL);
140 TEST_COMPARE (errno, 0);
141
142 /* direntries_read has been called more than once. */
143 TEST_VERIFY (read_count > 0);
144
145 /* Rewind both directory streams. */
146 xlseek (fd, 0, SEEK_SET);
147 rewinddir (reference);
148 }
149
150 large_buffer_checks (fd);
151
152 xclose (fd);
153 closedir (reference);
154 return 0;
155 }
156
157 #include <support/test-driver.c>