]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Add a check when comparing iterators. 5325/head
authorWolfgang Bangerth <bangerth@colostate.edu>
Wed, 25 Oct 2017 00:05:10 +0000 (18:05 -0600)
committerWolfgang Bangerth <bangerth@colostate.edu>
Wed, 25 Oct 2017 14:53:56 +0000 (08:53 -0600)
Right now, comparing iterators only compares their level and index, but surprisingly
not the triangulation pointers. There is an assertion higher up that makes sure
that if the triangulation pointers of both objects are both non-nullptr, that they
are the same, but it is conceivable that one of the objects in invalid and the
other has a valid pointer. Internally, if that is the case, we have the invariant
that for iterators for which the triangulation pointer is nullptr, that the
level and index values are also invalid, and so nothing bad can happen (TM) with
the current code.

But it still seems prudent to also compare triangulation pointers, even if we know
that if one only of the pointers is nullptr, then the other comparisons for equality
must necessarily also fail because of the invariant -- we should just err on the
side of safety.

I've thought whether the pointers should be compared first or last. I put it first
because I think that the common case is to compare 'cell != endc', and in that
case one has a valid pointer and the other does not, so the first check will
already fail.

include/deal.II/grid/tria_accessor.templates.h

index 48f187b00cb51c485e7258edae6bbd014ddc86e6..da38181ba7801f7f571ead255f864d3de9c9bd3e 100644 (file)
@@ -120,7 +120,8 @@ TriaAccessorBase<structdim,dim,spacedim>::operator == (const TriaAccessorBase<st
 {
   Assert (tria == a.tria || tria == nullptr || a.tria == nullptr,
           TriaAccessorExceptions::ExcCantCompareIterators());
-  return ((present_level == a.present_level) &&
+  return ((tria == a.tria) &&
+          (present_level == a.present_level) &&
           (present_index == a.present_index));
 }
 
@@ -133,7 +134,8 @@ TriaAccessorBase<structdim,dim,spacedim>::operator != (const TriaAccessorBase<st
 {
   Assert (tria == a.tria || tria == nullptr || a.tria == nullptr,
           TriaAccessorExceptions::ExcCantCompareIterators());
-  return ((present_level != a.present_level) ||
+  return ((tria != a.tria) ||
+          (present_level != a.present_level) ||
           (present_index != a.present_index));
 }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.