The example ([Senario2] below) in the commit message was incorrect.
Correctly, UAF will happen in the [Senario1] below.
Let me clarify those senarios.
When the entries to be removed (A) are consecutive, the second A is not
checked, leading to UAF.
[Senario1]
(A, A, B) with count=3
i=0:
(A, A, B) -> (A, B) with count=2
^ checked
i=1:
(A, B) -> (A, B) with count=2
^ checked (B, not A!)
i=2: (doesn't occur because i < count is false)
===> A remains with count=2 although A was freed, so UAF will happen.
When the entries to be removed (A) are not consecutive, all A entries are
removed luckily.
[Senario2]
(A, B, A) with count=3
i=0:
(A, B, A) -> (B, A) with count=2
^ checked
i=1:
(B, A) -> (B) with count=1
^ checked (A, not B)
i=2: (doesn't occur because i < count is false)
===> No A remains. No UAF in this case.
Although, even in the senario2, the fundamental issue remains
because B is never checked.
The fix addresses issues by preventing unintended skips.
Please let me know if I'm overlooking something or my understanding is
incorrect.
Thanks!