Skip to content

Commit 36a3f80

Browse files
committed
- Added positive path for clock_settime test
- Fixed error in clock_settime struct setup - Upd the time.rst doc with up to date info
1 parent bd265d2 commit 36a3f80

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

libc/docs/headers/time.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Implementation Status
6767
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
6868
| clock_getres | | | | | | | | | | | | | |
6969
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
70-
| clock_gettime | |check| | |check| | | |check| | | | | | | | | | |
70+
| clock_gettime | |check| | |check| | | |check| | | | | | | | | |check| | |check| |
7171
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+
7272
| clock_nanosleep | | | | | | | | | | | | | |
7373
+---------------------+---------+---------+---------+-----------------+---------+---------+---------+---------+---------+---------+---------+---------+---------+

libc/src/__support/time/linux/clock_settime.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) {
3434

3535
__kernel_timespec ts64{};
3636

37+
// Populate the 64-bit kernel structure from the user-provided timespec
38+
ts64.tv_sec = static_cast<decltype(ts64.tv_sec)>(ts->tv_sec);
39+
ts64.tv_nsec = static_cast<decltype(ts64.tv_nsec)>(ts->tv_nsec);
40+
3741
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64,
3842
static_cast<long>(clockid),
3943
reinterpret_cast<long>(&ts64));
40-
if (ret == 0) {
41-
ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec);
42-
ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(ts64.tv_nsec);
43-
}
4444
#else
4545
#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available."
4646
#endif

libc/test/src/time/clock_settime_test.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,26 @@ TEST_F(LlvmLibcClockSetTime, InvalidClockId) {
2929
ASSERT_EQ(result, -1);
3030
ASSERT_ERRNO_EQ(EINVAL);
3131
}
32+
33+
TEST_F(LlvmLibcClockSetTime, InvalidTimespecNsec) {
34+
timespec ts = {0, 1000000000L};
35+
int result = LIBC_NAMESPACE::clock_settime(CLOCK_REALTIME, &ts);
36+
ASSERT_EQ(result, -1);
37+
ASSERT_ERRNO_EQ(EINVAL);
38+
}
39+
40+
TEST_F(LlvmLibcClockSetTime, NullPointerIsEFAULT) {
41+
int result = LIBC_NAMESPACE::clock_settime(CLOCK_REALTIME, nullptr);
42+
ASSERT_EQ(result, -1);
43+
ASSERT_ERRNO_EQ(EFAULT);
44+
}
45+
46+
TEST_F(LlvmLibcClockSetTime, ClockIsSet) {
47+
timespec ts = {0, 0};
48+
int result = LIBC_NAMESPACE::clock_settime(CLOCK_REALTIME, &ts);
49+
if (result == 0) {
50+
ASSERT_ERRNO_SUCCESS();
51+
} else {
52+
ASSERT_ERRNO_EQ(EPERM);
53+
}
54+
}

0 commit comments

Comments
 (0)