This is an archive of the discontinued LLVM Phabricator instance.

[PATCH] New checker for UB in handler of a function-try-block
ClosedPublic

Authored by aaron.ballman on Aug 24 2015, 3:23 PM.

Details

Reviewers
rsmith
Summary

Per [except.handle]p10, the handler for a constructor or destructor function-try-block cannot refer to a non-static member of the object under construction. This patch adds a new clang-tidy check that warns the user when they've hit this undefined behavior.

Due to how infrequent function-try-blocks appear on constructors and destructors in the wild compared to how often member expressions are encountered, I felt this was more appropriate as a clang-tidy check than as a semantic warning. I was concerned with efficiency of checking whether an arbitrary member expression was referring to the object under construction/destruction within the function-try-block catch handler scope.

This patch corresponds to the CERT secure coding rule ERR53-CPP (https://www.securecoding.cert.org/confluence/display/cplusplus/ERR53-CPP.+Do+not+reference+base+classes+or+class+data+members+in+a+constructor+or+destructor+function-try-block+handler)

Diff Detail

Event Timeline

aaron.ballman retitled this revision from to [PATCH] New checker for UB in handler of a function-try-block.
aaron.ballman updated this object.
aaron.ballman added reviewers: alexfh, rsmith.
aaron.ballman added a subscriber: cfe-commits.

This patch completely reworks the way the warning is implemented by moving it into the frontend instead of clang-tidy.

alexfh removed a reviewer: alexfh.Aug 25 2015, 8:02 AM

This patch completely reworks the way the warning is implemented by moving it into the frontend instead of clang-tidy.

Ping

~Aaron

rsmith accepted this revision.Aug 31 2015, 3:13 PM
rsmith edited edge metadata.

Couple of thoughts, but LGTM.

lib/Sema/SemaExprMember.cpp
889

Maybe convert from a do-while into a while loop (or even a for loop)? We might not be in a function at all (for instance, we might be in a default member initializer), and that would let us bail out earlier in that case.

969–972

The FD checks are cheaper than the scope check; maybe reorder this to check the kind of the function first?

This revision is now accepted and ready to land.Aug 31 2015, 3:13 PM
aaron.ballman closed this revision.Sep 1 2015, 7:51 AM

Thanks! I've made those changes and commit in r246548, along with an updated test case for ObjC++ that I had previously missed.