Debian Codegraph @

lib/cmpbuf.c

raw
1 /* Buffer primitives for comparison operations.
2
3 Copyright (C) 1993, 1995, 1998, 2001-2002, 2006, 2009-2013, 2015-2025 Free
4 Software Foundation, Inc.
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program 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
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20
21 #include "cmpbuf.h"
22
23 #include <errno.h>
24 #include <limits.h>
25 #include <signal.h>
26 #include <stdckdint.h>
27 #include <unistd.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <sys/types.h>
31
32 #include "intprops.h"
33 #include "minmax.h"
34
35 /* Read NBYTES bytes from descriptor FD into BUF.
36 Return the number of characters successfully read.
37 On error, return -1, setting errno.
38 The number returned is always NBYTES unless end-of-file or error. */
39
40 ptrdiff_t
41 block_read (int fd, char *buf, idx_t nbytes)
42 {
43 char *bp = buf;
44 char const *buflim = buf + nbytes;
45 idx_t readlim = MIN (IDX_MAX, MIN (SSIZE_MAX, SIZE_MAX));
46
47 do
48 {
49 idx_t bytes_remaining = buflim - bp;
50 idx_t bytes_to_read = MIN (bytes_remaining, readlim);
51 ssize_t nread = read (fd, bp, bytes_to_read);
52 if (nread <= 0)
53 {
54 if (nread == 0)
55 break;
56
57 /* Accommodate FreeBSD 13, which can't read more than INT_MAX bytes
58 when debug.iosize_max_clamp is nonzero. Prefer a power
59 of two for the read limit in this case.
60
61 Also, work around a bug in Linux kernel 6.3.8 tmpfs,
62 which fails if the current offset + bytes_to_read exceeds
63 TYPE_MAXIMUM (off_t), even if EOF occurs before then. */
64 if (errno == EINVAL)
65 {
66 if (bytes_to_read <= 1)
67 {
68 if (lseek (fd, 0, SEEK_CUR) == TYPE_MAXIMUM (off_t))
69 {
70 nread = 0;
71 break;
72 }
73 errno = EINVAL;
74 }
75 else
76 {
77 readlim = MIN (bytes_to_read >> 1, INT_MAX / 2 + 1);
78 continue;
79 }
80 }
81
82 /* This is needed for programs that have signal handlers on
83 older hosts without SA_RESTART. It also accommodates
84 ancient AIX hosts that set errno to EINTR after uncaught
85 SIGCONT. See <news:[email protected]>
86 (1993-04-22). */
87 if (! SA_RESTART && errno == EINTR)
88 continue;
89
90 return -1;
91 }
92 bp += nread;
93 }
94 while (bp < buflim);
95
96 return bp - buf;
97 }
98
99 /* Least common multiple of two buffer sizes A and B. However, if
100 either A or B is zero, or if the multiple is greater than LCM_MAX,
101 return a reasonable buffer size. */
102
103 idx_t
104 buffer_lcm (idx_t a, idx_t b, idx_t lcm_max)
105 {
106 /* Yield reasonable values if buffer sizes are zero. Although A and
107 B must be nonnegative, GCC generates better code from (A <= 0)
108 than from (A != 0). */
109 if (a <= 0)
110 return b <= 0 ? 8 * 1024 : b;
111 if (b <= 0)
112 return a;
113
114 /* n = gcd (a, b) */
115 idx_t m, n, r;
116 for (m = a, n = b; (r = m % n) != 0; m = n, n = r)
117 continue;
118
119 /* Yield a if there is an overflow. */
120 idx_t q = a / n, lcm;
121 return !ckd_mul (&lcm, b, q) && lcm <= lcm_max ? lcm : a;
122 }