]>
Commit | Line | Data |
---|---|---|
a63c7fa1 | 1 | /* Overflow tests for stat, statfs, and lseek functions. |
2b778ceb | 2 | Copyright (C) 2011-2021 Free Software Foundation, Inc. |
a63c7fa1 CM |
3 | This file is part of the GNU C Library. |
4 | Contributed by Chris Metcalf <cmetcalf@tilera.com>, 2011. | |
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 | |
ab84e3ff | 17 | License along with the GNU C Library. If not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
a63c7fa1 CM |
19 | |
20 | #include <sys/stat.h> | |
21 | #include <sys/statfs.h> | |
01bd6251 | 22 | #include <sys/types.h> |
a63c7fa1 CM |
23 | |
24 | /* Test for overflows of structures where we ask the kernel to fill them | |
25 | in with standard 64-bit syscalls but return them through APIs that | |
26 | only expose the low 32 bits of some fields. */ | |
27 | ||
28 | static inline off_t lseek_overflow (loff_t res) | |
29 | { | |
30 | off_t retval = (off_t) res; | |
31 | if (retval == res) | |
32 | return retval; | |
33 | ||
34 | __set_errno (EOVERFLOW); | |
35 | return (off_t) -1; | |
36 | } | |
37 | ||
38 | static inline int stat_overflow (struct stat *buf) | |
39 | { | |
c8200592 AF |
40 | #if defined __INO_T_MATCHES_INO64_T |
41 | return 0; | |
42 | #else | |
c5f65462 JM |
43 | if (buf->__st_ino_pad == 0 && buf->__st_size_pad == 0 |
44 | && buf->__st_blocks_pad == 0) | |
a63c7fa1 CM |
45 | return 0; |
46 | ||
47 | __set_errno (EOVERFLOW); | |
48 | return -1; | |
c8200592 | 49 | #endif |
a63c7fa1 CM |
50 | } |
51 | ||
52 | /* Note that f_files and f_ffree may validly be a sign-extended -1. */ | |
53 | static inline int statfs_overflow (struct statfs *buf) | |
54 | { | |
acab0594 AF |
55 | #if __STATFS_MATCHES_STATFS64 |
56 | return 0; | |
57 | #else | |
c5f65462 JM |
58 | if (buf->__f_blocks_pad == 0 && buf->__f_bfree_pad == 0 |
59 | && buf->__f_bavail_pad == 0 | |
60 | && (buf->__f_files_pad == 0 | |
61 | || (buf->f_files == -1U && buf->__f_files_pad == -1)) | |
62 | && (buf->__f_ffree_pad == 0 | |
63 | || (buf->f_ffree == -1U && buf->__f_ffree_pad == -1))) | |
a63c7fa1 CM |
64 | return 0; |
65 | ||
66 | __set_errno (EOVERFLOW); | |
67 | return -1; | |
acab0594 | 68 | #endif |
a63c7fa1 | 69 | } |