Coverage Report

Created: 2025-10-04 18:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/libfido2/src/time.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2021 Yubico AB. All rights reserved.
3
 * Use of this source code is governed by a BSD-style
4
 * license that can be found in the LICENSE file.
5
 * SPDX-License-Identifier: BSD-2-Clause
6
 */
7
8
#include <errno.h>
9
#include "fido.h"
10
11
static int
12
timespec_to_ms(const struct timespec *ts)
13
2.18M
{
14
2.18M
        int64_t x, y;
15
16
2.18M
        if (ts->tv_sec < 0 || ts->tv_nsec < 0 ||
17
2.18M
            ts->tv_nsec >= 1000000000LL)
18
2.71k
                return -1;
19
20
2.18M
        if ((uint64_t)ts->tv_sec >= INT64_MAX / 1000LL)
21
0
                return -1;
22
23
2.18M
        x = ts->tv_sec * 1000LL;
24
2.18M
        y = ts->tv_nsec / 1000000LL;
25
26
2.18M
        if (INT64_MAX - x < y || x + y > INT_MAX)
27
0
                return -1;
28
29
2.18M
        return (int)(x + y);
30
2.18M
}
31
32
int
33
fido_time_now(struct timespec *ts_now)
34
2.47M
{
35
2.47M
        if (clock_gettime(CLOCK_MONOTONIC, ts_now) != 0) {
36
5.18k
                fido_log_error(errno, "%s: clock_gettime", __func__);
37
5.18k
                return -1;
38
5.18k
        }
39
40
2.46M
        return 0;
41
2.47M
}
42
43
int
44
fido_time_delta(const struct timespec *ts_start, int *ms_remain)
45
2.25M
{
46
2.25M
        struct timespec ts_end, ts_delta;
47
2.25M
        int ms;
48
49
2.25M
        if (*ms_remain < 0)
50
63.1k
                return 0;
51
52
2.18M
        if (clock_gettime(CLOCK_MONOTONIC, &ts_end) != 0) {
53
2.84k
                fido_log_error(errno, "%s: clock_gettime", __func__);
54
2.84k
                return -1;
55
2.84k
        }
56
57
2.18M
        if (timespeccmp(&ts_end, ts_start, <)) {
58
2.05k
                fido_log_debug("%s: timespeccmp", __func__);
59
2.05k
                return -1;
60
2.05k
        }
61
62
2.18M
        timespecsub(&ts_end, ts_start, &ts_delta);
63
64
2.18M
        if ((ms = timespec_to_ms(&ts_delta)) < 0) {
65
2.71k
                fido_log_debug("%s: timespec_to_ms", __func__);
66
2.71k
                return -1;
67
2.71k
        }
68
69
2.18M
        if (ms > *ms_remain)
70
151k
                ms = *ms_remain;
71
72
2.18M
        *ms_remain -= ms;
73
74
2.18M
        return 0;
75
2.18M
}