Skip to contentSkip to conversation

copperplate: heapmem: Handle size == 0 cleanly for heapobj_init_array_private

PATCH In review Jan Kiszka 2 participants 2 messages Oct 17, 2019

lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

PATCH 1/1 copperplate: heapmem: Handle size == 0 cleanly for heapobj_init_array_private

From: Jan Kiszka <[email protected]>

Reported by clang-9 scan-build: When size is 0, we will shift 1 beyond the size of 'size'. That's undefined behavior.

---
 lib/copperplate/heapobj-heapmem.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
lib/copperplate/heapobj-heapmem.c +13 -13
index bc6e5f236b..3e1a1d926a 100644
@@ -68,19 +68,19 @@ int __heapobj_init_private(struct heapobj *hobj, const char *name,
 int heapobj_init_array_private(struct heapobj *hobj, const char *name,
 			       size_t size, int elems)
 {
-	size_t log2 = sizeof(size) * CHAR_BIT - 1 -
-		xenomai_count_leading_zeros(size);
-
-	/*
-	 * Heapmem aligns individual object sizes on the next ^2
-	 * boundary, do likewise when determining the overall heap
-	 * size, so that we can allocate as many as @elems items.
-	 */
-	if (size & (size - 1))
-		log2++;
-
-	size = 1 << log2;
-
+	if (size != 0) {
+		size_t log2 = sizeof(size) * CHAR_BIT - 1 -
+			xenomai_count_leading_zeros(size);
+		/*
+		 * Heapmem aligns individual object sizes on the next ^2
+		 * boundary, do likewise when determining the overall heap
+		 * size, so that we can allocate as many as @elems items.
+		 */
+		if (size & (size - 1))
+			log2++;
+
+		size = 1 << log2;
+	}
 	return __bt(__heapobj_init_private(hobj, name,
 					   size * elems, NULL));
 }
Signature · 6 lines
2.16.4


-- 
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
#1 lore raw
Quoted 4 lines · Jan Kiszka via Xenomai

From: Jan Kiszka <[email protected]>

Reported by clang-9 scan-build: When size is 0, we will shift 1 beyond the size of 'size'. That's undefined behavior.

size == 0 is invalid call for this internal interface, any caller passing zero is broken. A simple assert() would suffice.

Signature · 1 line
Philippe.