* [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait()
@ 2017-09-13 8:28 Alberto Garcia
2017-09-13 13:54 ` Eric Blake
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Alberto Garcia @ 2017-09-13 8:28 UTC (permalink / raw)
To: qemu-devel; +Cc: Alberto Garcia, qemu-block, Paolo Bonzini, Peter Maydell
If bkt->max == 0 and bkt->burst_length > 1 then we could have a
division by 0 in throttle_do_compute_wait(). That configuration is
however not permitted and is already detected by throttle_is_valid(),
but let's assert it in throttle_compute_wait() to make it explicit.
Found by Coverity (CID: 1381016).
Signed-off-by: Alberto Garcia <berto@igalia.com>
---
util/throttle.c | 1 +
1 file changed, 1 insertion(+)
diff --git a/util/throttle.c b/util/throttle.c
index 06bf916adc..b38e742da5 100644
--- a/util/throttle.c
+++ b/util/throttle.c
@@ -124,6 +124,7 @@ int64_t throttle_compute_wait(LeakyBucket *bkt)
/* If the main bucket is not full yet we still have to check the
* burst bucket in order to enforce the burst limit */
if (bkt->burst_length > 1) {
+ assert(bkt->max > 0); /* see throttle_is_valid() */
extra = bkt->burst_level - burst_bucket_size;
if (extra > 0) {
return throttle_do_compute_wait(bkt->max, extra);
--
2.11.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait()
2017-09-13 8:28 [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait() Alberto Garcia
@ 2017-09-13 13:54 ` Eric Blake
2017-09-13 16:31 ` Philippe Mathieu-Daudé
2017-09-18 9:58 ` [Qemu-devel] [Qemu-block] " Kevin Wolf
2 siblings, 0 replies; 5+ messages in thread
From: Eric Blake @ 2017-09-13 13:54 UTC (permalink / raw)
To: Alberto Garcia, qemu-devel; +Cc: Paolo Bonzini, qemu-block, Peter Maydell
[-- Attachment #1: Type: text/plain, Size: 1282 bytes --]
On 09/13/2017 03:28 AM, Alberto Garcia wrote:
> If bkt->max == 0 and bkt->burst_length > 1 then we could have a
> division by 0 in throttle_do_compute_wait(). That configuration is
> however not permitted and is already detected by throttle_is_valid(),
> but let's assert it in throttle_compute_wait() to make it explicit.
>
> Found by Coverity (CID: 1381016).
>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
> util/throttle.c | 1 +
> 1 file changed, 1 insertion(+)
Reviewed-by: Eric Blake <eblake@redhat.com>
>
> diff --git a/util/throttle.c b/util/throttle.c
> index 06bf916adc..b38e742da5 100644
> --- a/util/throttle.c
> +++ b/util/throttle.c
> @@ -124,6 +124,7 @@ int64_t throttle_compute_wait(LeakyBucket *bkt)
> /* If the main bucket is not full yet we still have to check the
> * burst bucket in order to enforce the burst limit */
> if (bkt->burst_length > 1) {
> + assert(bkt->max > 0); /* see throttle_is_valid() */
> extra = bkt->burst_level - burst_bucket_size;
> if (extra > 0) {
> return throttle_do_compute_wait(bkt->max, extra);
>
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 619 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait()
2017-09-13 8:28 [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait() Alberto Garcia
2017-09-13 13:54 ` Eric Blake
@ 2017-09-13 16:31 ` Philippe Mathieu-Daudé
2017-09-14 9:00 ` Alberto Garcia
2017-09-18 9:58 ` [Qemu-devel] [Qemu-block] " Kevin Wolf
2 siblings, 1 reply; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2017-09-13 16:31 UTC (permalink / raw)
To: Alberto Garcia, qemu-devel
Cc: Paolo Bonzini, qemu-block, Peter Maydell, Eric Blake
Hi Alberto,
On 09/13/2017 05:28 AM, Alberto Garcia wrote:
> If bkt->max == 0 and bkt->burst_length > 1 then we could have a
> division by 0 in throttle_do_compute_wait(). That configuration is
> however not permitted and is already detected by throttle_is_valid(),
> but let's assert it in throttle_compute_wait() to make it explicit.
This is correct but I'm not sure this is enough, as
throttle_compute_wait() is exported/public, however it seems testing is
the only reason to export it.
Also I spent 10min looking at it thinking about how bkt->max is used,
before to realize there should be a simpler way to write this (KISS).
Anyway I'm queuing your patch in my /static-analysis branch I plan to
this week where I use a self-explanatory macro instead of assert() and
will see if I can simplify it (note that I'm not a maintainer!).
Regards,
Phil.
>
> Found by Coverity (CID: 1381016).
>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
> util/throttle.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/util/throttle.c b/util/throttle.c
> index 06bf916adc..b38e742da5 100644
> --- a/util/throttle.c
> +++ b/util/throttle.c
> @@ -124,6 +124,7 @@ int64_t throttle_compute_wait(LeakyBucket *bkt)
> /* If the main bucket is not full yet we still have to check the
> * burst bucket in order to enforce the burst limit */
> if (bkt->burst_length > 1) {
> + assert(bkt->max > 0); /* see throttle_is_valid() */
> extra = bkt->burst_level - burst_bucket_size;
> if (extra > 0) {
> return throttle_do_compute_wait(bkt->max, extra);
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait()
2017-09-13 16:31 ` Philippe Mathieu-Daudé
@ 2017-09-14 9:00 ` Alberto Garcia
0 siblings, 0 replies; 5+ messages in thread
From: Alberto Garcia @ 2017-09-14 9:00 UTC (permalink / raw)
To: Philippe Mathieu-Daudé, qemu-devel
Cc: Paolo Bonzini, qemu-block, Peter Maydell, Eric Blake
On Wed 13 Sep 2017 06:31:58 PM CEST, Philippe Mathieu-Daudé wrote:
>> If bkt->max == 0 and bkt->burst_length > 1 then we could have a
>> division by 0 in throttle_do_compute_wait(). That configuration is
>> however not permitted and is already detected by throttle_is_valid(),
>> but let's assert it in throttle_compute_wait() to make it explicit.
>
> This is correct but I'm not sure this is enough, as
> throttle_compute_wait() is exported/public, however it seems testing
> is the only reason to export it.
You're right but in general I don't think the throttling code is
guaranteed to behave correctly if the configuration hasn't been checked
with throttle_is_valid() first.
> Also I spent 10min looking at it thinking about how bkt->max is used,
> before to realize there should be a simpler way to write this (KISS).
I'm sure there is :) if you have suggestions I'll be glad to hear them.
Berto
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Qemu-devel] [Qemu-block] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait()
2017-09-13 8:28 [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait() Alberto Garcia
2017-09-13 13:54 ` Eric Blake
2017-09-13 16:31 ` Philippe Mathieu-Daudé
@ 2017-09-18 9:58 ` Kevin Wolf
2 siblings, 0 replies; 5+ messages in thread
From: Kevin Wolf @ 2017-09-18 9:58 UTC (permalink / raw)
To: Alberto Garcia; +Cc: qemu-devel, Paolo Bonzini, qemu-block, Peter Maydell
Am 13.09.2017 um 10:28 hat Alberto Garcia geschrieben:
> If bkt->max == 0 and bkt->burst_length > 1 then we could have a
> division by 0 in throttle_do_compute_wait(). That configuration is
> however not permitted and is already detected by throttle_is_valid(),
> but let's assert it in throttle_compute_wait() to make it explicit.
>
> Found by Coverity (CID: 1381016).
>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
Thanks, applied to the block branch.
Kevin
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2017-09-18 9:59 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-09-13 8:28 [Qemu-devel] [PATCH] throttle: Assert that bkt->max is valid in throttle_compute_wait() Alberto Garcia
2017-09-13 13:54 ` Eric Blake
2017-09-13 16:31 ` Philippe Mathieu-Daudé
2017-09-14 9:00 ` Alberto Garcia
2017-09-18 9:58 ` [Qemu-devel] [Qemu-block] " Kevin Wolf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).