[KPasswdServer] replace foreach with range/index-based for
ClosedPublic

Authored by ahmadsamir on Mar 10 2020, 9:46 AM.

Details

Diff Detail

Repository
R241 KIO
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
ahmadsamir requested review of this revision.Mar 10 2020, 9:46 AM
ahmadsamir created this revision.
meven accepted this revision.Mar 10 2020, 10:31 AM
This revision is now accepted and ready to land.Mar 10 2020, 10:31 AM

Please wait for a second review, I am not authoritative here.

Please wait for a second review, I am not authoritative here.

OK, thanks.

apol added a subscriber: apol.Mar 10 2020, 3:26 PM

Looks good to me, I wonder why you turned some to for+iterators.

meven added a comment.Mar 10 2020, 3:41 PM
In D27965#625381, @apol wrote:

Looks good to me, I wonder why you turned some to for+iterators.

Those modified the list as they iterated through the list : authList->removeOne(current);

apol added a comment.Mar 10 2020, 9:20 PM

Having the iterated value change under the hood will eventually break. I'd suggest preferring QList::erase to QList::removeOne.

src/kpasswdserver/kpasswdserver.cpp
648

Use erase.

680–681

Use erase.

In D27965#625526, @apol wrote:

Having the iterated value change under the hood will eventually break. I'd suggest preferring QList::erase to QList::removeOne.

Actually, the code also uses 'delete current', now if there's another instance of that AuthInfoContainer* in authList, on a next iteration accessing the pointer "current" would be invalid as the object it had pointed to has been already delete'ed... I think removeAll() would be a better fit; WDYT?

In D27965#625526, @apol wrote:

Having the iterated value change under the hood will eventually break. I'd suggest preferring QList::erase to QList::removeOne.

Actually, the code also uses 'delete current', now if there's another instance of that AuthInfoContainer* in authList, on a next iteration accessing the pointer "current" would be invalid as the object it had pointed to has been already delete'ed... I think removeAll() would be a better fit; WDYT?

authList does not contain duplicates. Otherwise the current code would already have the problem you're mentioning.
So I see no reason to protect against that case.
erase sounds much safer than removeOne which has unclear consequences on the iterators.

dfaure requested changes to this revision.Mar 11 2020, 9:48 PM
This revision now requires changes to proceed.Mar 11 2020, 9:48 PM
ahmadsamir updated this revision to Diff 77477.Mar 12 2020, 4:39 AM
  • I missed one foreach before
  • Use erase() instead of remove(), safer to work on QList iterators
dfaure requested changes to this revision.Mar 12 2020, 8:37 PM
dfaure added inline comments.
src/kpasswdserver/kpasswdserver.cpp
201

I wonder why the whole method isn't const

648

This is not the way to use erase(). It invalidates the iterator...

The proper way is

it = authList->erase(it);

and doing ++it at the end of the for loop, not in the 3rd part of the for() line, since we don't want ++it after it=erase(it).

680–681

same problem here.
add "} else { ++it; }"

716

(this one is fine, there's a "break;" below)

This revision now requires changes to proceed.Mar 12 2020, 8:37 PM

QList::erase() invalidates the iterators

dfaure accepted this revision.Mar 15 2020, 9:28 AM
This revision is now accepted and ready to land.Mar 15 2020, 9:28 AM
This revision was automatically updated to reflect the committed changes.