]> git.proxmox.com Git - wasi-libc.git/commitdiff
Fix utimensat to avoid passing uninitialized values into WASI calls.
authorDan Gohman <dev@sunfishcode.online>
Tue, 24 May 2022 20:56:33 +0000 (13:56 -0700)
committerDan Gohman <dev@sunfishcode.online>
Tue, 24 May 2022 22:17:44 +0000 (15:17 -0700)
Previously, utimensat would leave the mtim and/or atim timestamps
uninitialized when the `MTIM_NOW` or `ATIM_NOW` were in use, because
that means the respective timestamps are not used.

However, clang now automatically adds `noundef` to the arguments in
functions like `__wasi_path_filestat_set_times`, and there are cases
where simplifycfg can see paths where the uninitialized values are
passed to those `noundef` arguments.

To fix this, change the utimens code to zero out the timestamps when
they aren't in use, to avoid passing uninitialized arguments.

libc-bottom-half/cloudlibc/src/libc/sys/stat/stat_impl.h

index c649d3b6d956cf1ea78d0a5f9a4777b37aa334bb..9726b5165685b7342c8e972c9e1f3dbc30682992 100644 (file)
@@ -75,14 +75,18 @@ static inline bool utimens_get_timestamps(const struct timespec *times,
   if (times == NULL) {
     // Update both timestamps.
     *flags = __WASI_FSTFLAGS_ATIM_NOW | __WASI_FSTFLAGS_MTIM_NOW;
+    *st_atim = (__wasi_timestamp_t) { 0 };
+    *st_mtim = (__wasi_timestamp_t) { 0 };
   } else {
     // Set individual timestamps.
     *flags = 0;
     switch (times[0].tv_nsec) {
       case UTIME_NOW:
         *flags |= __WASI_FSTFLAGS_ATIM_NOW;
+        *st_atim = (__wasi_timestamp_t) { 0 };
         break;
       case UTIME_OMIT:
+        *st_atim = (__wasi_timestamp_t) { 0 };
         break;
       default:
         *flags |= __WASI_FSTFLAGS_ATIM;
@@ -94,8 +98,10 @@ static inline bool utimens_get_timestamps(const struct timespec *times,
     switch (times[1].tv_nsec) {
       case UTIME_NOW:
         *flags |= __WASI_FSTFLAGS_MTIM_NOW;
+        *st_mtim = (__wasi_timestamp_t) { 0 };
         break;
       case UTIME_OMIT:
+        *st_mtim = (__wasi_timestamp_t) { 0 };
         break;
       default:
         *flags |= __WASI_FSTFLAGS_MTIM;