Math error in libbsd timespecsub

Bug #2033113 reported by Reggie McMurtrey
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
libbsd (Ubuntu)
New
Undecided
Unassigned

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.

Revision history for this message
Thorsten Glaser (mirabilos) wrote :

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.

Revision history for this message
Reggie McMurtrey (reggie-mcmurtrey) wrote : Re: [Bug 2033113] Re: Math error in libbsd timespecsub

Ok, I see what you are doing and why, but it may be worth putting a blurb
in the timespecsub man page about how you are handling negative numbers.
I'm fighting another bug in a system where we aren't meeting our deadline
(why we are gettiing negative sleep #'s). we added some debug code to see
what our sleep values were and the values didn't make sense.

On Fri, Aug 25, 2023 at 5:30 PM Thorsten Glaser <email address hidden>
wrote:

> 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 subscribed to the bug
> report.
> 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
>
>

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.