This is an archive of the discontinued LLVM Phabricator instance.

[NFC][CLANG] Fix dereference issue before null check found by Coverity static analyzer tool
ClosedPublic

Authored by Manna on May 18 2023, 11:18 AM.

Details

Summary

Reported by Coverity static analyzer tool:

Inside "ParsePragma.cpp" file, in <unnamed>::​PragmaRISCVHandler::​HandlePragma(clang::​Preprocessor &, clang::​PragmaIntroducer, clang::​Token &): All paths that lead to this null pointer comparison already dereference the pointer earlier

PP.Lex(Tok);
II = Tok.getIdentifierInfo();
//deref_ptr_in_call: Dereferencing pointer II. 
StringRef IntrinsicClass = II->getName();
    	
//Dereference before null check (REVERSE_INULL)
//check_after_deref: Null-checking II suggests that it may be null, but it has already been dereferenced on all paths leading to the check.
  if (!II || !(II->isStr("vector") || II->isStr("sifive_vector"))) {
    PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
        << PP.getSpelling(Tok) << "riscv" << /*Expected=*/true
        << "'vector' or 'sifive_vector'";
    return;
}

This patch moves the line StringRef IntrinsicClass = II->getName() after null checking II

Diff Detail

Event Timeline

Manna created this revision.May 18 2023, 11:18 AM
Manna requested review of this revision.May 18 2023, 11:18 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 18 2023, 11:18 AM
Manna edited the summary of this revision. (Show Details)May 18 2023, 11:18 AM
erichkeane added inline comments.May 18 2023, 11:20 AM
clang/lib/Parse/ParsePragma.cpp
4042

I think you still need line 4043 here, right? The one where II is set to the Token?

Manna updated this revision to Diff 523468.May 18 2023, 11:26 AM
Manna edited the summary of this revision. (Show Details)

I have updated patch.

Manna updated this revision to Diff 523481.May 18 2023, 11:50 AM
erichkeane added inline comments.May 18 2023, 11:55 AM
clang/lib/Parse/ParsePragma.cpp
4057

I'm having trouble figuring out why this isn't a breaking change. PP.Lex(Tok) moves the current 'token' to be the next one (that is, it has side effects!), so now this ends up looking in a different place for the intrin name?

This will result in IntrinsicClass becoming the value of the token 2 after the fact, which doesn't seem right to me. I suspect the correct answer here is to just move IntrinsicClass ~4050.

Manna added inline comments.May 18 2023, 12:01 PM
clang/lib/Parse/ParsePragma.cpp
4042

oops! Fixed now. Thanks @erichkeane for catching!

4057

I am wondering whether do we need IntrinsicClass`. Can we do something like:

if (II->isStr("vector"))
    Actions.DeclareRISCVVBuiltins = true;
  else if (II->isStr("sifive_vector")
    Actions.DeclareRISCVVectorBuiltins = true;
erichkeane added inline comments.May 18 2023, 12:07 PM
clang/lib/Parse/ParsePragma.cpp
4057

Probably not necessary, as long as II doesn't after ~4044.

Manna updated this revision to Diff 523502.May 18 2023, 12:20 PM

I have removed IntrinsicClass and updated patch.

erichkeane added inline comments.May 18 2023, 12:22 PM
clang/lib/Parse/ParsePragma.cpp
4050

So I'd say leave this where it was. Once line 4043 happens, II is stable, we just need to make sure it doesn't get changed (which I see it doesnt). So I'd probably just put this back where it was.

Manna updated this revision to Diff 523509.May 18 2023, 12:32 PM
erichkeane accepted this revision.May 18 2023, 12:34 PM
This revision is now accepted and ready to land.May 18 2023, 12:34 PM
Manna marked an inline comment as done.May 18 2023, 12:36 PM
Manna added inline comments.
clang/lib/Parse/ParsePragma.cpp
4050

Thank you @erichkeane!

This revision was automatically updated to reflect the committed changes.
Manna marked an inline comment as done.