Skip to content

Commit

Permalink
[NFC][CLANG] Fix dereference issue before null check found by Coverit…
Browse files Browse the repository at this point in the history
…y static analyzer tool

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 removes redundant StringRef type 'IntrinsicClass' and checks
II->isStr("vector") || II->isStr("sifive_vector") instead to set Actions.DeclareRISCVVBuiltins or
Actions.DeclareRISCVVectorBuiltins.

Reviewed By: erichkeane

Differential Revision: https://reviews.llvm.org/D150895
  • Loading branch information
smanna12 committed May 19, 2023
1 parent 5590315 commit be37e3e
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions clang/lib/Parse/ParsePragma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4041,7 +4041,6 @@ void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,

PP.Lex(Tok);
II = Tok.getIdentifierInfo();
StringRef IntrinsicClass = II->getName();
if (!II || !(II->isStr("vector") || II->isStr("sifive_vector"))) {
PP.Diag(Tok.getLocation(), diag::warn_pragma_invalid_argument)
<< PP.getSpelling(Tok) << "riscv" << /*Expected=*/true
Expand All @@ -4056,8 +4055,8 @@ void PragmaRISCVHandler::HandlePragma(Preprocessor &PP,
return;
}

if (IntrinsicClass == "vector")
if (II->isStr("vector"))
Actions.DeclareRISCVVBuiltins = true;
else if (IntrinsicClass == "sifive_vector")
else if (II->isStr("sifive_vector"))
Actions.DeclareRISCVVectorBuiltins = true;
}

0 comments on commit be37e3e

Please sign in to comment.