From 7bb7862b456022206bd18997579fd8e0a0e6912c Mon Sep 17 00:00:00 2001 From: HUA HSUAN Date: Thu, 28 Mar 2024 20:13:59 +0800 Subject: [PATCH 1/2] Fix the is_circular function Transform the is_circular function by shifting from a single pointer approach to employing slow and fast pointers. The single pointer method falls short in detecting loops that occur mid-list without additional storage. Utilizing slow and fast pointers overcomes this limitation, enabling the detection of mid-list loops. Although this approach introduces a slight increase in conditional checks, it remains a superior method for loop detection without the need for extra storage. --- qtest.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/qtest.c b/qtest.c index 44857cf30..d676a2ff5 100644 --- a/qtest.c +++ b/qtest.c @@ -880,17 +880,23 @@ static bool do_merge(int argc, char *argv[]) static bool is_circular() { struct list_head *cur = current->q->next; + struct list_head *fast = (cur) ? cur->next : NULL; while (cur != current->q) { - if (!cur) + if (!cur || !fast || !(fast->next)) + return false; + if (cur == fast) return false; cur = cur->next; + fast = fast->next->next; } cur = current->q->prev; + fast = (cur) ? cur->prev : NULL; while (cur != current->q) { - if (!cur) + if (!cur || !fast || !(fast->prev)) return false; cur = cur->prev; + fast = fast->prev->prev; } return true; } From bc0d83a94e250a0b6713c5172031267ecc78397f Mon Sep 17 00:00:00 2001 From: HUA HSUAN Date: Thu, 28 Mar 2024 22:23:40 +0800 Subject: [PATCH 2/2] Remove useless parentheses To follow the coding style, remove the usless parentheses. --- qtest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qtest.c b/qtest.c index d676a2ff5..15210a60c 100644 --- a/qtest.c +++ b/qtest.c @@ -882,7 +882,7 @@ static bool is_circular() struct list_head *cur = current->q->next; struct list_head *fast = (cur) ? cur->next : NULL; while (cur != current->q) { - if (!cur || !fast || !(fast->next)) + if (!cur || !fast || !fast->next) return false; if (cur == fast) return false; @@ -893,7 +893,7 @@ static bool is_circular() cur = current->q->prev; fast = (cur) ? cur->prev : NULL; while (cur != current->q) { - if (!cur || !fast || !(fast->prev)) + if (!cur || !fast || !fast->prev) return false; cur = cur->prev; fast = fast->prev->prev;