Re: [Libguestfs] [PATCH nbdkit v2 2/6] error: Use new nbdkit_parse_probability

2023-05-17 Thread Eric Blake
On Wed, May 17, 2023 at 11:06:55AM +0100, Richard W.M. Jones wrote:
> This replaces the existing ad hoc parsing with the new public function.
> ---
>  filters/error/nbdkit-error-filter.pod | 12 +++-
>  filters/error/error.c | 14 +-
>  2 files changed, 8 insertions(+), 18 deletions(-)
> 

> +  if (nbdkit_parse_probability (key, value, ) == -1)
>  return -1;
> -  }
>if (d < 0 || d > 1) {

If you change nbdkit_parse_probability() based on my comments to patch
1 to guarantee a non-negative value in d on success, then then left
half of this conditional would now be dead code.

Otherwise,

Reviewed-by: Eric Blake 

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.   +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
___
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs



[Libguestfs] [PATCH nbdkit v2 2/6] error: Use new nbdkit_parse_probability

2023-05-17 Thread Richard W.M. Jones
This replaces the existing ad hoc parsing with the new public function.
---
 filters/error/nbdkit-error-filter.pod | 12 +++-
 filters/error/error.c | 14 +-
 2 files changed, 8 insertions(+), 18 deletions(-)

diff --git a/filters/error/nbdkit-error-filter.pod 
b/filters/error/nbdkit-error-filter.pod
index bedd97924..49f21d10a 100644
--- a/filters/error/nbdkit-error-filter.pod
+++ b/filters/error/nbdkit-error-filter.pod
@@ -6,7 +6,7 @@ nbdkit-error-filter - inject errors for testing clients
 
  nbdkit --filter=error PLUGIN
  [error=EPERM|EIO|ENOMEM|EINVAL|ENOSPC|ESHUTDOWN]
- [error-rate=10%|0.1]
+ [error-rate=10%|0.1|1:10]
  [error-file=/tmp/inject]
  [error-pread=...] [error-pread-rate=...] [error-pread-file=...]
  [error-pwrite=...] [error-pwrite-rate=...] [error-pwrite-file=...]
@@ -66,11 +66,13 @@ This parameter is optional and the default is C
 
 =item B0..1
 
+=item BNB<:>M
+
 The rate of injected errors per NBD request.  This can be expressed as
-either a percentage between C<0%> and C<100%> or as a probability
-between C<0> and C<1>.  If C<0%> or C<0> is used then no errors are
-ever injected, and if C<100%> or C<1> is used then all requests return
-errors.
+a percentage between C<0%> and C<100%>, or as a probability between
+C<0> and C<1>, or as a ratio like C<1:10>.  If C<0%> or C<0> is used
+then no errors are ever injected, and if C<100%> or C<1> is used then
+all requests return errors.
 
 This parameter is optional and the default is C<0%>.
 B
diff --git a/filters/error/error.c b/filters/error/error.c
index ac4da8c16..08e19e3bd 100644
--- a/filters/error/error.c
+++ b/filters/error/error.c
@@ -130,21 +130,9 @@ static int
 parse_error_rate (const char *key, const char *value, double *retp)
 {
   double d;
-  int n;
 
-  if (sscanf (value, "%lg%n", , ) == 1) {
-if (strcmp ([n], "%") == 0) /* percentage */
-  d /= 100.0;
-else if (strcmp ([n], "") == 0) /* probability */
-  ;
-else
-  goto bad_parse;
-  }
-  else {
-  bad_parse:
-nbdkit_error ("%s: could not parse rate '%s'", key, value);
+  if (nbdkit_parse_probability (key, value, ) == -1)
 return -1;
-  }
   if (d < 0 || d > 1) {
 nbdkit_error ("%s: rate out of range: '%s' parsed as %g", key, value, d);
 return -1;
-- 
2.39.2

___
Libguestfs mailing list
Libguestfs@redhat.com
https://listman.redhat.com/mailman/listinfo/libguestfs