Math error in libbsd timespecsub
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
libbsd (Ubuntu) |
New
|
Undecided
|
Unassigned |
Bug Description
#define timespecsub(tsp, usp, vsp) \
do { \
if ((vsp)->tv_nsec < 0) { \
} \
} 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.
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.