Print this page
locale stuff should use libc safe lmalloc. Other fixes from tests.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libc/port/locale/rune.c
+++ new/usr/src/lib/libc/port/locale/rune.c
1 1 /*
2 2 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
3 3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 4 * Copyright (c) 1993
5 5 * The Regents of the University of California. All rights reserved.
6 6 *
7 7 * This code is derived from software contributed to Berkeley by
8 8 * Paul Borman at Krystal Technologies.
9 9 *
10 10 * Redistribution and use in source and binary forms, with or without
11 11 * modification, are permitted provided that the following conditions
12 12 * are met:
13 13 * 1. Redistributions of source code must retain the above copyright
14 14 * notice, this list of conditions and the following disclaimer.
15 15 * 2. Redistributions in binary form must reproduce the above copyright
16 16 * notice, this list of conditions and the following disclaimer in the
17 17 * documentation and/or other materials provided with the distribution.
18 18 * 4. Neither the name of the University nor the names of its contributors
19 19 * may be used to endorse or promote products derived from this software
20 20 * without specific prior written permission.
21 21 *
22 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 32 * SUCH DAMAGE.
33 33 */
34 34
35 35 #include "lint.h"
36 36 #include "file64.h"
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
37 37 #include <errno.h>
38 38 #include <stdio.h>
39 39 #include <string.h>
40 40 #include <stdlib.h>
41 41 #include <sys/types.h>
42 42 #include <sys/stat.h>
43 43 #include <sys/mman.h>
44 44 #include <fcntl.h>
45 45 #include <unistd.h>
46 46
47 +#include "libc.h"
47 48 #include "runetype.h"
48 49 #include "runefile.h"
49 50
50 51 _RuneLocale *
51 52 _Read_RuneMagi(const char *fname)
52 53 {
53 54 char *fdata, *data;
54 55 void *lastp;
55 56 _FileRuneLocale *frl;
56 57 _RuneLocale *rl;
57 58 _FileRuneEntry *frr;
58 59 _RuneEntry *rr;
59 60 struct stat sb;
60 61 int x, saverr;
61 62 void *variable;
62 63 _FileRuneEntry *runetype_ext_ranges;
63 64 _FileRuneEntry *maplower_ext_ranges;
64 65 _FileRuneEntry *mapupper_ext_ranges;
65 66 int runetype_ext_len = 0;
66 67 int fd;
67 68
68 69 if ((fd = open(fname, O_RDONLY)) < 0) {
69 70 errno = EINVAL;
70 71 return (NULL);
71 72 }
72 73
73 74 if (fstat(fd, &sb) < 0) {
74 75 (void) close(fd);
75 76 errno = EINVAL;
76 77 return (NULL);
77 78 }
78 79
79 80 if ((size_t)sb.st_size < sizeof (_FileRuneLocale)) {
80 81 (void) close(fd);
81 82 errno = EINVAL;
82 83 return (NULL);
83 84 }
84 85
85 86
86 87 fdata = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
87 88 (void) close(fd);
88 89 if (fdata == NULL) {
89 90 errno = EINVAL;
90 91 return (NULL);
91 92 }
92 93
93 94 frl = (_FileRuneLocale *)(void *)fdata;
94 95 lastp = fdata + sb.st_size;
95 96
96 97 variable = frl + 1;
97 98
98 99 if (memcmp(frl->magic, _FILE_RUNE_MAGIC_1, sizeof (frl->magic))) {
99 100 goto invalid;
100 101 }
101 102
102 103 runetype_ext_ranges = (_FileRuneEntry *)variable;
103 104 variable = runetype_ext_ranges + frl->runetype_ext_nranges;
104 105 if (variable > lastp) {
105 106 goto invalid;
106 107 }
107 108
108 109 maplower_ext_ranges = (_FileRuneEntry *)variable;
109 110 variable = maplower_ext_ranges + frl->maplower_ext_nranges;
110 111 if (variable > lastp) {
111 112 goto invalid;
112 113 }
113 114
114 115 mapupper_ext_ranges = (_FileRuneEntry *)variable;
115 116 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
116 117 if (variable > lastp) {
117 118 goto invalid;
118 119 }
119 120
120 121 frr = runetype_ext_ranges;
121 122 for (x = 0; x < frl->runetype_ext_nranges; ++x) {
122 123 uint32_t *types;
123 124
124 125 if (frr[x].map == 0) {
125 126 int len = frr[x].max - frr[x].min + 1;
126 127 types = variable;
127 128 variable = types + len;
128 129 runetype_ext_len += len;
129 130 if (variable > lastp) {
130 131 goto invalid;
131 132 }
↓ open down ↓ |
75 lines elided |
↑ open up ↑ |
132 133 }
133 134 }
134 135
135 136 if ((char *)variable + frl->variable_len > (char *)lastp) {
136 137 goto invalid;
137 138 }
138 139
139 140 /*
140 141 * Convert from disk format to host format.
141 142 */
142 - data = malloc(sizeof (_RuneLocale) +
143 + data = libc_malloc(sizeof (_RuneLocale) +
143 144 (frl->runetype_ext_nranges + frl->maplower_ext_nranges +
144 145 frl->mapupper_ext_nranges) * sizeof (_RuneEntry) +
145 146 runetype_ext_len * sizeof (*rr->__types) +
146 147 frl->variable_len);
147 148 if (data == NULL) {
148 149 saverr = errno;
149 150 (void) munmap(fdata, sb.st_size);
150 151 errno = saverr;
151 152 return (NULL);
152 153 }
153 154
154 155 rl = (_RuneLocale *)(void *)data;
155 156 rl->__variable = rl + 1;
156 157
157 158 (void) memcpy(rl->__magic, _RUNE_MAGIC_1, sizeof (rl->__magic));
158 159 (void) memcpy(rl->__encoding, frl->encoding, sizeof (rl->__encoding));
159 160
160 161 rl->__variable_len = frl->variable_len;
161 162 rl->__runetype_ext.__nranges = frl->runetype_ext_nranges;
162 163 rl->__maplower_ext.__nranges = frl->maplower_ext_nranges;
163 164 rl->__mapupper_ext.__nranges = frl->mapupper_ext_nranges;
164 165
165 166 for (x = 0; x < _CACHED_RUNES; ++x) {
166 167 rl->__runetype[x] = frl->runetype[x];
167 168 rl->__maplower[x] = frl->maplower[x];
168 169 rl->__mapupper[x] = frl->mapupper[x];
169 170 }
170 171
171 172 rl->__runetype_ext.__ranges = (_RuneEntry *)rl->__variable;
172 173 rl->__variable = rl->__runetype_ext.__ranges +
173 174 rl->__runetype_ext.__nranges;
174 175
175 176 rl->__maplower_ext.__ranges = (_RuneEntry *)rl->__variable;
176 177 rl->__variable = rl->__maplower_ext.__ranges +
177 178 rl->__maplower_ext.__nranges;
178 179
179 180 rl->__mapupper_ext.__ranges = (_RuneEntry *)rl->__variable;
180 181 rl->__variable = rl->__mapupper_ext.__ranges +
181 182 rl->__mapupper_ext.__nranges;
182 183
183 184 variable = mapupper_ext_ranges + frl->mapupper_ext_nranges;
184 185 frr = runetype_ext_ranges;
185 186 rr = rl->__runetype_ext.__ranges;
186 187 for (x = 0; x < rl->__runetype_ext.__nranges; ++x) {
187 188 uint32_t *types;
188 189
189 190 rr[x].__min = frr[x].min;
190 191 rr[x].__max = frr[x].max;
191 192 rr[x].__map = frr[x].map;
192 193 if (rr[x].__map == 0) {
193 194 int len = rr[x].__max - rr[x].__min + 1;
194 195 types = variable;
195 196 variable = types + len;
196 197 rr[x].__types = rl->__variable;
197 198 rl->__variable = rr[x].__types + len;
198 199 while (len-- > 0)
199 200 rr[x].__types[len] = types[len];
200 201 } else
201 202 rr[x].__types = NULL;
202 203 }
203 204
204 205 frr = maplower_ext_ranges;
205 206 rr = rl->__maplower_ext.__ranges;
206 207 for (x = 0; x < rl->__maplower_ext.__nranges; ++x) {
207 208 rr[x].__min = frr[x].min;
208 209 rr[x].__max = frr[x].max;
209 210 rr[x].__map = frr[x].map;
210 211 }
211 212
212 213 frr = mapupper_ext_ranges;
213 214 rr = rl->__mapupper_ext.__ranges;
214 215 for (x = 0; x < rl->__mapupper_ext.__nranges; ++x) {
215 216 rr[x].__min = frr[x].min;
216 217 rr[x].__max = frr[x].max;
217 218 rr[x].__map = frr[x].map;
218 219 }
219 220
220 221 (void) memcpy(rl->__variable, variable, rl->__variable_len);
221 222 (void) munmap(fdata, sb.st_size);
222 223
223 224 /*
224 225 * Go out and zero pointers that should be zero.
225 226 */
226 227 if (!rl->__variable_len)
227 228 rl->__variable = NULL;
228 229
229 230 if (!rl->__runetype_ext.__nranges)
230 231 rl->__runetype_ext.__ranges = NULL;
231 232
232 233 if (!rl->__maplower_ext.__nranges)
233 234 rl->__maplower_ext.__ranges = NULL;
234 235
235 236 if (!rl->__mapupper_ext.__nranges)
236 237 rl->__mapupper_ext.__ranges = NULL;
237 238
238 239 return (rl);
239 240
240 241 invalid:
241 242 (void) munmap(fdata, sb.st_size);
242 243 errno = EINVAL;
243 244 return (NULL);
244 245 }
↓ open down ↓ |
92 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX