diff mbox series

[6/7] tests/unit/test-throttle: Avoid unintended integer division

Message ID 20240312183810.557768-7-peter.maydell@linaro.org
State Superseded
Headers show
Series various: 7 minor Coverity fixes | expand

Commit Message

Peter Maydell March 12, 2024, 6:38 p.m. UTC
In test_compute_wait() we do
 double units = bkt.max / 10;
which does an integer division and then assigns it to a double variable,
and similarly later on in the expression for an assertion.

Use 10.0 so that we do a floating point division and calculate the
exact value, rather than doing an integer division.

Spotted by Coverity.

Resolves: Coverity CID 1432564
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
 tests/unit/test-throttle.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Thomas Huth March 12, 2024, 7:22 p.m. UTC | #1
On 12/03/2024 19.38, Peter Maydell wrote:
> In test_compute_wait() we do
>   double units = bkt.max / 10;
> which does an integer division and then assigns it to a double variable,
> and similarly later on in the expression for an assertion.
> 
> Use 10.0 so that we do a floating point division and calculate the
> exact value, rather than doing an integer division.
> 
> Spotted by Coverity.
> 
> Resolves: Coverity CID 1432564
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
>   tests/unit/test-throttle.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/tests/unit/test-throttle.c b/tests/unit/test-throttle.c
> index 2146cfacd36..24032a02667 100644
> --- a/tests/unit/test-throttle.c
> +++ b/tests/unit/test-throttle.c
> @@ -127,13 +127,13 @@ static void test_compute_wait(void)
>       bkt.avg = 10;
>       bkt.max = 200;
>       for (i = 0; i < 22; i++) {
> -        double units = bkt.max / 10;
> +        double units = bkt.max / 10.0;
>           bkt.level += units;
>           bkt.burst_level += units;
>           throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 10);
>           wait = throttle_compute_wait(&bkt);
>           g_assert(double_cmp(bkt.burst_level, 0));
> -        g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10));
> +        g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10.0));
>           /* We can do bursts for the 2 seconds we have configured in
>            * burst_length. We have 100 extra milliseconds of burst
>            * because bkt.level has been leaking during this time.

Reviewed-by: Thomas Huth <thuth@redhat.com>
diff mbox series

Patch

diff --git a/tests/unit/test-throttle.c b/tests/unit/test-throttle.c
index 2146cfacd36..24032a02667 100644
--- a/tests/unit/test-throttle.c
+++ b/tests/unit/test-throttle.c
@@ -127,13 +127,13 @@  static void test_compute_wait(void)
     bkt.avg = 10;
     bkt.max = 200;
     for (i = 0; i < 22; i++) {
-        double units = bkt.max / 10;
+        double units = bkt.max / 10.0;
         bkt.level += units;
         bkt.burst_level += units;
         throttle_leak_bucket(&bkt, NANOSECONDS_PER_SECOND / 10);
         wait = throttle_compute_wait(&bkt);
         g_assert(double_cmp(bkt.burst_level, 0));
-        g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10));
+        g_assert(double_cmp(bkt.level, (i + 1) * (bkt.max - bkt.avg) / 10.0));
         /* We can do bursts for the 2 seconds we have configured in
          * burst_length. We have 100 extra milliseconds of burst
          * because bkt.level has been leaking during this time.