Skip to contentSkip to conversation

list: add missing empty list check to list_cut_before()

PATCH Superseded Ziran Zhang 2 participants 5 messages Sep 16

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 list: add missing empty list check to list_cut_before()

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.

---
 include/linux/list.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/list.h +5 -0
index 77fb62f79928..4d2061d35beb 100644
@@ -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;
Signature · 1 line
2.51.0
#1 lore raw
Quoted 5 lines · Ziran Zhang

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).

Quoted 3 lines

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?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 3 lines · Andy Shevchenko

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.

Quoted 1 line

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

#3 lore raw
Quoted 5 lines · Ziran Zhang
Quoted 3 lines · Andy Shevchenko

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).

Quoted 8 lines

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

Quoted 1 line

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")

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 4 lines · Andy Shevchenko

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