[opt] Fix static code analysis concerns
authorArvind Sudarsanam <arvind.sudarsanam@intel.com>
Thu, 19 Jan 2023 18:24:46 +0000 (10:24 -0800)
committerArthur Eubanks <aeubanks@google.com>
Thu, 19 Jan 2023 19:35:15 +0000 (11:35 -0800)
This is an issue reported inside the NewPMDriver module. Static analyzer reported that Null pointer 'P' may be dereferenced at line 371 and two more sites. Proposed change guards this use.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D142047

llvm/tools/opt/NewPMDriver.cpp

index 2e66c0b..a8db0c6 100644 (file)
@@ -354,11 +354,15 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
   }
   if (CSPGOKindFlag != NoCSPGO) {
     if (P && (P->Action == PGOOptions::IRInstr ||
-              P->Action == PGOOptions::SampleUse))
+              P->Action == PGOOptions::SampleUse)) {
       errs() << "CSPGOKind cannot be used with IRInstr or SampleUse";
+      return false;
+    }
     if (CSPGOKindFlag == CSInstrGen) {
-      if (CSProfileGenFile.empty())
+      if (CSProfileGenFile.empty()) {
         errs() << "CSInstrGen needs to specify CSProfileGenFile";
+        return false;
+      }
       if (P) {
         P->CSAction = PGOOptions::CSIRInstr;
         P->CSProfileGenFile = CSProfileGenFile;
@@ -366,8 +370,10 @@ bool llvm::runPassPipeline(StringRef Arg0, Module &M, TargetMachine *TM,
         P = PGOOptions("", CSProfileGenFile, ProfileRemappingFile,
                        PGOOptions::NoAction, PGOOptions::CSIRInstr);
     } else /* CSPGOKindFlag == CSInstrUse */ {
-      if (!P)
+      if (!P) {
         errs() << "CSInstrUse needs to be together with InstrUse";
+        return false;
+      }
       P->CSAction = PGOOptions::CSIRUse;
     }
   }