Fix invalid file descriptor reuse while sending DNS query (BZ #15946)
[glibc.git] / sysdeps / posix / preadv.c
blobb1fd060b30dd7b342b383ee352488d44281cfab0
1 /* Copyright (C) 2009-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <stdbool.h>
24 #include <sys/param.h>
25 #if __WORDSIZE == 64 && !defined PREADV
26 /* Hide the preadv64 declaration. */
27 # define preadv64 __redirect_preadv64
28 #endif
29 #include <sys/uio.h>
30 #include <bits/wordsize.h>
32 #ifndef PREADV
33 # define PREADV preadv
34 # define PREAD __pread
35 # define OFF_T off_t
36 #endif
39 static void
40 ifree (char **ptrp)
42 free (*ptrp);
46 /* Read data from file descriptor FD at the given position OFFSET
47 without change the file pointer, and put the result in the buffers
48 described by VECTOR, which is a vector of COUNT 'struct iovec's.
49 The buffers are filled in the order specified. Operates just like
50 'pread' (see <unistd.h>) except that data are put in VECTOR instead
51 of a contiguous buffer. */
52 ssize_t
53 PREADV (int fd, const struct iovec *vector, int count, OFF_T offset)
55 /* Find the total number of bytes to be read. */
56 size_t bytes = 0;
57 for (int i = 0; i < count; ++i)
59 /* Check for ssize_t overflow. */
60 if (SSIZE_MAX - bytes < vector[i].iov_len)
62 __set_errno (EINVAL);
63 return -1;
65 bytes += vector[i].iov_len;
68 /* Allocate a temporary buffer to hold the data. We should normally
69 use alloca since it's faster and does not require synchronization
70 with other threads. But we cannot if the amount of memory
71 required is too large. */
72 char *buffer;
73 char *malloced_buffer __attribute__ ((__cleanup__ (ifree))) = NULL;
74 if (__libc_use_alloca (bytes))
75 buffer = (char *) __alloca (bytes);
76 else
78 malloced_buffer = buffer = (char *) malloc (bytes);
79 if (buffer == NULL)
80 return -1;
83 /* Read the data. */
84 ssize_t bytes_read = PREAD (fd, buffer, bytes, offset);
85 if (bytes_read < 0)
86 return -1;
88 /* Copy the data from BUFFER into the memory specified by VECTOR. */
89 bytes = bytes_read;
90 for (int i = 0; i < count; ++i)
92 size_t copy = MIN (vector[i].iov_len, bytes);
94 (void) memcpy ((void *) vector[i].iov_base, (void *) buffer, copy);
96 buffer += copy;
97 bytes -= copy;
98 if (bytes == 0)
99 break;
102 return bytes_read;
104 #if __WORDSIZE == 64 && defined preadv64
105 # undef preadv64
106 strong_alias (preadv, preadv64)
107 #endif