]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/generic/wordsize-32/overflow.h
45efcd8fd34cf24322fda2ab32165b65fa23ce15
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / generic / wordsize-32 / overflow.h
1 /* Overflow tests for stat, statfs, and lseek functions.
2 Copyright (C) 2011-2019 Free Software Foundation, Inc.
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
17 License along with the GNU C Library. If not, see
18 <https://www.gnu.org/licenses/>. */
19
20 #include <sys/stat.h>
21 #include <sys/statfs.h>
22
23 /* Test for overflows of structures where we ask the kernel to fill them
24 in with standard 64-bit syscalls but return them through APIs that
25 only expose the low 32 bits of some fields. */
26
27 static inline off_t lseek_overflow (loff_t res)
28 {
29 off_t retval = (off_t) res;
30 if (retval == res)
31 return retval;
32
33 __set_errno (EOVERFLOW);
34 return (off_t) -1;
35 }
36
37 static inline int stat_overflow (struct stat *buf)
38 {
39 if (buf->__st_ino_pad == 0 && buf->__st_size_pad == 0
40 && buf->__st_blocks_pad == 0)
41 return 0;
42
43 __set_errno (EOVERFLOW);
44 return -1;
45 }
46
47 /* Note that f_files and f_ffree may validly be a sign-extended -1. */
48 static inline int statfs_overflow (struct statfs *buf)
49 {
50 if (buf->__f_blocks_pad == 0 && buf->__f_bfree_pad == 0
51 && buf->__f_bavail_pad == 0
52 && (buf->__f_files_pad == 0
53 || (buf->f_files == -1U && buf->__f_files_pad == -1))
54 && (buf->__f_ffree_pad == 0
55 || (buf->f_ffree == -1U && buf->__f_ffree_pad == -1)))
56 return 0;
57
58 __set_errno (EOVERFLOW);
59 return -1;
60 }