Hash :
88811b7d
Author :
Date :
2023-05-14T23:27:51
timespec: fill in other members This problem was found when compiling GNU Emacs with --enable-gcc-warnings on a platform where tv_sec is 64 bits and tv_nsec is 32 bits, and struct timespec has padding. GCC -Wuse-of-uninitialized-value complained when a struct timespec initialized only via assigning to tv_sec and tv_nsec was copied via assignment (this was in lib/timespec.h’s make_timespec). Although behavior is well-defined on this platform, the warning is annoying and the behavior might not be well-defined on theoretical platforms where struct timespec has other members. To work around this, initialize all the struct’s members. * lib/getsockopt.c (rpl_getsockopt): * lib/gettime.c (gettime): * lib/gettimeofday.c (gettimeofday): * lib/glthread/thread.c (gl_thread_self): * lib/nanosleep.c (nanosleep): * lib/parse-datetime.y (digits_to_date_time, set_hhmmss) (signed_seconds, unsigned_seconds, yylex, parse_datetime_body): * lib/poll.c (poll): * lib/pselect.c (pselect): * lib/pthread-cond.c (endlessly, pthread_cond_timedwait): * lib/pthread-rwlock.c (pthread_rwlock_timedrdlock) (pthread_rwlock_timedwrlock): * lib/pthread_mutex_timedlock.c (pthread_mutex_timedlock): * lib/select.c (rpl_select): * lib/settime.c (settime): * lib/stat-time.h (get_stat_atime, get_stat_ctime) (get_stat_mtime, get_stat_birthtime): * lib/thrd.c (rpl_thrd_current): * lib/timespec.h (make_timespec): * lib/timespec_getres.c (timespec_getres): * lib/utimecmp.c (utimecmpat): * lib/utimens.c (fdutimens): When filling in a struct timespec or similar time-related structure that might be copied elsewhere, also assign to any storage other than tv_sec and tv_nsec, to avoid undefined behavior on (likely theoretical) platforms where struct timespec has other members, and also to avoid warnings from GCC and/or valgrind.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/* pselect - synchronous I/O multiplexing
Copyright 2011-2023 Free Software Foundation, Inc.
This file is part of gnulib.
This file is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This file is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. */
/* written by Paul Eggert */
#include <config.h>
#include <sys/select.h>
#include <errno.h>
#include <signal.h>
/* Examine the size-NFDS file descriptor sets in RFDS, WFDS, and XFDS
to see whether some of their descriptors are ready for reading,
ready for writing, or have exceptions pending. Wait for at most
TIMEOUT seconds, and use signal mask SIGMASK while waiting. A null
pointer parameter stands for no descriptors, an infinite timeout,
or an unaffected signal mask. */
#if !HAVE_PSELECT
int
pselect (int nfds, fd_set *restrict rfds,
fd_set *restrict wfds, fd_set *restrict xfds,
struct timespec const *restrict timeout,
sigset_t const *restrict sigmask)
{
int select_result;
sigset_t origmask;
struct timeval tv, *tvp;
if (nfds < 0 || nfds > FD_SETSIZE)
{
errno = EINVAL;
return -1;
}
if (timeout)
{
if (! (0 <= timeout->tv_nsec && timeout->tv_nsec < 1000000000))
{
errno = EINVAL;
return -1;
}
tv = (struct timeval) {
.tv_sec = timeout->tv_sec,
.tv_usec = (timeout->tv_nsec + 999) / 1000
};
tvp = &tv;
}
else
tvp = NULL;
/* Signal mask munging should be atomic, but this is the best we can
do in this emulation. */
if (sigmask)
pthread_sigmask (SIG_SETMASK, sigmask, &origmask);
select_result = select (nfds, rfds, wfds, xfds, tvp);
if (sigmask)
{
int select_errno = errno;
pthread_sigmask (SIG_SETMASK, &origmask, NULL);
errno = select_errno;
}
return select_result;
}
#else /* HAVE_PSELECT */
# include <unistd.h>
# undef pselect
int
rpl_pselect (int nfds, fd_set *restrict rfds,
fd_set *restrict wfds, fd_set *restrict xfds,
struct timespec const *restrict timeout,
sigset_t const *restrict sigmask)
{
int i;
/* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */
if (nfds < 0 || nfds > FD_SETSIZE)
{
errno = EINVAL;
return -1;
}
for (i = 0; i < nfds; i++)
{
if (((rfds && FD_ISSET (i, rfds))
|| (wfds && FD_ISSET (i, wfds))
|| (xfds && FD_ISSET (i, xfds)))
&& dup2 (i, i) != i)
return -1;
}
return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
}
#endif