Skip to content

Commit 3b8f832

Browse files
RKSimonmemfrob
authored andcommitted
[AMDGPU] Fix null-dereference static analysis warnings. NFCI.
Avoid repeated calls to isZeroValue() and check for a null pointer before dereferencing a dyn_cast<>.
1 parent 7a28a3e commit 3b8f832

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

llvm/lib/Target/AMDGPU/AMDGPUPrintfRuntimeBinding.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
182182

183183
StringRef Str("unknown");
184184
if (GVar && GVar->hasInitializer()) {
185-
auto Init = GVar->getInitializer();
186-
if (auto CA = dyn_cast<ConstantDataArray>(Init)) {
185+
auto *Init = GVar->getInitializer();
186+
if (auto *CA = dyn_cast<ConstantDataArray>(Init)) {
187187
if (CA->isString())
188188
Str = CA->getAsCString();
189189
} else if (isa<ConstantAggregateZero>(Init)) {
@@ -246,16 +246,15 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
246246
}
247247
}
248248
if (shouldPrintAsStr(OpConvSpecifiers[ArgCount - 1], ArgType)) {
249-
if (ConstantExpr *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
250-
GlobalVariable *GV =
251-
dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
249+
if (auto *ConstExpr = dyn_cast<ConstantExpr>(Arg)) {
250+
auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
252251
if (GV && GV->hasInitializer()) {
253252
Constant *Init = GV->getInitializer();
254-
ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init);
255-
if (Init->isZeroValue() || CA->isString()) {
256-
size_t SizeStr = Init->isZeroValue()
257-
? 1
258-
: (strlen(CA->getAsCString().data()) + 1);
253+
bool IsZeroValue = Init->isZeroValue();
254+
auto *CA = dyn_cast<ConstantDataArray>(Init);
255+
if (IsZeroValue || (CA && CA->isString())) {
256+
size_t SizeStr =
257+
IsZeroValue ? 1 : (strlen(CA->getAsCString().data()) + 1);
259258
size_t Rem = SizeStr % DWORD_ALIGN;
260259
size_t NSizeStr = 0;
261260
LLVM_DEBUG(dbgs() << "Printf string original size = " << SizeStr
@@ -430,9 +429,10 @@ bool AMDGPUPrintfRuntimeBindingImpl::lowerPrintfForGpu(Module &M) {
430429
auto *GV = dyn_cast<GlobalVariable>(ConstExpr->getOperand(0));
431430
if (GV && GV->hasInitializer()) {
432431
Constant *Init = GV->getInitializer();
433-
ConstantDataArray *CA = dyn_cast<ConstantDataArray>(Init);
434-
if (Init->isZeroValue() || CA->isString()) {
435-
S = Init->isZeroValue() ? "" : CA->getAsCString().data();
432+
bool IsZeroValue = Init->isZeroValue();
433+
auto *CA = dyn_cast<ConstantDataArray>(Init);
434+
if (IsZeroValue || (CA && CA->isString())) {
435+
S = IsZeroValue ? "" : CA->getAsCString().data();
436436
}
437437
}
438438
}

0 commit comments

Comments
 (0)