[PATCH] list: add missing empty list check to list_cut_before()

Ziran Zhang posted 1 patch 1 week, 1 day ago
There is a newer version of this series
include/linux/list.h | 5 +++++
1 file changed, 5 insertions(+)
[PATCH] list: add missing empty list check to list_cut_before()
Posted by Ziran Zhang 1 week, 1 day ago
list_cut_before() lacks the list_empty() guard present
in list_cut_position().

With an empty head and an entry not on the list, it
corrupts the list.

Add the missing check.  When head is empty, initialize
@list as empty, consistent with the existing
head->next == entry case.

Signed-off-by: Ziran Zhang <zhangcoder@yeah.net>
---
 include/linux/list.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/include/linux/list.h b/include/linux/list.h
index 77fb62f79928..4d2061d35beb 100644
--- a/include/linux/list.h
+++ b/include/linux/list.h
@@ -555,6 +555,11 @@ static inline void list_cut_before(struct list_head *list,
 				   struct list_head *head,
 				   struct list_head *entry)
 {
+	if (list_empty(head)) {
+		INIT_LIST_HEAD(list);
+		return;
+	}
+
 	if (head->next == entry) {
 		INIT_LIST_HEAD(list);
 		return;
-- 
2.51.0
Re: [PATCH] list: add missing empty list check to list_cut_before()
Posted by Andy Shevchenko 1 week, 1 day ago
On Wed, Sep 16, 2026 at 06:00:01PM +0800, Ziran Zhang wrote:
> list_cut_before() lacks the list_empty() guard present
> in list_cut_position().
> 
> With an empty head and an entry not on the list, it
> corrupts the list.

Is it IRL case or the code analysis? If the former, add necessary information
like hardware description (if applicable) and the important ~3-5 lines from
traceback (I assume you rung with CONFIG_LIST_DEBUG=y).

> Add the missing check.  When head is empty, initialize
> @list as empty, consistent with the existing
> head->next == entry case.

Nice! Where is the test case?

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] list: add missing empty list check to list_cut_before()
Posted by Ziran Zhang 1 week, 1 day ago
On Wed, 16 Sep 2026 13:53:56 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Is it IRL case or the code analysis? If the former, add necessary information
> like hardware description (if applicable) and the important ~3-5 lines from
> traceback (I assume you rung with CONFIG_LIST_DEBUG=y).

It is code analysis, not an IRL case.  The motivation is consistency
with list_cut_position(), which already has the same guard.

> Nice! Where is the test case?

There is no test case.  If a test is required for this change, I can
try to add one.  I wanted to first check whether the consistency
argument alone is acceptable.

Thanks,
Ziran Zhang
Re: [PATCH] list: add missing empty list check to list_cut_before()
Posted by Andy Shevchenko 1 week, 1 day ago
On Wed, Sep 16, 2026 at 07:11:12PM +0800, Ziran Zhang wrote:
> On Wed, 16 Sep 2026 13:53:56 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > Is it IRL case or the code analysis? If the former, add necessary information
> > like hardware description (if applicable) and the important ~3-5 lines from
> > traceback (I assume you rung with CONFIG_LIST_DEBUG=y).
> 
> It is code analysis, not an IRL case.  The motivation is consistency
> with list_cut_position(), which already has the same guard.
> 
> > Nice! Where is the test case?
> 
> There is no test case.  If a test is required for this change, I can
> try to add one.  I wanted to first check whether the consistency
> argument alone is acceptable.

Any new code to lib must be accompanied with a test case(s) as documented [1].
Look at lib/tests/, there is already a set of them.

[1] 0a83293322fd ("doc: development-process: add notice on testing")

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] list: add missing empty list check to list_cut_before()
Posted by Ziran Zhang 1 week, 1 day ago
On Wed, 16 Sep 2026 18:37:30 +0300, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> Any new code to lib must be accompanied with a test case(s) as documented [1].
> Look at lib/tests/, there is already a set of them.
> 
> [1] 0a83293322fd ("doc: development-process: add notice on testing")

Thanks for the suggestion! I will add a KUnit test case and send the v2 patches
in a few days.

Thanks,
Ziran Zhang