Coverage Report

Created: 2026-01-17 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wpantund/src/util/time-utils.c
Line
Count
Source
1
/*
2
 *
3
 * Copyright (c) 2016 Nest Labs, Inc.
4
 * All rights reserved.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 *
18
 */
19
20
#if HAVE_CONFIG_H
21
#include <config.h>
22
#endif
23
24
#include "time-utils.h"
25
#include <sys/time.h>
26
#include <stdio.h>
27
28
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
29
static uint64_t sFuzzCms = 0;
30
31
void
32
15.1k
fuzz_set_cms(cms_t value) {
33
#if DEBUG
34
  fprintf(stderr, "fuzz_set_cms: %dms\n", (int)value);
35
#endif
36
15.1k
  sFuzzCms = value;
37
15.1k
}
38
39
void
40
89.7k
fuzz_ff_cms(cms_t increment) {
41
#if DEBUG
42
  fprintf(stderr, "fuzz_ff_cms: fast forward %dms\n", (int)increment);
43
#endif
44
89.7k
  if (increment <= CMS_DISTANT_FUTURE) {
45
89.7k
    sFuzzCms += increment;
46
89.7k
  }
47
89.7k
}
48
49
cms_t
50
time_ms(void)
51
12.1M
{
52
12.1M
  return (cms_t)sFuzzCms;
53
12.1M
}
54
55
time_t
56
time_get_monotonic(void)
57
0
{
58
0
  return (time_t)(sFuzzCms/MSEC_PER_SEC);
59
0
}
60
61
#else // if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
62
63
cms_t
64
time_ms(void)
65
{
66
#if HAVE_CLOCK_GETTIME
67
  struct timespec tv = { 0 };
68
  int ret;
69
70
  ret = clock_gettime(CLOCK_MONOTONIC, &tv);
71
72
  return (cms_t)(tv.tv_sec * MSEC_PER_SEC) + (cms_t)(tv.tv_nsec / NSEC_PER_MSEC);
73
#else
74
  struct timeval tv = { 0 };
75
  gettimeofday(&tv, NULL);
76
  return (cms_t)(tv.tv_sec * MSEC_PER_SEC) + (cms_t)(tv.tv_usec / USEC_PER_MSEC);
77
#endif
78
}
79
80
time_t
81
time_get_monotonic(void)
82
{
83
#if HAVE_CLOCK_GETTIME
84
  struct timespec ts;
85
  int ret;
86
87
  ret = clock_gettime(CLOCK_MONOTONIC, &ts);
88
89
  return ret == 0 ? ts.tv_sec : 0;
90
#else
91
  return time(NULL);
92
#endif // !__linux__
93
}
94
#endif // else FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
95
96
cms_t
97
cms_until_time(time_t time)
98
0
{
99
0
  time -= time_get_monotonic();
100
101
0
  if (time > (TIME_DISTANT_FUTURE / MSEC_PER_SEC)) {
102
    // Overflow.
103
0
    return CMS_DISTANT_FUTURE;
104
0
  }
105
106
0
  return (cms_t)(time * MSEC_PER_SEC);
107
0
}