Skip to content

Commit 0338350

Browse files
authored
[llvm-c] Add missing nullptr check in LLVMGetFirstDbgRecord (llvm#151101)
I'm using the LLVM C bindings through the llvm-sys rust crate, and noticed 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.
1 parent e8489c1 commit 0338350

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

llvm/lib/IR/Core.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2994,6 +2994,8 @@ LLVMValueRef LLVMIsATerminatorInst(LLVMValueRef Inst) {
29942994

29952995
LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
29962996
Instruction *Instr = unwrap<Instruction>(Inst);
2997+
if (!Instr->DebugMarker)
2998+
return nullptr;
29972999
auto I = Instr->DebugMarker->StoredDbgRecords.begin();
29983000
if (I == Instr->DebugMarker->StoredDbgRecords.end())
29993001
return nullptr;
@@ -3002,6 +3004,8 @@ LLVMDbgRecordRef LLVMGetFirstDbgRecord(LLVMValueRef Inst) {
30023004

30033005
LLVMDbgRecordRef LLVMGetLastDbgRecord(LLVMValueRef Inst) {
30043006
Instruction *Instr = unwrap<Instruction>(Inst);
3007+
if (!Instr->DebugMarker)
3008+
return nullptr;
30053009
auto I = Instr->DebugMarker->StoredDbgRecords.rbegin();
30063010
if (I == Instr->DebugMarker->StoredDbgRecords.rend())
30073011
return nullptr;

llvm/tools/llvm-c-test/debuginfo.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ int llvm_test_dibuilder(void) {
325325
LLVMValueRef Phi2 = LLVMBuildPhi(Builder, I64, "p2");
326326
LLVMAddIncoming(Phi2, &Zero, &FooEntryBlock, 1);
327327

328+
// Test that LLVMGetFirstDbgRecord and LLVMGetLastDbgRecord return NULL for
329+
// instructions without debug info.
330+
LLVMDbgRecordRef Phi1FirstDbgRecord = LLVMGetFirstDbgRecord(Phi1);
331+
assert(Phi1FirstDbgRecord == NULL);
332+
LLVMDbgRecordRef Phi1LastDbgRecord = LLVMGetLastDbgRecord(Phi1);
333+
assert(Phi1LastDbgRecord == NULL);
334+
328335
// Insert a non-phi before the `ret` but not before the debug records to
329336
// test that works as expected.
330337
LLVMPositionBuilder(Builder, FooVarBlock, Ret);

0 commit comments

Comments
 (0)