Skip to contentSkip to conversation

lib/scatterlist: error handling in __sg_alloc_table()

PATCH New Dan Carpenter 1 participant 1 message Jun 20, 2013 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/scatterlist: error handling in __sg_alloc_table()

I was reviewing code which I suspected might allocate a zero size SG table. That will cause memory corruption. Also we can't return before doing the memset or we could end up using uninitialized memory in the cleanup path.

lib/scatterlist.c +4 -2
index a1cf8ca..d39178b 100644
@@ -247,13 +247,15 @@ int __sg_alloc_table(struct sg_table *table, unsigned int nents,
 	struct scatterlist *sg, *prv;
 	unsigned int left;
 
+	memset(table, 0, sizeof(*table));
+
+	if (nents == 0)
+		return -EINVAL;
 #ifndef ARCH_HAS_SG_CHAIN
 	if (WARN_ON_ONCE(nents > max_ents))
 		return -EINVAL;
 #endif
 
-	memset(table, 0, sizeof(*table));
-
 	left = nents;
 	prv = NULL;
 	do {