Eric Blake
2020-Oct-17 04:35 UTC
[Libguestfs] [libnbd PATCH] info: Keep request within 4G bound
Otherwise, we get a failure due to Numerical result out of range.
And for safety's sake, we are best capping our request to an aligned
value, if the server insists on minimum alignment.
Fixes: f3fd935c
---
info/nbdinfo.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/info/nbdinfo.c b/info/nbdinfo.c
index 1afdf98..2b22f51 100644
--- a/info/nbdinfo.c
+++ b/info/nbdinfo.c
@@ -31,6 +31,8 @@
#include <libnbd.h>
+#define MIN(a,b) ((a) < (b) ? (a) : (b))
+
static const char *progname;
static FILE *fp;
static bool list_all = false;
@@ -267,7 +269,7 @@ main (int argc, char *argv[])
fprintf (fp, "%" PRIi64 "\n", size);
}
else if (map) { /* --map (!list_all) */
- uint64_t offset, prev_offset;
+ uint64_t offset, prev_offset, align, max_len;
/* Did we get the requested map? */
if (!nbd_can_meta_context (nbd, map)) {
@@ -276,6 +278,8 @@ main (int argc, char *argv[])
progname, map);
exit (EXIT_FAILURE);
}
+ align = nbd_get_block_size (nbd, LIBNBD_SIZE_MINIMUM) ?: 512;
+ max_len = UINT32_MAX - align + 1;
size = nbd_get_size (nbd);
if (size == -1) {
@@ -286,7 +290,7 @@ main (int argc, char *argv[])
if (json_output) fprintf (fp, "[\n");
for (offset = 0; offset < size;) {
prev_offset = offset;
- if (nbd_block_status (nbd, size - offset, offset,
+ if (nbd_block_status (nbd, MIN (size - offset, max_len), offset,
(nbd_extent_callback) { .callback =
extent_callback,
.user_data = &offset },
0) == -1) {
--
2.29.0.rc1
Richard W.M. Jones
2020-Oct-17 10:35 UTC
Re: [Libguestfs] [libnbd PATCH] info: Keep request within 4G bound
On Fri, Oct 16, 2020 at 11:35:55PM -0500, Eric Blake wrote:> Otherwise, we get a failure due to Numerical result out of range. > > And for safety's sake, we are best capping our request to an aligned > value, if the server insists on minimum alignment. > > Fixes: f3fd935c > --- > info/nbdinfo.c | 8 ++++++-- > 1 file changed, 6 insertions(+), 2 deletions(-) > > diff --git a/info/nbdinfo.c b/info/nbdinfo.c > index 1afdf98..2b22f51 100644 > --- a/info/nbdinfo.c > +++ b/info/nbdinfo.c > @@ -31,6 +31,8 @@ > > #include <libnbd.h> > > +#define MIN(a,b) ((a) < (b) ? (a) : (b))I suppose we could import the min & max macros from nbdkit common/include/minmax.h. However that's a larger change.> static const char *progname; > static FILE *fp; > static bool list_all = false; > @@ -267,7 +269,7 @@ main (int argc, char *argv[]) > fprintf (fp, "%" PRIi64 "\n", size); > } > else if (map) { /* --map (!list_all) */ > - uint64_t offset, prev_offset; > + uint64_t offset, prev_offset, align, max_len; > > /* Did we get the requested map? */ > if (!nbd_can_meta_context (nbd, map)) { > @@ -276,6 +278,8 @@ main (int argc, char *argv[]) > progname, map); > exit (EXIT_FAILURE); > } > + align = nbd_get_block_size (nbd, LIBNBD_SIZE_MINIMUM) ?: 512; > + max_len = UINT32_MAX - align + 1; > > size = nbd_get_size (nbd); > if (size == -1) { > @@ -286,7 +290,7 @@ main (int argc, char *argv[]) > if (json_output) fprintf (fp, "[\n"); > for (offset = 0; offset < size;) { > prev_offset = offset; > - if (nbd_block_status (nbd, size - offset, offset, > + if (nbd_block_status (nbd, MIN (size - offset, max_len), offset, > (nbd_extent_callback) { .callback = extent_callback, > .user_data = &offset }, > 0) == -1) { > -- > 2.29.0.rc1ACK Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Seemingly Similar Threads
- [PATCH nbdinfo proposal] info: Add a --map option for displaying allocation metadata
- [PATCH nbdinfo v2] info: Add a --map option for displaying allocation metadata.
- [PATCH nbdinfo v2] info: Add a --map option for displaying allocation metadata.
- [PATCH libnbd 0/5] info: --map: Coalesce adjacent extents of the same type.
- [PATCH libnbd] info: Write output atomically.