/src/PROJ/curl/lib/warnless.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #if defined(__INTEL_COMPILER) && defined(__unix__) |
28 | | |
29 | | #ifdef HAVE_NETINET_IN_H |
30 | | # include <netinet/in.h> |
31 | | #endif |
32 | | #ifdef HAVE_ARPA_INET_H |
33 | | # include <arpa/inet.h> |
34 | | #endif |
35 | | |
36 | | #endif /* __INTEL_COMPILER && __unix__ */ |
37 | | |
38 | | #include "warnless.h" |
39 | | |
40 | | #ifdef _WIN32 |
41 | | #undef read |
42 | | #undef write |
43 | | #endif |
44 | | |
45 | | #include <limits.h> |
46 | | |
47 | 0 | #define CURL_MASK_UCHAR ((unsigned char)~0) |
48 | | #define CURL_MASK_SCHAR (CURL_MASK_UCHAR >> 1) |
49 | | |
50 | 0 | #define CURL_MASK_USHORT ((unsigned short)~0) |
51 | | #define CURL_MASK_SSHORT (CURL_MASK_USHORT >> 1) |
52 | | |
53 | 0 | #define CURL_MASK_UINT ((unsigned int)~0) |
54 | 0 | #define CURL_MASK_SINT (CURL_MASK_UINT >> 1) |
55 | | |
56 | 0 | #define CURL_MASK_ULONG ((unsigned long)~0) |
57 | | #define CURL_MASK_SLONG (CURL_MASK_ULONG >> 1) |
58 | | |
59 | 0 | #define CURL_MASK_UCOFFT ((unsigned CURL_TYPEOF_CURL_OFF_T)~0) |
60 | 0 | #define CURL_MASK_SCOFFT (CURL_MASK_UCOFFT >> 1) |
61 | | |
62 | 0 | #define CURL_MASK_USIZE_T ((size_t)~0) |
63 | 0 | #define CURL_MASK_SSIZE_T (CURL_MASK_USIZE_T >> 1) |
64 | | |
65 | | /* |
66 | | ** unsigned long to unsigned short |
67 | | */ |
68 | | |
69 | | unsigned short curlx_ultous(unsigned long ulnum) |
70 | 0 | { |
71 | | #ifdef __INTEL_COMPILER |
72 | | # pragma warning(push) |
73 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
74 | | #endif |
75 | |
|
76 | 0 | DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_USHORT); |
77 | 0 | return (unsigned short)(ulnum & (unsigned long) CURL_MASK_USHORT); |
78 | |
|
79 | | #ifdef __INTEL_COMPILER |
80 | | # pragma warning(pop) |
81 | | #endif |
82 | 0 | } |
83 | | |
84 | | /* |
85 | | ** unsigned long to unsigned char |
86 | | */ |
87 | | |
88 | | unsigned char curlx_ultouc(unsigned long ulnum) |
89 | 0 | { |
90 | | #ifdef __INTEL_COMPILER |
91 | | # pragma warning(push) |
92 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
93 | | #endif |
94 | |
|
95 | 0 | DEBUGASSERT(ulnum <= (unsigned long) CURL_MASK_UCHAR); |
96 | 0 | return (unsigned char)(ulnum & (unsigned long) CURL_MASK_UCHAR); |
97 | |
|
98 | | #ifdef __INTEL_COMPILER |
99 | | # pragma warning(pop) |
100 | | #endif |
101 | 0 | } |
102 | | |
103 | | /* |
104 | | ** unsigned size_t to signed curl_off_t |
105 | | */ |
106 | | |
107 | | curl_off_t curlx_uztoso(size_t uznum) |
108 | 0 | { |
109 | | #ifdef __INTEL_COMPILER |
110 | | # pragma warning(push) |
111 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
112 | | #elif defined(_MSC_VER) |
113 | | # pragma warning(push) |
114 | | # pragma warning(disable:4310) /* cast truncates constant value */ |
115 | | #endif |
116 | |
|
117 | 0 | DEBUGASSERT(uznum <= (size_t) CURL_MASK_SCOFFT); |
118 | 0 | return (curl_off_t)(uznum & (size_t) CURL_MASK_SCOFFT); |
119 | |
|
120 | | #if defined(__INTEL_COMPILER) || defined(_MSC_VER) |
121 | | # pragma warning(pop) |
122 | | #endif |
123 | 0 | } |
124 | | |
125 | | /* |
126 | | ** unsigned size_t to signed int |
127 | | */ |
128 | | |
129 | | int curlx_uztosi(size_t uznum) |
130 | 0 | { |
131 | | #ifdef __INTEL_COMPILER |
132 | | # pragma warning(push) |
133 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
134 | | #endif |
135 | |
|
136 | 0 | DEBUGASSERT(uznum <= (size_t) CURL_MASK_SINT); |
137 | 0 | return (int)(uznum & (size_t) CURL_MASK_SINT); |
138 | |
|
139 | | #ifdef __INTEL_COMPILER |
140 | | # pragma warning(pop) |
141 | | #endif |
142 | 0 | } |
143 | | |
144 | | /* |
145 | | ** unsigned size_t to unsigned long |
146 | | */ |
147 | | |
148 | | unsigned long curlx_uztoul(size_t uznum) |
149 | 0 | { |
150 | | #ifdef __INTEL_COMPILER |
151 | | # pragma warning(push) |
152 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
153 | | #endif |
154 | |
|
155 | | #if ULONG_MAX < SIZE_T_MAX |
156 | | DEBUGASSERT(uznum <= (size_t) CURL_MASK_ULONG); |
157 | | #endif |
158 | 0 | return (unsigned long)(uznum & (size_t) CURL_MASK_ULONG); |
159 | |
|
160 | | #ifdef __INTEL_COMPILER |
161 | | # pragma warning(pop) |
162 | | #endif |
163 | 0 | } |
164 | | |
165 | | /* |
166 | | ** unsigned size_t to unsigned int |
167 | | */ |
168 | | |
169 | | unsigned int curlx_uztoui(size_t uznum) |
170 | 0 | { |
171 | | #ifdef __INTEL_COMPILER |
172 | | # pragma warning(push) |
173 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
174 | | #endif |
175 | |
|
176 | 0 | #if UINT_MAX < SIZE_T_MAX |
177 | 0 | DEBUGASSERT(uznum <= (size_t) CURL_MASK_UINT); |
178 | 0 | #endif |
179 | 0 | return (unsigned int)(uznum & (size_t) CURL_MASK_UINT); |
180 | |
|
181 | | #ifdef __INTEL_COMPILER |
182 | | # pragma warning(pop) |
183 | | #endif |
184 | 0 | } |
185 | | |
186 | | /* |
187 | | ** signed long to signed int |
188 | | */ |
189 | | |
190 | | int curlx_sltosi(long slnum) |
191 | 0 | { |
192 | | #ifdef __INTEL_COMPILER |
193 | | # pragma warning(push) |
194 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
195 | | #endif |
196 | |
|
197 | 0 | DEBUGASSERT(slnum >= 0); |
198 | 0 | #if INT_MAX < LONG_MAX |
199 | 0 | DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_SINT); |
200 | 0 | #endif |
201 | 0 | return (int)(slnum & (long) CURL_MASK_SINT); |
202 | |
|
203 | | #ifdef __INTEL_COMPILER |
204 | | # pragma warning(pop) |
205 | | #endif |
206 | 0 | } |
207 | | |
208 | | /* |
209 | | ** signed long to unsigned int |
210 | | */ |
211 | | |
212 | | unsigned int curlx_sltoui(long slnum) |
213 | 0 | { |
214 | | #ifdef __INTEL_COMPILER |
215 | | # pragma warning(push) |
216 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
217 | | #endif |
218 | |
|
219 | 0 | DEBUGASSERT(slnum >= 0); |
220 | 0 | #if UINT_MAX < LONG_MAX |
221 | 0 | DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_UINT); |
222 | 0 | #endif |
223 | 0 | return (unsigned int)(slnum & (long) CURL_MASK_UINT); |
224 | |
|
225 | | #ifdef __INTEL_COMPILER |
226 | | # pragma warning(pop) |
227 | | #endif |
228 | 0 | } |
229 | | |
230 | | /* |
231 | | ** signed long to unsigned short |
232 | | */ |
233 | | |
234 | | unsigned short curlx_sltous(long slnum) |
235 | 0 | { |
236 | | #ifdef __INTEL_COMPILER |
237 | | # pragma warning(push) |
238 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
239 | | #endif |
240 | |
|
241 | 0 | DEBUGASSERT(slnum >= 0); |
242 | 0 | DEBUGASSERT((unsigned long) slnum <= (unsigned long) CURL_MASK_USHORT); |
243 | 0 | return (unsigned short)(slnum & (long) CURL_MASK_USHORT); |
244 | |
|
245 | | #ifdef __INTEL_COMPILER |
246 | | # pragma warning(pop) |
247 | | #endif |
248 | 0 | } |
249 | | |
250 | | /* |
251 | | ** unsigned size_t to signed ssize_t |
252 | | */ |
253 | | |
254 | | ssize_t curlx_uztosz(size_t uznum) |
255 | 0 | { |
256 | | #ifdef __INTEL_COMPILER |
257 | | # pragma warning(push) |
258 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
259 | | #endif |
260 | |
|
261 | 0 | DEBUGASSERT(uznum <= (size_t) CURL_MASK_SSIZE_T); |
262 | 0 | return (ssize_t)(uznum & (size_t) CURL_MASK_SSIZE_T); |
263 | |
|
264 | | #ifdef __INTEL_COMPILER |
265 | | # pragma warning(pop) |
266 | | #endif |
267 | 0 | } |
268 | | |
269 | | /* |
270 | | ** signed curl_off_t to unsigned size_t |
271 | | */ |
272 | | |
273 | | size_t curlx_sotouz(curl_off_t sonum) |
274 | 0 | { |
275 | | #ifdef __INTEL_COMPILER |
276 | | # pragma warning(push) |
277 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
278 | | #endif |
279 | |
|
280 | 0 | DEBUGASSERT(sonum >= 0); |
281 | 0 | return (size_t)(sonum & (curl_off_t) CURL_MASK_USIZE_T); |
282 | |
|
283 | | #ifdef __INTEL_COMPILER |
284 | | # pragma warning(pop) |
285 | | #endif |
286 | 0 | } |
287 | | |
288 | | /* |
289 | | ** signed ssize_t to signed int |
290 | | */ |
291 | | |
292 | | int curlx_sztosi(ssize_t sznum) |
293 | 0 | { |
294 | | #ifdef __INTEL_COMPILER |
295 | | # pragma warning(push) |
296 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
297 | | #endif |
298 | |
|
299 | 0 | DEBUGASSERT(sznum >= 0); |
300 | 0 | #if INT_MAX < SSIZE_T_MAX |
301 | 0 | DEBUGASSERT((size_t) sznum <= (size_t) CURL_MASK_SINT); |
302 | 0 | #endif |
303 | 0 | return (int)(sznum & (ssize_t) CURL_MASK_SINT); |
304 | |
|
305 | | #ifdef __INTEL_COMPILER |
306 | | # pragma warning(pop) |
307 | | #endif |
308 | 0 | } |
309 | | |
310 | | /* |
311 | | ** unsigned int to unsigned short |
312 | | */ |
313 | | |
314 | | unsigned short curlx_uitous(unsigned int uinum) |
315 | 0 | { |
316 | | #ifdef __INTEL_COMPILER |
317 | | # pragma warning(push) |
318 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
319 | | #endif |
320 | |
|
321 | 0 | DEBUGASSERT(uinum <= (unsigned int) CURL_MASK_USHORT); |
322 | 0 | return (unsigned short) (uinum & (unsigned int) CURL_MASK_USHORT); |
323 | |
|
324 | | #ifdef __INTEL_COMPILER |
325 | | # pragma warning(pop) |
326 | | #endif |
327 | 0 | } |
328 | | |
329 | | /* |
330 | | ** signed int to unsigned size_t |
331 | | */ |
332 | | |
333 | | size_t curlx_sitouz(int sinum) |
334 | 0 | { |
335 | | #ifdef __INTEL_COMPILER |
336 | | # pragma warning(push) |
337 | | # pragma warning(disable:810) /* conversion may lose significant bits */ |
338 | | #endif |
339 | |
|
340 | 0 | DEBUGASSERT(sinum >= 0); |
341 | 0 | return (size_t) sinum; |
342 | |
|
343 | | #ifdef __INTEL_COMPILER |
344 | | # pragma warning(pop) |
345 | | #endif |
346 | 0 | } |
347 | | |
348 | | #ifdef USE_WINSOCK |
349 | | |
350 | | /* |
351 | | ** curl_socket_t to signed int |
352 | | */ |
353 | | |
354 | | int curlx_sktosi(curl_socket_t s) |
355 | | { |
356 | | return (int)((ssize_t) s); |
357 | | } |
358 | | |
359 | | /* |
360 | | ** signed int to curl_socket_t |
361 | | */ |
362 | | |
363 | | curl_socket_t curlx_sitosk(int i) |
364 | | { |
365 | | return (curl_socket_t)((ssize_t) i); |
366 | | } |
367 | | |
368 | | #endif /* USE_WINSOCK */ |
369 | | |
370 | | #if defined(_WIN32) |
371 | | |
372 | | ssize_t curlx_read(int fd, void *buf, size_t count) |
373 | | { |
374 | | return (ssize_t)read(fd, buf, curlx_uztoui(count)); |
375 | | } |
376 | | |
377 | | ssize_t curlx_write(int fd, const void *buf, size_t count) |
378 | | { |
379 | | return (ssize_t)write(fd, buf, curlx_uztoui(count)); |
380 | | } |
381 | | |
382 | | #endif /* _WIN32 */ |
383 | | |
384 | | /* Ensure that warnless.h redefinitions continue to have an effect |
385 | | in "unity" builds. */ |
386 | | #undef HEADER_CURL_WARNLESS_H_REDEFS |