[llvm] Add non-null check before accessing pointer (PR #83459)

Martin Wehking via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 29 10:27:47 PST 2024


https://github.com/MartinWehking created https://github.com/llvm/llvm-project/pull/83459

Add a check if RC is not null to ensure that a consecutive access is safe.

A static analyzer flagged this issue since hasVectorRegisters potentially dereferences RC.

>From 670304176e9cee338e7cd09fa4104993cbe954a3 Mon Sep 17 00:00:00 2001
From: Martin Wehking <martin.wehking at codeplay.com>
Date: Thu, 29 Feb 2024 18:14:43 +0000
Subject: [PATCH] Add non-null check before accessing pointer

Add a check if RC is not null to ensure that a consecutive access
is safe.

A static analyzer flagged this issue since hasVectorRegisters
potentially dereferences RC.
---
 llvm/lib/Target/AMDGPU/SIFoldOperands.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
index 634b4aeb30a730..1cef4f1fa81e3a 100644
--- a/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
+++ b/llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
@@ -1054,7 +1054,7 @@ void SIFoldOperands::foldOperand(
       // Don't fold if OpToFold doesn't hold an aligned register.
       const TargetRegisterClass *RC =
           TRI->getRegClassForReg(*MRI, OpToFold.getReg());
-      if (TRI->hasVectorRegisters(RC) && OpToFold.getSubReg()) {
+      if (RC && TRI->hasVectorRegisters(RC) && OpToFold.getSubReg()) {
         unsigned SubReg = OpToFold.getSubReg();
         if (const TargetRegisterClass *SubRC =
                 TRI->getSubRegisterClass(RC, SubReg))



More information about the llvm-commits mailing list