The type, precision, and range of clock_t are implementation defined. time_t is specified as an "arithmetic type capable of representing times" as size_t, which is "an unsigned integer type", but the unsigned result of the sizeof operator. However, how time is encoding encoded within the arithmetic type is unspecified.
Computing Time Passed
Non-Compliant Code Example
...
| Code Block | ||
|---|---|---|
| ||
int do_work(int seconds_to_work) {
time_t start;
start = time();
if (start == (time_t)(-1)) {
/* Handle error */
}
while (time() < start + second_to_work) {
do_some_work();
}
}
|
Compliant
...
Solution
The This compliant code example solution uses difftime() to determine the difference between two time_t values. difftime() returns the number of seconds from the second parameter until the first parameter and returns the result as a double.
...
Note that this loop may still not exit, as the range of time_t may not be able to represent two times seconds_to_work apart.
Compute time in the future or past
This section is incomplete
Risk Assessment
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
|---|---|---|---|---|---|
MSC05-A | 1 (low) | 1 (low) | 2 (medium) | P4 P2 | L2 L3 |
References
| Wiki Markup |
|---|
\[[Kettlewell 02|AA. C References#Kettlewell 02]\] Section 4.1, "time_t" |
...