Skip to content

Commit

Permalink
Don't create null references in Ref
Browse files Browse the repository at this point in the history
Ref used to get the underlying pointer of another Ref object by doing
"&*OTHER", where the "*" would invoked Ref::operator*.  In the case
where OTHER.obj is null, this would return a "null reference" which is
not a valid thing in C++.

This actually worked correctly in most cases, because we'd immediately
take the address, and given the way C++ compilers implement
references, it would all sort of work out.  However it's still not
valid code, and was causing crashes when compiled with recent versions
of clang.

Fix this simply by directly getting the pointer, instead of going
through a reference.
  • Loading branch information
snogglethorpe committed Aug 20, 2014
1 parent 257b1fa commit b70caef
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions util/ref.h
@@ -1,6 +1,6 @@
// ref.h -- Reference-counting framework
//
// Copyright (C) 2005, 2006, 2007, 2008, 2011 Miles Bader <miles@gnu.org>
// Copyright (C) 2005-2008, 2011, 2014 Miles Bader <miles@gnu.org>
//
// This source code is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
Expand Down Expand Up @@ -47,10 +47,10 @@ class Ref

Ref () : obj (0) { }
Ref (T *_obj) : obj (_obj) { if (obj) obj->ref (); }
Ref (const Ref &ref) : obj (&*ref) { if (obj) obj->ref (); }
Ref (const Ref &ref) : obj (ref.obj) { if (obj) obj->ref (); }

template<class T2>
Ref (const Ref<T2> &ref) : obj (&*ref) { if (obj) obj->ref (); }
Ref (const Ref<T2> &ref) : obj (ref.ptr ()) { if (obj) obj->ref (); }

~Ref () { if (obj) obj->deref (); }

Expand All @@ -65,6 +65,10 @@ class Ref
//
bool null () const { return !obj; }

// Return a pointer to the referenced object; it may be null.
//
T *ptr () const { return obj; }

//operator T* () const { return obj; }

template<class T2>
Expand Down

0 comments on commit b70caef

Please sign in to comment.