Skip to content

Commit 492ccb0

Browse files
committed
Fix list insert index range check
1 parent 13f7fae commit 492ccb0

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/engine/internal/llvm/instructions/lists.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ LLVMInstruction *Lists::buildInsertToList(LLVMInstruction *ins)
184184
// Range check
185185
llvm::Value *indexDouble = m_utils.castValue(indexArg.second, indexArg.first);
186186
llvm::Value *indexInt = getIndex(listPtr, indexDouble);
187-
llvm::Value *inRange = createSizeRangeCheck(listPtr, indexInt, "insertToList.indexInRange");
187+
llvm::Value *inRange = createSizeRangeCheck(listPtr, indexInt, "insertToList.indexInRange", true);
188188

189189
llvm::BasicBlock *insertBlock = llvm::BasicBlock::Create(llvmCtx, "", function);
190190
llvm::BasicBlock *nextBlock = llvm::BasicBlock::Create(llvmCtx, "", function);
@@ -367,10 +367,14 @@ llvm::Value *Lists::getIndex(const LLVMListPtr &listPtr, llvm::Value *indexDoubl
367367
return m_builder.CreateSelect(isNegative, llvm::ConstantInt::get(m_builder.getInt64Ty(), INT64_MAX), m_builder.CreateFPToUI(indexDouble, m_builder.getInt64Ty(), "listIndex.int"));
368368
}
369369

370-
llvm::Value *Lists::createSizeRangeCheck(const LLVMListPtr &listPtr, llvm::Value *indexInt, const std::string &name)
370+
llvm::Value *Lists::createSizeRangeCheck(const LLVMListPtr &listPtr, llvm::Value *indexInt, const std::string &name, bool includeSize)
371371
{
372372
llvm::Value *size = m_utils.getListSize(listPtr);
373-
return m_builder.CreateICmpULT(indexInt, size, name);
373+
374+
if (includeSize)
375+
return m_builder.CreateICmpULE(indexInt, size, name);
376+
else
377+
return m_builder.CreateICmpULT(indexInt, size, name);
374378
}
375379

376380
void Lists::createListTypeUpdate(const LLVMListPtr &listPtr, const LLVMRegister *newValue, Compiler::StaticType newValueType)

src/engine/internal/llvm/instructions/lists.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Lists : public InstructionGroup
3535
LLVMInstruction *buildListContainsItem(LLVMInstruction *ins);
3636

3737
llvm::Value *getIndex(const LLVMListPtr &listPtr, llvm::Value *indexDouble);
38-
llvm::Value *createSizeRangeCheck(const LLVMListPtr &listPtr, llvm::Value *indexInt, const std::string &name);
38+
llvm::Value *createSizeRangeCheck(const LLVMListPtr &listPtr, llvm::Value *indexInt, const std::string &name, bool includeSize = false);
3939

4040
void createListTypeUpdate(const LLVMListPtr &listPtr, const LLVMRegister *newValue, Compiler::StaticType newValueType);
4141
llvm::Value *createListTypeVar(const LLVMListPtr &listPtr, llvm::Value *type);

0 commit comments

Comments
 (0)