Skip to content

Commit 346fa64

Browse files
committed
Check ty is sized in build_malloc (fixes rust-lang#150)
1 parent 0a864eb commit 346fa64

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/builder.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::basic_block::BasicBlock;
1212
use crate::values::{AggregateValue, AggregateValueEnum, AsValueRef, BasicValue, BasicValueEnum, PhiValue, FunctionValue, IntValue, PointerValue, VectorValue, InstructionValue, GlobalValue, IntMathValue, FloatMathValue, PointerMathValue, InstructionOpcode, CallSiteValue};
1313
#[llvm_versions(3.9..=latest)]
1414
use crate::values::StructValue;
15-
use crate::types::{AsTypeRef, BasicType, IntMathType, FloatMathType, PointerType, PointerMathType};
15+
use crate::types::{AsTypeRef, BasicType, BasicTypeEnum, IntMathType, FloatMathType, PointerType, PointerMathType};
1616

1717
use std::ffi::CString;
1818
use std::marker::PhantomData;
@@ -349,6 +349,19 @@ impl<'ctx> Builder<'ctx> {
349349

350350
// TODOC: Heap allocation
351351
pub fn build_malloc<T: BasicType<'ctx>>(&self, ty: T, name: &str) -> PointerValue<'ctx> {
352+
// LLVMBulidMalloc segfaults if ty is unsized
353+
let is_sized = match ty.as_basic_type_enum() {
354+
BasicTypeEnum::ArrayType(ty) => ty.is_sized(),
355+
BasicTypeEnum::FloatType(ty) => ty.is_sized(),
356+
BasicTypeEnum::IntType(ty) => ty.is_sized(),
357+
BasicTypeEnum::PointerType(ty) => ty.is_sized(),
358+
BasicTypeEnum::StructType(ty) => ty.is_sized(),
359+
BasicTypeEnum::VectorType(ty) => ty.is_sized(),
360+
};
361+
if !is_sized {
362+
panic!("Cannot build malloc call for an unsized type {:?}", ty);
363+
}
364+
352365
let c_string = CString::new(name).expect("Conversion to CString failed unexpectedly");
353366

354367
let value = unsafe {

0 commit comments

Comments
 (0)