[llvm] [llvm-c] Add missing nullptr check in LLVMGetFirstDbgRecord (PR #151101)
Maxime Arthaud via llvm-commits
llvm-commits at lists.llvm.org
Tue Aug 5 11:33:58 PDT 2025
https://github.com/arthaud updated https://github.com/llvm/llvm-project/pull/151101
>From e12de59a585c87b06a61bd29a4b430ea7d9e3592 Mon Sep 17 00:00:00 2001
From: Maxime Arthaud <maxime at arthaud.me>
Date: Tue, 29 Jul 2025 10:35:06 +0200
Subject: [PATCH] [llvm-c] Add missing nullptr check in LLVMGetFirstDbgRecord
Fix a crash when calling LLVMGetFirstDbgRecord or LLVMGetLastDbgRecord
on instructions without debug markers.
---
llvm/lib/IR/Core.cpp | 4 ++++
llvm/tools/llvm-c-test/debuginfo.c | 7 +++++++
2 files changed, 11 insertions(+)
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index f7ef4aa473ef5..56190545eb024 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -2984,6 +2984,8 @@ LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
Instruction *Instr = unwrap<Instruction>(Inst);
+ if (!Instr->DebugMarker)
+ return nullptr;
auto I = Instr->DebugMarker->StoredDbgRecords.begin();
if (I == Instr->DebugMarker->StoredDbgRecords.end())
return nullptr;
@@ -2992,6 +2994,8 @@ LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) {
Instruction *Instr = unwrap<Instruction>(Inst);
+ if (!Instr->DebugMarker)
+ return nullptr;
auto I = Instr->DebugMarker->StoredDbgRecords.rbegin();
if (I == Instr->DebugMarker->StoredDbgRecords.rend())
return nullptr;
diff --git a/llvm/tools/llvm-c-test/debuginfo.c b/llvm/tools/llvm-c-test/debuginfo.c
index e73f69743805c..4d25673326fc1 100644
--- a/llvm/tools/llvm-c-test/debuginfo.c
+++ b/llvm/tools/llvm-c-test/debuginfo.c
@@ -325,6 +325,13 @@ int llvm_test_dibuilder(void) {
LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2");
LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1);
+ // Test that LLVMGetFirstDbgRecord and LLVMGetLastDbgRecord return NULL for
+ // instructions without debug info.
+ LLVMDbgRecordRef Phi1FirstDbgRecord = LLVMGetFirstDbgRecord(Phi1);
+ assert(Phi1FirstDbgRecord == NULL);
+ LLVMDbgRecordRef Phi1LastDbgRecord = LLVMGetLastDbgRecord(Phi1);
+ assert(Phi1LastDbgRecord == NULL);
+
// Insert a non-phi before the `ret` but not before the debug records to
// test that works as expected.
LLVMPositionBuilder(Builder, FooVarBlock, Ret);
More information about the llvm-commits
mailing list