Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This compliant solution uses difftime() to determine the difference between two time_t values. The difftime() function returns the number of seconds from the second parameter until the first parameter and returns the result as a double.

Code Block
bgColor#ccccff
int do_work(int seconds_to_work) {
  time_t start = time(NULL);
  time_t current = start;

  if (start == (time_t)(-1)) {
    /* Handle error */
  }
  while (difftime(current, start) < seconds_to_work) {
    current = time(NULL);
    if (current == (time_t)(-1)) {
       /* Handle error */
    }
    /* ... */
  }
  return 0;
}

...

Using time_t incorrectly can lead to broken logic that can place a program in an infinite loop or cause an expected logic branch to not actually execute.

Recommendation

Severity

Likelihood

Remediation Cost

Priority

Level

MSC05-C

low

unlikely

medium

P2

L3

...