watchdog: avoid usage of iterator after loop
PATCH v2 1/1 watchdog: avoid usage of iterator after loop
If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the kfree() should be within the loop body.
Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the macro to avoid any potential misuse after the loop [1].
---
Changes in v2:
- avoid returning within the list iterator
- Link to v1: https://lore.kernel.org/r/20230301-watchdog-avoid-iter-after-loop-v1-1-851f583be9f7@gmail.com
--- drivers/watchdog/watchdog_pretimeout.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/watchdog/watchdog_pretimeout.c +1 -2
@@ -207,10 +207,9 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)list_for_each_entry_safe (p, t, &pretimeout_list, entry) {if (p->wdd == wdd) {list_del (&p->entry); +kfree (p);break ; } }spin_unlock_irq (&pretimeout_lock); - -kfree (p); }
---
base-commit: c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-watchdog-avoid-iter-after-loop-a197bf201435
Best regards,
Signature · 1 line
Jakob Koschel <[email protected]>
Quoted 11 lines · Jakob Koschel
If potentially no valid element is found, 'p' would contain an invalid pointer past the iterator loop. To ensure 'p' is valid under any circumstances, the
kfree()should be within the loop body.Additionally, Linus proposed to avoid any use of the list iterator variable after the loop, in the attempt to move the list iterator variable declaration into the macro to avoid any potential misuse after the loop [1].
Quoted 30 lines
---
Changes in v2:
- avoid returning within the list iterator
- Link to v1: https://lore.kernel.org/r/20230301-watchdog-avoid-iter-after-loop-v1-1-851f583be9f7@gmail.com--- drivers/watchdog/watchdog_pretimeout.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)drivers/watchdog/watchdog_pretimeout.c +1
-3@@ -207,10 +207,9 @@ void watchdog_unregister_pretimeout(struct watchdog_device *wdd)list_for_each_entry_safe (p, t, &pretimeout_list, entry) {if (p->wdd == wdd) {list_del (&p->entry); +kfree (p);break ; } }spin_unlock_irq (&pretimeout_lock); - -kfree (p); } ---base-commit:
c0927a7a5391f7d8e593e5e50ead7505a23cadf9
change-id: 20230301-watchdog-avoid-iter-after-loop-a197bf201435Best regards,