[PATCH] block/file-posix: use unsigned int for zones consistently

Stefan Hajnoczi posted 1 patch 1 year ago
Failed in applying to current master (apply log)
block/file-posix.c | 12 +++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)
[PATCH] block/file-posix: use unsigned int for zones consistently
Posted by Stefan Hajnoczi 1 year ago
Avoid mixing int and unsigned int for zone index and count values. This
eliminates the possibility of accidental negative write pointer array
indices. It also makes code review easier because we don't need to worry
about signed/unsigned comparisons.

In practice I don't think zoned devices are likely to exceed MAX_INT
zones any time soon, so this is mostly a code cleanup.

Cc: Sam Li <faithilikerun@gmail.com>
Cc: Dmitry Fomichev <dmitry.fomichev@wdc.com>
Cc: Damien Le Moal <damien.lemoal@opensource.wdc.com>
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 block/file-posix.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

This is a cleanup on top of "[PATCH v9 0/4] Add zone append write for
zoned device".

Based-on: <20230407081657.17947-1-faithilikerun@gmail.com>

diff --git a/block/file-posix.c b/block/file-posix.c
index 32b16bc4fb..77fbf9e33e 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -1338,8 +1338,9 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
     size_t rep_size;
     uint64_t sector = offset >> BDRV_SECTOR_BITS;
     BlockZoneWps *wps = bs->wps;
-    int j = offset / bs->bl.zone_size;
-    int ret, n = 0, i = 0;
+    unsigned int j = offset / bs->bl.zone_size;
+    int ret;
+    unsigned int n = 0, i = 0;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
     g_autofree struct blk_zone_report *rep = NULL;
 
@@ -2092,7 +2093,8 @@ static int handle_aiocb_zone_report(void *opaque)
     struct blk_zone *blkz;
     size_t rep_size;
     unsigned int nrz;
-    int ret, n = 0, i = 0;
+    int ret;
+    unsigned int n = 0, i = 0;
 
     nrz = *nr_zones;
     rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
@@ -3507,11 +3509,11 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
             return ret;
         }
     } else if (zo == BLKRESETZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             wp[j] = offset + j * zone_size;
         }
     } else if (zo == BLKFINISHZONE) {
-        for (int j = 0; j < nrz; ++j) {
+        for (unsigned int j = 0; j < nrz; ++j) {
             /* The zoned device allows the last zone smaller that the
              * zone size. */
             wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
-- 
2.39.2
Re: [PATCH] block/file-posix: use unsigned int for zones consistently
Posted by Sam Li 1 year ago
Stefan Hajnoczi <stefanha@redhat.com> 于2023年4月10日周一 21:49写道:
>
> Avoid mixing int and unsigned int for zone index and count values. This
> eliminates the possibility of accidental negative write pointer array
> indices. It also makes code review easier because we don't need to worry
> about signed/unsigned comparisons.
>
> In practice I don't think zoned devices are likely to exceed MAX_INT
> zones any time soon, so this is mostly a code cleanup.
>
> Cc: Sam Li <faithilikerun@gmail.com>
> Cc: Dmitry Fomichev <dmitry.fomichev@wdc.com>
> Cc: Damien Le Moal <damien.lemoal@opensource.wdc.com>
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>  block/file-posix.c | 12 +++++++-----
>  1 file changed, 7 insertions(+), 5 deletions(-)
>
> This is a cleanup on top of "[PATCH v9 0/4] Add zone append write for
> zoned device".
>
> Based-on: <20230407081657.17947-1-faithilikerun@gmail.com>

Reviewed-by: Sam Li <faithilikerun@gmail.com>

>
> diff --git a/block/file-posix.c b/block/file-posix.c
> index 32b16bc4fb..77fbf9e33e 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -1338,8 +1338,9 @@ static int get_zones_wp(BlockDriverState *bs, int fd, int64_t offset,
>      size_t rep_size;
>      uint64_t sector = offset >> BDRV_SECTOR_BITS;
>      BlockZoneWps *wps = bs->wps;
> -    int j = offset / bs->bl.zone_size;
> -    int ret, n = 0, i = 0;
> +    unsigned int j = offset / bs->bl.zone_size;
> +    int ret;
> +    unsigned int n = 0, i = 0;
>      rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
>      g_autofree struct blk_zone_report *rep = NULL;
>
> @@ -2092,7 +2093,8 @@ static int handle_aiocb_zone_report(void *opaque)
>      struct blk_zone *blkz;
>      size_t rep_size;
>      unsigned int nrz;
> -    int ret, n = 0, i = 0;
> +    int ret;
> +    unsigned int n = 0, i = 0;
>
>      nrz = *nr_zones;
>      rep_size = sizeof(struct blk_zone_report) + nrz * sizeof(struct blk_zone);
> @@ -3507,11 +3509,11 @@ static int coroutine_fn raw_co_zone_mgmt(BlockDriverState *bs, BlockZoneOp op,
>              return ret;
>          }
>      } else if (zo == BLKRESETZONE) {
> -        for (int j = 0; j < nrz; ++j) {
> +        for (unsigned int j = 0; j < nrz; ++j) {
>              wp[j] = offset + j * zone_size;
>          }
>      } else if (zo == BLKFINISHZONE) {
> -        for (int j = 0; j < nrz; ++j) {
> +        for (unsigned int j = 0; j < nrz; ++j) {
>              /* The zoned device allows the last zone smaller that the
>               * zone size. */
>              wp[j] = MIN(offset + (j + 1) * zone_size, offset + len);
> --
> 2.39.2
>