diff mbox series

[10/13] block/vhdx: make range overlap check more readable

Message ID 20240722040742.11513-11-yaoxt.fnst@fujitsu.com (mailing list archive)
State New, archived
Headers show
Series make range overlap check more readable | expand

Commit Message

Xingtao Yao (Fujitsu) July 22, 2024, 4:07 a.m. UTC
use range_overlaps_range() instead of open-coding the overlap check to improve
the readability of the code.

Signed-off-by: Yao Xingtao <yaoxt.fnst@fujitsu.com>
---
 block/vhdx.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/block/vhdx.c b/block/vhdx.c
index 5aa1a1350626..c31661b946b6 100644
--- a/block/vhdx.c
+++ b/block/vhdx.c
@@ -32,6 +32,7 @@ 
 #include "qapi/qmp/qdict.h"
 #include "qapi/qobject-input-visitor.h"
 #include "qapi/qapi-visit-block-core.h"
+#include "qemu/range.h"
 
 /* Options for VHDX creation */
 
@@ -231,15 +232,16 @@  void vhdx_guid_generate(MSGUID *guid)
 static int vhdx_region_check(BDRVVHDXState *s, uint64_t start, uint64_t length)
 {
     int ret = 0;
-    uint64_t end;
     VHDXRegionEntry *r;
+    Range range1, range2;
 
-    end = start + length;
+    range_init_nofail(&range1, start, length);
     QLIST_FOREACH(r, &s->regions, entries) {
-        if (!((start >= r->end) || (end <= r->start))) {
+        range_init_nofail(&range2, r->start, r->end - r->start);
+        if (range_overlaps_range(&range1, &range2)) {
             error_report("VHDX region %" PRIu64 "-%" PRIu64 " overlaps with "
-                         "region %" PRIu64 "-%." PRIu64, start, end, r->start,
-                         r->end);
+                         "region %" PRIu64 "-%." PRIu64, start, start + length,
+                         r->start, r->end);
             ret = -EINVAL;
             goto exit;
         }