diff options
author | Paul Eggert <eggert@cs.ucla.edu> | 2023-05-14 23:27:51 -0700 |
---|---|---|
committer | Paul Eggert <eggert@cs.ucla.edu> | 2023-05-14 23:30:16 -0700 |
commit | 88811b7d0413ef4b48566e52eca4f91c37be3a3f (patch) | |
tree | cb1a2b721e81b7b5b5dfc8171c444b0057dfb7c9 /lib/parse-datetime.y | |
parent | 29eff208358df3d6bb1668649f008894a312b336 (diff) | |
download | gnulib-88811b7d0413ef4b48566e52eca4f91c37be3a3f.tar.gz |
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.
Diffstat (limited to 'lib/parse-datetime.y')
-rw-r--r-- | lib/parse-datetime.y | 17 |
1 files changed, 7 insertions, 10 deletions
diff --git a/lib/parse-datetime.y b/lib/parse-datetime.y index 45c5e43415..a0487a3713 100644 --- a/lib/parse-datetime.y +++ b/lib/parse-datetime.y @@ -281,8 +281,7 @@ digits_to_date_time (parser_control *pc, textint text_int) pc->hour = text_int.value / 100; pc->minutes = text_int.value % 100; } - pc->seconds.tv_sec = 0; - pc->seconds.tv_nsec = 0; + pc->seconds = (struct timespec) {0}; pc->meridian = MER24; } } @@ -320,8 +319,7 @@ set_hhmmss (parser_control *pc, intmax_t hour, intmax_t minutes, { pc->hour = hour; pc->minutes = minutes; - pc->seconds.tv_sec = sec; - pc->seconds.tv_nsec = nsec; + pc->seconds = (struct timespec) { .tv_sec = sec, .tv_nsec = nsec }; } /* Return a textual representation of the day ordinal/number values @@ -966,14 +964,14 @@ signed_seconds: tSDECIMAL_NUMBER | tSNUMBER { if (time_overflow ($1.value)) YYABORT; - $$.tv_sec = $1.value; $$.tv_nsec = 0; } + $$ = (struct timespec) { .tv_sec = $1.value }; } ; unsigned_seconds: tUDECIMAL_NUMBER | tUNUMBER { if (time_overflow ($1.value)) YYABORT; - $$.tv_sec = $1.value; $$.tv_nsec = 0; } + $$ = (struct timespec) { .tv_sec = $1.value }; } ; number: @@ -1480,8 +1478,8 @@ yylex (union YYSTYPE *lvalp, parser_control *pc) ns = BILLION - ns; } - lvalp->timespec.tv_sec = s; - lvalp->timespec.tv_nsec = ns; + lvalp->timespec = (struct timespec) { .tv_sec = s, + .tv_nsec = ns }; pc->input = p; return sign ? tSDECIMAL_NUMBER : tUDECIMAL_NUMBER; } @@ -1812,8 +1810,7 @@ parse_datetime_body (struct timespec *result, char const *p, pc.day = tmp.tm_mday; pc.hour = tmp.tm_hour; pc.minutes = tmp.tm_min; - pc.seconds.tv_sec = tmp.tm_sec; - pc.seconds.tv_nsec = Start_ns; + pc.seconds = (struct timespec) { .tv_sec = tmp.tm_sec, .tv_nsec = Start_ns }; tm.tm_isdst = tmp.tm_isdst; pc.meridian = MER24; |