Invalid iterator in RelationTableView.cxx? (dbaccess module)

Lionel Elie Mamane lionel at mamane.lu
Mon Feb 9 14:19:37 PST 2015


On Mon, Feb 09, 2015 at 02:22:04PM -0700, julien2412 [via Document Foundation Mail Archive] wrote:

> On RelationTableView.cxx file, I noticed this:
>      99     for(;aIter != rTabWinDataList.rend();++aIter)
>     100     {
>     101         TTableWindowData::value_type pData = *aIter;
>     102         OTableWindow* pTabWin = createWindow(pData);
>     103 
>     104         if (!pTabWin->Init())
>     105         {
> ... 
>     112             rTabWinDataList.erase(
> ::std::remove(rTabWinDataList.begin(), rTabWinDataList.end(), *aIter),
> rTabWinDataList.end());
>     113             continue;
>     114         }
> 
> idem block 139-143
> 
> See
> http://opengrok.libreoffice.org/xref/core/dbaccess/source/ui/relationdesign/RelationTableView.cxx#99
> 
> Shouldn't the "continue" be replaced by "break" to avoid invalid iterator or
> "aIter" isn't invalid for "for loops" at all even after "erase"
> lines?

Note that aIter is a *reverse* iterator, that goes backwards.

After erase(A, B), the iterators pointing to positions A or after are
invalid, but iterators pointing to positions BEFORE A are valid.

The std::remove will move elements inside rTabWinDataList, moving all
elements that are equal to *aIter to the end. The erase then actually
removes these elements (the ones equal to aIter). It seems to me that:

1) the return value of std::remove is guaranteed to be at aIter or
   after.

2) thus the erase can only invalidate positions at and after aIter.

3) then we do ++aIter (moving it LEFT, backwards), which thus then
   guaranteed to be a valid position (or rend, that is one-before-the
   first position).

4) However, this algorithm means that (unless rTabWinDataList is
   sorted or something like that), the for loop might traverse the
   same *elements* SEVERAL TIMES, since they are moved around, it may
   encounter the same element several times at different positions.

5) It will, however, not miss any element equivalence class,
   traversing each element OR AN ELEMENT EQUAL TO IT at least once.

-- 
Lionel




--
View this message in context: http://nabble.documentfoundation.org/Invalid-iterator-in-RelationTableView-cxx-dbaccess-module-tp4139541p4139547.html
Sent from the Dev mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/libreoffice/attachments/20150209/bf0f9372/attachment.html>


More information about the LibreOffice mailing list