This is an archive of the discontinued LLVM Phabricator instance.

Avoid nullptr dereferencing of 'Constraint'
ClosedPublic

Authored by schittir on Aug 20 2021, 12:49 PM.

Details

Summary
Klocwork static code analysis exposed this bug:
Pointer 'Constraint' returned from call to function
'cast_or_null<clang::ConceptSpecializationExpr,clang::Expr>' may
be NULL and will be dereferenced in the statement following it

Replace 'cast_or_null' with 'cast' so that the latter can assert
when it encounters a NULL

Diff Detail

Event Timeline

schittir requested review of this revision.Aug 20 2021, 12:49 PM
schittir created this revision.
aaron.ballman added a subscriber: aaron.ballman.

Thanks for the cleanup! Btw, it's helpful if you add more context to the patch when generating the diff. I typically use git diff -U9999 when generating a diff as that gives plenty of context for the patch review within Phabricator.

clang/lib/Sema/SemaConcept.cpp
1065–1068

If we're going to be touching this code, there's more suspect code here that needs to be cleaned up a bit. Directly above this is:

const TypeConstraint *TC =
    cast<TemplateTypeParmDecl>(TPL->getParam(0))->getTypeConstraint();
assert(TC &&
       "TPL must have a template type parameter with a type constraint");

That assertion can be removed entirely -- if the cast<> fails, it doesn't return null, it asserts.

schittir updated this revision to Diff 368183.Aug 23 2021, 12:55 PM
  1. Removed the useless assert on TC.
  2. Retained the change from cast_or_null to cast
erichkeane accepted this revision.Aug 23 2021, 12:56 PM
This revision is now accepted and ready to land.Aug 23 2021, 12:56 PM
aaron.ballman accepted this revision.Aug 23 2021, 12:58 PM

LGTM, thanks for the cleanup! Do you need someone to commit on your behalf? If so, what name and email address would you like me to use for patch attribution?

schittir marked an inline comment as done.Aug 23 2021, 1:01 PM

LGTM, thanks for the cleanup! Do you need someone to commit on your behalf? If so, what name and email address would you like me to use for patch attribution?

Thank you, Aaron.
Yes, please! Sindhu Chittireddy; sindhu.chittireddy@intel.com

aaron.ballman closed this revision.Aug 24 2021, 4:10 AM

LGTM, thanks for the cleanup! Do you need someone to commit on your behalf? If so, what name and email address would you like me to use for patch attribution?

Thank you, Aaron.
Yes, please! Sindhu Chittireddy; sindhu.chittireddy@intel.com

Thanks! I've committed on your behalf in 98339f14a0420cdfbe4215d8d1bc0a01165e0495.

mlychkov added inline comments.
clang/lib/Sema/SemaConcept.cpp
1065–1068

Actually, cast return nonnull value, but

TC = "cast_result"->getTypeConstraint();

and TC may become nullptr, I suppose.

aaron.ballman added inline comments.Aug 26 2021, 9:25 AM
clang/lib/Sema/SemaConcept.cpp
1065–1068

Good catch! I think we should add the assertion on TC being nonnull back. I'll take care of that.

erichkeane added inline comments.Aug 26 2021, 9:27 AM
clang/lib/Sema/SemaConcept.cpp
1065–1068

Ooh, good catch! I definitely missed that when suggesting removing the assert. @aaron.ballman can you re-add the assert on 1065?

aaron.ballman added inline comments.Aug 26 2021, 9:53 AM
clang/lib/Sema/SemaConcept.cpp
1065–1068

I've put the assert back in 0cf4f81082e9fa052e60450b8cbb10007e59931c, thanks for letting us know @mlychkov!