[llvm] Add missing nullptr check in LLVMGetFirstDbgRecord (PR #151101)

Maxime Arthaud via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 29 01:38:15 PDT 2025


https://github.com/arthaud created https://github.com/llvm/llvm-project/pull/151101

I'm using the LLVM C bindings through the llvm-sys rust crate, and notice that LLVMGetFirstDbgRecord and LLVMGetLastDbgRecord are segfault-ing when called on instructions without debug markers. I found out it's missing a null pointer check. This PR fixes the issue.



>From 7031036375ab00cd8dd948391d3ca19806e98114 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] 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 ++++
 1 file changed, 4 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;



More information about the llvm-commits mailing list