Skip to contentSkip to conversation

lib/stackdepot.c: fix a condition in stack_depot_fetch()

PATCH In review Dan Carpenter 2 participants 2 messages Mar 12, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/stackdepot.c: fix a condition in stack_depot_fetch()

We should check for a NULL pointer first before adding the offset. Otherwise if the pointer is NULL and the offset is non-zero, it will lead to an Oops.

Fixes d45048e65a59 ("lib/stackdepot.c: check depot_index before accessing the stack slab")
---
 lib/stackdepot.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/stackdepot.c +3 -5
index da5d1880bf34..2caffc64e4c8 100644
@@ -207,18 +207,16 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,
 	size_t offset = parts.offset << STACK_ALLOC_ALIGN;
 	struct stack_record *stack;
 
+	*entries = NULL;
 	if (parts.slabindex > depot_index) {
 		WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
 			parts.slabindex, depot_index, handle);
-		*entries = NULL;
 		return 0;
 	}
 	slab = stack_slabs[parts.slabindex];
-	stack = slab + offset;
-	if (!stack) {
-		*entries = NULL;
+	if (!slab)
 		return 0;
-	}
+	stack = slab + offset;
 
 	*entries = stack->entries;
 	return stack->size;
Signature · 1 line
2.20.1
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Dan Carpenter

We should check for a NULL pointer first before adding the offset. Otherwise if the pointer is NULL and the offset is non-zero, it will lead to an Oops.

Thanks!

Quoted 2 lines
Fixes d45048e65a59 ("lib/stackdepot.c: check depot_index before accessing the stack slab")
Quoted 33 lines
---
 lib/stackdepot.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
lib/stackdepot.c +3 -6
index da5d1880bf34..2caffc64e4c8 100644
@@ -207,18 +207,16 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle,
        size_t offset = parts.offset << STACK_ALLOC_ALIGN;
        struct stack_record *stack;
 
+       *entries = NULL;
        if (parts.slabindex > depot_index) {
                WARN(1, "slab index %d out of bounds (%d) for stack id %08x\n",
                        parts.slabindex, depot_index, handle);
-               *entries = NULL;
                return 0;
        }
        slab = stack_slabs[parts.slabindex];
-       stack = slab + offset;
-       if (!stack) {
-               *entries = NULL;
+       if (!slab)
                return 0;
-       }
+       stack = slab + offset;
 
        *entries = stack->entries;
        return stack->size;
--

2.20.1

Signature · 10 lines
Alexander Potapenko
Software Engineer

Google Germany GmbH
Erika-Mann-Straße, 33
80636 München

Geschäftsführer: Paul Manicle, Halimah DeLaine Prado
Registergericht und -nummer: Hamburg, HRB 86891
Sitz der Gesellschaft: Hamburg