fix segfault in lutimes when tv argument is NULL
authorÉrico Rolim <ericonr@disroot.org>
Thu, 12 Nov 2020 20:52:23 +0000 (17:52 -0300)
committerRich Felker <dalias@aerifal.cx>
Sun, 29 Nov 2020 05:57:24 +0000 (00:57 -0500)
calling lutimes with tv=0 is valid if the application wants to set the
timestamps to the current time. this commit makes it so the timespec
struct is populated with values from tv only if tv != 0 and calls
utimensat with times=0 if tv == 0.

src/legacy/lutimes.c

index 2e5502d..dd46592 100644 (file)
@@ -6,9 +6,11 @@
 int lutimes(const char *filename, const struct timeval tv[2])
 {
        struct timespec times[2];
-       times[0].tv_sec  = tv[0].tv_sec;
-       times[0].tv_nsec = tv[0].tv_usec * 1000;
-       times[1].tv_sec  = tv[1].tv_sec;
-       times[1].tv_nsec = tv[1].tv_usec * 1000;
-       return utimensat(AT_FDCWD, filename, times, AT_SYMLINK_NOFOLLOW);
+       if (tv) {
+               times[0].tv_sec  = tv[0].tv_sec;
+               times[0].tv_nsec = tv[0].tv_usec * 1000;
+               times[1].tv_sec  = tv[1].tv_sec;
+               times[1].tv_nsec = tv[1].tv_usec * 1000;
+       }
+       return utimensat(AT_FDCWD, filename, tv ? times : 0, AT_SYMLINK_NOFOLLOW);
 }