[Bug 2033113] Re: Math error in libbsd timespecsub

Thorsten Glaser 2033113 at bugs.launchpad.net
Fri Aug 25 22:24:17 UTC 2023


No, the code is correct.

If you calculate (for the sake of simplicity I’m skipping the 0s) 0.1 -
0.2 you get -0.1 which in timespec is the same as -1 + .9.

If you’re trying to calculate how long to sleep for, you probably
shouldn’t sleep *at all* if your destination timestamp is already in the
past.

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to libbsd in Ubuntu.
https://bugs.launchpad.net/bugs/2033113

Title:
  Math error in libbsd timespecsub

Status in libbsd package in Ubuntu:
  New

Bug description:
  #define timespecsub(tsp, usp, vsp)                                      \
          do {                                                            \
                  (vsp)->tv_sec = (tsp)->tv_sec - (usp)->tv_sec;          \
                  (vsp)->tv_nsec = (tsp)->tv_nsec - (usp)->tv_nsec;       \
                  if ((vsp)->tv_nsec < 0) {                               \
                          (vsp)->tv_sec--;                                \
                          (vsp)->tv_nsec += 1000000000L;                  \
                  }                                                       \
          } while (0)
  #endif
  *******************************************
  the line 

  "if ((vsp)->tv_nsec < 0) {"

  should be

  "if ((vsp)->tv_nsec < 0 && (vsp)->tv_sec != 0) {"

  try tsp.tv_sec = 0, tsp.tv_nsec = 1, usp.tv_sec = 0, usp.tv_nsec = 2
  as a test case

  I found this trying to calculate how long to nanosleep for.

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/libbsd/+bug/2033113/+subscriptions




More information about the foundations-bugs mailing list