Skip to contentSkip to conversation

dm-switch: Remove unnecessary condition in conditional

PATCH In review Tomohiro Kusumi 2 participants 2 messages Oct 28, 2015 – Oct 29, 2015 Device mapper

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 dm-switch: Remove unnecessary condition in conditional

Quoted 7 lines

2015-10-28 8:03 GMT+09:00 Mikulas Patocka:

Quoted 3 lines · Tomohiro Kusumi

It's always (sctx->nr_regions == nr_regions) considering the assignment right before this conditional, and this function is only used by .ctr.

Quoted 6 lines

The variable sctx->nr_regions has type unsigned long and the variable nr_regions has type sector_t.

Thus the variables may be different when overflow happens.

It could be changed to "if (nr_regions >= ULONG_MAX)".

Sorry about that.
Changed the conditional to "if (nr_regions >= ULONG_MAX)", and brought the assignment of nr_regions after sector_div() and the sanity check which looks more sane.

---
 drivers/md/dm-switch.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/md/dm-switch.c +2 -2
index 50fca46..e1b6722 100644
@@ -99,11 +99,11 @@ static int alloc_region_table(struct dm_target *ti, unsigned nr_paths)
 	if (sector_div(nr_regions, sctx->region_size))
 		nr_regions++;
 
-	sctx->nr_regions = nr_regions;
-	if (sctx->nr_regions != nr_regions || sctx->nr_regions >= ULONG_MAX) {
+	if (nr_regions >= ULONG_MAX) {
 		ti->error = "Region table too large";
 		return -EINVAL;
 	}
+	sctx->nr_regions = nr_regions;
 
 	nr_slots = nr_regions;
 	if (sector_div(nr_slots, sctx->region_entries_per_slot))
Signature · 1 line
1.7.1
maintainer Acked-by on v2 1/1
#1 lore raw
Quoted 14 lines · Tomohiro Kusumi
Quoted 7 lines

2015-10-28 8:03 GMT+09:00 Mikulas Patocka:

Quoted 3 lines · Tomohiro Kusumi

It's always (sctx->nr_regions == nr_regions) considering the assignment right before this conditional, and this function is only used by .ctr.

Quoted 6 lines

The variable sctx->nr_regions has type unsigned long and the variable nr_regions has type sector_t.

Thus the variables may be different when overflow happens.

It could be changed to "if (nr_regions >= ULONG_MAX)".

Quoted 7 lines

Sorry about that.
Changed the conditional to "if (nr_regions >= ULONG_MAX)", and brought the assignment of nr_regions after sector_div() and the sanity check which looks more sane.

Quoted 25 lines
---
 drivers/md/dm-switch.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
drivers/md/dm-switch.c +2 -2
index 50fca46..e1b6722 100644
@@ -99,11 +99,11 @@ static int alloc_region_table(struct dm_target *ti, unsigned nr_paths)
 	if (sector_div(nr_regions, sctx->region_size))
 		nr_regions++;
 
-	sctx->nr_regions = nr_regions;
-	if (sctx->nr_regions != nr_regions || sctx->nr_regions >= ULONG_MAX) {
+	if (nr_regions >= ULONG_MAX) {
 		ti->error = "Region table too large";
 		return -EINVAL;
 	}
+	sctx->nr_regions = nr_regions;
 
 	nr_slots = nr_regions;
 	if (sector_div(nr_slots, sctx->region_entries_per_slot))
Signature · 1 line
1.7.1