From 88811b7d0413ef4b48566e52eca4f91c37be3a3f Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 14 May 2023 23:27:51 -0700 Subject: timespec: fill in other members MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- ChangeLog | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'ChangeLog') diff --git a/ChangeLog b/ChangeLog index 34e7d72130..3ed1b1ef8f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,44 @@ 2023-05-14 Paul Eggert + 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. + year2038-recommended: Improve failure diagnostic. * m4/largefile.m4 (AC_SYS_YEAR2038_RECOMMENDED): Sync from Autoconf. -- cgit v1.2.1