Linux SPARSE checker discussions
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: linux-sparse@vger.kernel.org
Cc: Bart Van Assche <bvanassche@acm.org>
Subject: [PATCH 02/20] ptrlist: Guard against null list in ptrlist operations
Date: Mon, 19 Oct 2026 10:56:15 -0700	[thread overview]
Message-ID: <20261019175633.415493-3-bvanassche@acm.org> (raw)
In-Reply-To: <20261019175633.415493-1-bvanassche@acm.org>

Guard against null list pointers before accessing list->rm in ptrlist.h
and ptrlist.c to eliminate undefined behavior and UBSan null member
access traps.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 ptrlist.c | 47 +++++++++++++++++++++++++++++++++--------------
 ptrlist.h | 14 ++++++++++----
 2 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/ptrlist.c b/ptrlist.c
index c5766002e7ef..29fea6a35c70 100644
--- a/ptrlist.c
+++ b/ptrlist.c
@@ -165,19 +165,21 @@ void *ptr_list_nth_entry(struct ptr_list *list, unsigned int idx)
 int linearize_ptr_list(struct ptr_list *head, void **arr, int max)
 {
 	int nr = 0;
-	if (head && max > 0) {
+	if (head && max >= 0) {
 		struct ptr_list *list = head;
 
 		do {
-			int i = list->nr;
-			nr += i;
-			if (max == 0)
-				continue;
-			if (i > max) 
-				i = max;
-			memcpy(arr, list->list, i*sizeof(void *));
-			arr += i;
-			max -= i;
+			int i;
+			for (i = 0; i < list->nr; i++) {
+				void *ptr = list->list[i];
+				if (list->rm && !ptr)
+					continue;
+				nr++;
+				if (max > 0) {
+					*arr++ = ptr;
+					max--;
+				}
+			}
 		} while ((list = list->next) != head);
 	}
 	return nr;
@@ -247,6 +249,15 @@ void split_ptr_list_head(struct ptr_list *head)
 	newlist->nr = nr;
 	memcpy(newlist->list, head->list + old, nr * sizeof(void *));
 	memset(head->list + old, 0xf0, nr * sizeof(void *));
+	if (head->rm) {
+		int i, rm = 0;
+		for (i = 0; i < nr; i++) {
+			if (!newlist->list[i])
+				rm++;
+		}
+		newlist->rm = rm;
+		head->rm -= rm;
+	}
 }
 
 ///
@@ -409,10 +420,18 @@ void * delete_ptr_list_last(struct ptr_list **head)
 
 	if (!first)
 		return NULL;
-	last = first->prev;
-	if (last->nr)
-		ptr = last->list[--last->nr];
-	if (last->nr <=0) {
+	while ((last = first->prev)->nr == 0) {
+		if (last == first) {
+			*head = NULL;
+			__free_ptrlist(last);
+			return NULL;
+		}
+		first->prev = last->prev;
+		last->prev->next = first;
+		__free_ptrlist(last);
+	}
+	ptr = last->list[--last->nr];
+	if (last->nr <= 0) {
 		first->prev = last->prev;
 		last->prev->next = first;
 		if (last == first)
diff --git a/ptrlist.h b/ptrlist.h
index 5a3dcbeb97ae..89f15d53fa13 100644
--- a/ptrlist.h
+++ b/ptrlist.h
@@ -11,7 +11,7 @@
  */
 
 /* Silly type-safety check ;) */
-#define CHECK_TYPE(head,ptr)		(void)(&(ptr) == &(head)->list[0])
+#define CHECK_TYPE(head,ptr)		(void)sizeof(&(ptr) == &(head)->list[0])
 #define PTRLIST_TYPE(head)		__typeof__((head)->list[0])
 #define VRFY_PTR_LIST(head)		(void)(sizeof((head)->list[0]))
 
@@ -163,15 +163,19 @@ extern void __free_ptr_list(struct ptr_list **);
 
 
 #define PTR_NEXT(ptr, __head, __list, __nr, PTR_ENTRY)			\
-	do {								\
+	for (;;) {							\
 		if (__nr < __list->nr) {				\
 			ptr = PTR_ENTRY(__list,__nr);			\
 			__nr++;						\
+			if (__list->rm && !ptr)				\
+				continue;				\
 			break;						\
 		}							\
 		ptr = NULL;						\
 		__nr = 0;						\
-	} while ((__list = __list->next) != __head)			\
+		if ((__list = __list->next) == __head)			\
+			break;						\
+	}			\
 
 #define DO_PREPARE(head, ptr, __head, __list, __nr, PTR_ENTRY)		\
 	do {								\
@@ -224,7 +228,7 @@ extern void __free_ptr_list(struct ptr_list **);
 	__typeof__(head) __list = __head;				\
 	__typeof__(head) __name = __head;				\
 	int __nr;							\
-	if (!head)							\
+	if (!__head)							\
 		break;							\
 	do {								\
 		__list = __list->prev;					\
@@ -255,6 +259,8 @@ extern void __free_ptr_list(struct ptr_list **);
 	__inside##new:							\
 		while (--__newnr >= 0) {				\
 			new = PTR_ENTRY(__newlist,__newnr);		\
+			if (__newlist->rm && !new)			\
+				continue;		\
 
 #define DO_THIS_ADDRESS(ptr, __head, __list, __nr)			\
 	(&__list->list[__nr])

  parent reply	other threads:[~2026-10-19 17:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-19 17:56 [PATCH 00/20] Sanitizer fixes, LLVM updates, and CI workflow Bart Van Assche
2026-10-19 17:56 ` [PATCH 01/20] doc: Update maintainer and mirror information Bart Van Assche
2026-10-19 17:56 ` Bart Van Assche [this message]
2026-10-19 17:56 ` [PATCH 03/20] bits: Guard against out-of-bounds bit shifts in bitmask helpers Bart Van Assche
2026-10-19 17:56 ` [PATCH 04/20] storage: Free hash table entries when freeing storage Bart Van Assche
2026-10-19 17:56 ` [PATCH 05/20] builtin: Bound argument array iteration in declare_builtin() Bart Van Assche
2026-10-19 17:56 ` [PATCH 06/20] c2xml: Use an unsigned shift in examine_modifiers() Bart Van Assche
2026-10-19 17:56 ` [PATCH 07/20] parse: Avoid incrementing rank on invalid long real specifiers Bart Van Assche
2026-10-19 17:56 ` [PATCH 08/20] pre-process: Avoid zero-length VLAs for parameterless macros Bart Van Assche
2026-10-19 17:56 ` [PATCH 09/20] pre-process: Avoid reading token->string on non-string tokens Bart Van Assche
2026-10-19 17:56 ` [PATCH 10/20] target: Avoid signed overflow in bit and byte conversion helpers Bart Van Assche
2026-10-19 17:56 ` [PATCH 11/20] evaluate: Use bitmask helpers in evaluate_ptr_add() Bart Van Assche
2026-10-19 17:56 ` [PATCH 12/20] linearize: Use bits_mask() in linearize_bitfield_insert() Bart Van Assche
2026-10-19 17:56 ` [PATCH 13/20] expand: Avoid undefined shifts and signed overflow in constant folding Bart Van Assche
2026-10-19 17:56 ` [PATCH 14/20] simplify: Avoid undefined shifts and signed overflow in simplification Bart Van Assche
2026-10-19 17:56 ` [PATCH 15/20] sparsec: Emit object files directly with llc -filetype=obj Bart Van Assche
2026-10-19 17:56 ` [PATCH 16/20] llvm: Stop calling LLVMConstGEP2() for string literals Bart Van Assche
2026-10-19 17:56 ` [PATCH 17/20] llvm: Free leaked allocations and LLVM objects Bart Van Assche
2026-10-19 17:56 ` [PATCH 18/20] parse: Accept an empty attribute argument list Bart Van Assche
2026-10-19 17:56 ` [PATCH 19/20] llvm: Use explicit LLVMContextRef instead of global context Bart Van Assche
2026-10-19 17:56 ` [PATCH 20/20] ci: Add a GitHub Actions workflow and CI scripts Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261019175633.415493-3-bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=linux-sparse@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox