[PATCH] shared, tools: introduce and use array iterator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Introduce the convenient kernel-style array iterator and use it where
appropriate. Note this also fixes the following warnings reported by UBSan:

shared/array.c:67:23: runtime error: applying zero offset to null pointer
tools/depmod.c:1983:25: runtime error: applying zero offset to null pointer

Signed-off-by: Dmitry Antipov <dmantipov@xxxxxxxxx>
---
 shared/array.c | 11 ++++++-----
 shared/array.h |  5 +++++
 tools/depmod.c | 24 +++++++++++++-----------
 3 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/shared/array.c b/shared/array.c
index c2e2e14..bc314c6 100644
--- a/shared/array.c
+++ b/shared/array.c
@@ -63,11 +63,12 @@ int array_append(struct array *array, const void *element)
 
 int array_append_unique(struct array *array, const void *element)
 {
-	void **itr = array->array;
-	void **itr_end = itr + array->count;
-	for (; itr < itr_end; itr++)
-		if (*itr == element)
-			return -EEXIST;
+	if (array->array) {
+		void **itr, **end;
+		array_for_each(array, itr, end)
+			if (*itr == element)
+				return -EEXIST;
+	}
 	return array_append(array, element);
 }
 
diff --git a/shared/array.h b/shared/array.h
index b88482f..7172a66 100644
--- a/shared/array.h
+++ b/shared/array.h
@@ -20,3 +20,8 @@ void array_pop(struct array *array);
 void array_free_array(struct array *array);
 void array_sort(struct array *array, int (*cmp)(const void *a, const void *b));
 int array_remove_at(struct array *array, unsigned int pos);
+
+/* Convenient kernel-style array iterator */
+#define array_for_each(_array, _itr, _end) \
+	for (_itr = _array->array, _end = _itr + _array->count; \
+	     _itr < _end; _itr++)
diff --git a/tools/depmod.c b/tools/depmod.c
index a2c39b3..ade33ae 100644
--- a/tools/depmod.c
+++ b/tools/depmod.c
@@ -1970,7 +1970,6 @@ static int depmod_calculate_dependencies(struct depmod *depmod)
 
 	/* topological sort (outputs modules without users first) */
 	while (n_roots > 0) {
-		const struct mod **itr_dst, **itr_dst_end;
 		struct mod *src;
 		uint16_t src_idx = roots[--n_roots];
 
@@ -1979,16 +1978,19 @@ static int depmod_calculate_dependencies(struct depmod *depmod)
 		sorted[n_sorted] = src_idx;
 		n_sorted++;
 
-		itr_dst = (const struct mod **)src->deps.array;
-		itr_dst_end = itr_dst + src->deps.count;
-		for (; itr_dst < itr_dst_end; itr_dst++) {
-			const struct mod *dst = *itr_dst;
-			uint16_t dst_idx = dst->idx;
-			assert(users[dst_idx] > 0);
-			users[dst_idx]--;
-			if (users[dst_idx] == 0) {
-				roots[n_roots] = dst_idx;
-				n_roots++;
+		if (src->deps.array) {
+			void **itr, **end;
+			struct array *base = &src->deps;
+
+			array_for_each(base, itr, end) {
+				const struct mod *dst = *itr;
+				uint16_t dst_idx = dst->idx;
+				assert(users[dst_idx] > 0);
+				users[dst_idx]--;
+				if (users[dst_idx] == 0) {
+					roots[n_roots] = dst_idx;
+					n_roots++;
+				}
 			}
 		}
 	}
-- 
2.40.1




[Index of Archives]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux