[PATCH 1/9] maple_tree: Fix allocation when min is equal to max in mas_empty_area/_area_rev()

From: Peng Zhang
Date: Tue Apr 25 2023 - 07:06:09 EST


Make the allocation valid when min is equal to max in mas_empty_area()
and mas_empty_area_rev(). As Liam R. Howlett said, VMA doesn't make this
allocation, so now this bug won't trigger.

Also add some checks for invalid parameters.

Fixes: 54a611b60590 ("Maple Tree: add new data structure")
Signed-off-by: Peng Zhang <zhangpeng.00@xxxxxxxxxxxxx>
---
lib/maple_tree.c | 12 +++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/lib/maple_tree.c b/lib/maple_tree.c
index 110a36479dced..72099b4b32169 100644
--- a/lib/maple_tree.c
+++ b/lib/maple_tree.c
@@ -5289,7 +5289,10 @@ int mas_empty_area(struct ma_state *mas, unsigned long min,
unsigned long *pivots;
enum maple_type mt;

- if (min >= max)
+ if (unlikely(min > max))
+ return -EINVAL;
+
+ if (unlikely(size == 0) || unlikely(max - min < size - 1))
return -EINVAL;

if (mas_is_start(mas))
@@ -5344,7 +5347,10 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
{
struct maple_enode *last = mas->node;

- if (min >= max)
+ if (unlikely(min > max))
+ return -EINVAL;
+
+ if (unlikely(size == 0) || unlikely(max - min < size - 1))
return -EINVAL;

if (mas_is_start(mas)) {
@@ -5380,7 +5386,7 @@ int mas_empty_area_rev(struct ma_state *mas, unsigned long min,
return -EBUSY;

/* Trim the upper limit to the max. */
- if (max <= mas->last)
+ if (max < mas->last)
mas->last = max;

mas->index = mas->last - size + 1;
--
2.20.1