Request for a new compiler warning for moved objects

17 views
Skip to first unread message

Sean

unread,
Dec 1, 2016, 2:26:14 PM12/1/16
to Chromium-dev
Is it possible to add a compiler warning when calling methods on objects that have been moved.  Especially in a function call?  I've been bitten by this a few times now.

Throws SIGSEGV:

std::unique_ptr<Thingy> smart_ptr = ...;
...
delegate_->Resolve(
  smart_ptr->bar(),
  base::Bind(&Handler::OnResolveHandled,
                      weak_factory_.GetWeakPtr(), base::Passed(&smart_ptr)));


FIXED:
std::unique_ptr<Thingy> smart_ptr = ...;
...

const Bar& ref = smart_ptr->bar();
delegate_->Resolve(
  ref,
  base::Bind(&Handler::OnResolveHandled,
                      weak_factory_.GetWeakPtr(), base::Passed(&smart_ptr)));

The cause of this is that there's no required ordering for the evaluation of function parameters.  So, smart_ptr is moved before smart_ptr->bar() is evaluated.  So, smart_ptr->bar() might cause a segfault.

Nick Maniscalco

unread,
Dec 1, 2016, 2:31:31 PM12/1/16
to sk...@chromium.org, Chromium-dev

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Nico Weber

unread,
Dec 1, 2016, 2:39:31 PM12/1/16
to manis...@chromium.org, sk...@chromium.org, Chromium-dev
Yup; also see https://llvm.org/bugs/show_bug.cgi?id=20819 Let me know if you're interested in working on this :-)

Sean Kau

unread,
Dec 1, 2016, 2:48:34 PM12/1/16
to Nico Weber, manis...@chromium.org, Chromium-dev
yep.  That's definitely the right bug.  Would I be interested? yes.  Do I have time?  Probably not.  :(

Sean

unread,
Dec 1, 2016, 4:26:22 PM12/1/16
to Chromium-dev
If anybody ends up picking this up, this is an update to llvm's use-after-move detection:

CJ Dimeglio

unread,
Dec 1, 2016, 4:40:50 PM12/1/16
to Chromium-dev
Im looking into it now.
Reply all
Reply to author
Forward
0 new messages