annotate lib/memchr.c @ 425:67815606c5d9 3.0-beta2

Prevent NULL deref in lw_expr_destroy
author lost@l-w.ca
date Sun, 19 Sep 2010 10:27:47 -0600
parents 3c2e1f24cc7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
421
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
1 /* Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2004, 2006, 2008-2010
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
2 Free Software Foundation, Inc.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
3
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
4 Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
5 with help from Dan Sahlin (dan@sics.se) and
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
6 commentary by Jim Blandy (jimb@ai.mit.edu);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
7 adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
8 and implemented by Roland McGrath (roland@ai.mit.edu).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
9
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
10 NOTE: The canonical source of this file is maintained with the GNU C Library.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
11 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
12
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
13 This program is free software: you can redistribute it and/or modify it
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
14 under the terms of the GNU General Public License as published by the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
15 Free Software Foundation; either version 3 of the License, or any
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
16 later version.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
17
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
18 This program is distributed in the hope that it will be useful,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
21 GNU General Public License for more details.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
22
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
23 You should have received a copy of the GNU General Public License
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
24 along with this program. If not, see <http://www.gnu.org/licenses/>. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
25
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
26 #ifndef _LIBC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
27 # include <config.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
28 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
29
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
30 #include <string.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
31
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
32 #include <stddef.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
33
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
34 #if defined _LIBC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
35 # include <memcopy.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
36 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
37 # define reg_char char
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
38 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
39
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
40 #include <limits.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
41
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
42 #if HAVE_BP_SYM_H || defined _LIBC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
43 # include <bp-sym.h>
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
44 #else
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
45 # define BP_SYM(sym) sym
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
46 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
47
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
48 #undef __memchr
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
49 #ifdef _LIBC
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
50 # undef memchr
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
51 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
52
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
53 #ifndef weak_alias
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
54 # define __memchr memchr
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
55 #endif
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
56
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
57 /* Search no more than N bytes of S for C. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
58 void *
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
59 __memchr (void const *s, int c_in, size_t n)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
60 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
61 /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
62 long instead of a 64-bit uintmax_t tends to give better
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
63 performance. On 64-bit hardware, unsigned long is generally 64
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
64 bits already. Change this typedef to experiment with
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
65 performance. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
66 typedef unsigned long int longword;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
67
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
68 const unsigned char *char_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
69 const longword *longword_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
70 longword repeated_one;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
71 longword repeated_c;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
72 unsigned reg_char c;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
73
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
74 c = (unsigned char) c_in;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
75
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
76 /* Handle the first few bytes by reading one byte at a time.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
77 Do this until CHAR_PTR is aligned on a longword boundary. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
78 for (char_ptr = (const unsigned char *) s;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
79 n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
80 --n, ++char_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
81 if (*char_ptr == c)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
82 return (void *) char_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
83
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
84 longword_ptr = (const longword *) char_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
85
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
86 /* All these elucidatory comments refer to 4-byte longwords,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
87 but the theory applies equally well to any size longwords. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
88
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
89 /* Compute auxiliary longword values:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
90 repeated_one is a value which has a 1 in every byte.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
91 repeated_c has c in every byte. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
92 repeated_one = 0x01010101;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
93 repeated_c = c | (c << 8);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
94 repeated_c |= repeated_c << 16;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
95 if (0xffffffffU < (longword) -1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
96 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
97 repeated_one |= repeated_one << 31 << 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
98 repeated_c |= repeated_c << 31 << 1;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
99 if (8 < sizeof (longword))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
100 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
101 size_t i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
102
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
103 for (i = 64; i < sizeof (longword) * 8; i *= 2)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
104 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
105 repeated_one |= repeated_one << i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
106 repeated_c |= repeated_c << i;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
107 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
108 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
109 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
110
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
111 /* Instead of the traditional loop which tests each byte, we will test a
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
112 longword at a time. The tricky part is testing if *any of the four*
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
113 bytes in the longword in question are equal to c. We first use an xor
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
114 with repeated_c. This reduces the task to testing whether *any of the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
115 four* bytes in longword1 is zero.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
116
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
117 We compute tmp =
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
118 ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
119 That is, we perform the following operations:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
120 1. Subtract repeated_one.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
121 2. & ~longword1.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
122 3. & a mask consisting of 0x80 in every byte.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
123 Consider what happens in each byte:
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
124 - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
125 and step 3 transforms it into 0x80. A carry can also be propagated
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
126 to more significant bytes.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
127 - If a byte of longword1 is nonzero, let its lowest 1 bit be at
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
128 position k (0 <= k <= 7); so the lowest k bits are 0. After step 1,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
129 the byte ends in a single bit of value 0 and k bits of value 1.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
130 After step 2, the result is just k bits of value 1: 2^k - 1. After
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
131 step 3, the result is 0. And no carry is produced.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
132 So, if longword1 has only non-zero bytes, tmp is zero.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
133 Whereas if longword1 has a zero byte, call j the position of the least
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
134 significant zero byte. Then the result has a zero at positions 0, ...,
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
135 j-1 and a 0x80 at position j. We cannot predict the result at the more
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
136 significant bytes (positions j+1..3), but it does not matter since we
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
137 already have a non-zero bit at position 8*j+7.
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
138
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
139 So, the test whether any byte in longword1 is zero is equivalent to
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
140 testing whether tmp is nonzero. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
141
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
142 while (n >= sizeof (longword))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
143 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
144 longword longword1 = *longword_ptr ^ repeated_c;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
145
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
146 if ((((longword1 - repeated_one) & ~longword1)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
147 & (repeated_one << 7)) != 0)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
148 break;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
149 longword_ptr++;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
150 n -= sizeof (longword);
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
151 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
152
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
153 char_ptr = (const unsigned char *) longword_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
154
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
155 /* At this point, we know that either n < sizeof (longword), or one of the
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
156 sizeof (longword) bytes starting at char_ptr is == c. On little-endian
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
157 machines, we could determine the first such byte without any further
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
158 memory accesses, just by looking at the tmp result from the last loop
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
159 iteration. But this does not work on big-endian machines. Choose code
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
160 that works in both cases. */
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
161
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
162 for (; n > 0; --n, ++char_ptr)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
163 {
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
164 if (*char_ptr == c)
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
165 return (void *) char_ptr;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
166 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
167
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
168 return NULL;
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
169 }
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
170 #ifdef weak_alias
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
171 weak_alias (__memchr, BP_SYM (memchr))
3c2e1f24cc7d Added generated files for release
lost@l-w.ca
parents:
diff changeset
172 #endif