[PATCH] [libc++] Const qualify __gnu_cxx::hash_map<>::const_iterator::pointer type.

Peter Collingbourne peter at pcc.me.uk
Thu Feb 27 11:58:41 PST 2014


On Thu, Feb 27, 2014 at 07:50:28AM -0800, Marshall Clow wrote:
> On Feb 14, 2014, at 7:31 PM, Peter Collingbourne <peter at pcc.me.uk> wrote:
> 
> > Hi mclow.lists,
> > 
> > http://llvm-reviews.chandlerc.com/D2811
> > 
> > Files:
> >  include/ext/hash_map
> > 
> > Index: include/ext/hash_map
> > ===================================================================
> > --- include/ext/hash_map
> > +++ include/ext/hash_map
> > @@ -430,9 +430,9 @@
> >     typedef const value_type&                                    reference;
> >     typedef typename __pointer_traits::template
> > #ifndef _LIBCPP_HAS_NO_TEMPLATE_ALIASES
> > -            rebind<value_type>
> > +            rebind<const value_type>
> > #else
> > -            rebind<value_type>::other
> > +            rebind<const value_type>::other
> > #endif
> >                                                                  pointer;
> 
> Peter - I'm lacking context here.
> What problem is this patch solving?
> And how can we test it?

The problem is that hash_map's const_iterator can otherwise be used to cast
away constness. For example:

#include <ext/hash_map>

int main() {
  __gnu_cxx::hash_map<int, int> m;
  m[1] = 1;
  const __gnu_cxx::hash_map<int, int> &cm = m;
  cm.find(1)->second = 2;
  return m[1]; // returns 2
}

Admittedly no test for this yet. It seems that the test coverage for this
class and indeed everything in ext/ is currently zero, but it makes sense
to do something about that now -- I'll take a look at adding a test for this
and my other change.

Thanks,
-- 
Peter



More information about the cfe-commits mailing list