Skip to content

Commit 3d74ccb

Browse files
committed
Fix segmentation fault if item is not in the list
1 parent 8f3393d commit 3d74ccb

1 file changed

Lines changed: 32 additions & 16 deletions

File tree

src/libasr/codegen/llvm_utils.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4724,7 +4724,10 @@ namespace LCompilers {
47244724

47254725
/* Equivalent in C++:
47264726
* int i = start;
4727-
* while(list[i] != item && end_point > i) {
4727+
* while(end_point > i) {
4728+
* if (list[i] == item) {
4729+
* break;
4730+
* }
47284731
* i++;
47294732
* }
47304733
*
@@ -4740,28 +4743,38 @@ namespace LCompilers {
47404743
// head
47414744
llvm_utils->start_new_block(loophead);
47424745
{
4743-
llvm::Value* left_arg = read_item(list, LLVM::CreateLoad(*builder, i),
4744-
false, module, LLVM::is_llvm_struct(item_type));
4745-
llvm::Value* is_item_not_equal = builder->CreateNot(
4746-
llvm_utils->is_equal_by_value(
4747-
left_arg, item,
4748-
module, item_type)
4749-
);
4750-
llvm::Value *cond = builder->CreateAnd(is_item_not_equal,
4751-
builder->CreateICmpSGT(end_point,
4752-
LLVM::CreateLoad(*builder, i)));
4746+
llvm::Value *cond = builder->CreateICmpSGT(end_point,
4747+
LLVM::CreateLoad(*builder, i));
47534748
builder->CreateCondBr(cond, loopbody, loopend);
47544749
}
47554750

47564751
// body
47574752
llvm_utils->start_new_block(loopbody);
47584753
{
4759-
tmp = builder->CreateAdd(
4760-
LLVM::CreateLoad(*builder, i),
4761-
llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
4762-
LLVM::CreateStore(*builder, tmp, i);
4754+
llvm::Value* left_arg = read_item(list, LLVM::CreateLoad(*builder, i),
4755+
false, module, LLVM::is_llvm_struct(item_type));
4756+
4757+
llvm::Value* is_item_equal = llvm_utils->is_equal_by_value(
4758+
left_arg, item,
4759+
module, item_type);
4760+
4761+
llvm::BasicBlock *ifblock = llvm::BasicBlock::Create(context, "if");
4762+
llvm::BasicBlock *elseblock = llvm::BasicBlock::Create(context, "else");
4763+
4764+
llvm_utils->start_new_block(ifblock);
4765+
{
4766+
builder->CreateCondBr(is_item_equal, loopend, elseblock);
4767+
}
4768+
4769+
llvm_utils->start_new_block(elseblock);
4770+
{
4771+
tmp = builder->CreateAdd(
4772+
LLVM::CreateLoad(*builder, i),
4773+
llvm::ConstantInt::get(context, llvm::APInt(32, 1)));
4774+
LLVM::CreateStore(*builder, tmp, i);
4775+
builder->CreateBr(loophead);
4776+
}
47634777
}
4764-
builder->CreateBr(loophead);
47654778

47664779
// end
47674780
llvm_utils->start_new_block(loopend);
@@ -4775,6 +4788,9 @@ namespace LCompilers {
47754788
std::string message = "The list does not contain the element: ";
47764789
llvm::Value *fmt_ptr = builder->CreateGlobalStringPtr("ValueError: %s%d\n");
47774790
llvm::Value *fmt_ptr2 = builder->CreateGlobalStringPtr(message);
4791+
if (ASR::is_a<ASR::Character_t>(*item_type)) {
4792+
fmt_ptr = builder->CreateGlobalStringPtr("ValueError: %s%s\n");
4793+
}
47784794
print_error(context, module, *builder, {fmt_ptr, fmt_ptr2, item});
47794795
int exit_code_int = 1;
47804796
llvm::Value *exit_code = llvm::ConstantInt::get(context,

0 commit comments

Comments
 (0)