From 207eb94a51f385b319360bfa72b77fef386653d0 Mon Sep 17 00:00:00 2001 From: Gagan Nagaraj Date: Sun, 25 Jan 2026 20:31:22 -0700 Subject: [PATCH 1/2] check if samplers size > seq id before access --- common/common.cpp | 3 +++ tools/completion/completion.cpp | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/common/common.cpp b/common/common.cpp index 26250abb6c..0b8047c480 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1195,6 +1195,9 @@ llama_context * common_init_result::context() { } common_sampler * common_init_result::sampler(llama_seq_id seq_id) { + if (pimpl->samplers.size() <= (size_t) seq_id) { + return nullptr; + } return pimpl->samplers[seq_id].get(); } diff --git a/tools/completion/completion.cpp b/tools/completion/completion.cpp index f368a2f4c6..d9ac94babb 100644 --- a/tools/completion/completion.cpp +++ b/tools/completion/completion.cpp @@ -144,6 +144,10 @@ int main(int argc, char ** argv) { ctx = llama_init->context(); model = llama_init->model(); smpl = llama_init->sampler(0); + if (smpl == NULL) { + LOG_ERR("%s: error: unable to create sampler\n", __func__); + return 1; + } if (ctx == NULL) { LOG_ERR("%s: error: unable to create context\n", __func__); From 3c4b093e80bba07461af084de5a4ddfc8a098ab9 Mon Sep 17 00:00:00 2001 From: Gagan Nagaraj Date: Tue, 27 Jan 2026 21:12:33 -0700 Subject: [PATCH 2/2] check ctx and model before calling sampler --- common/common.cpp | 3 --- tools/completion/completion.cpp | 11 ++++------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/common/common.cpp b/common/common.cpp index 0b8047c480..26250abb6c 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1195,9 +1195,6 @@ llama_context * common_init_result::context() { } common_sampler * common_init_result::sampler(llama_seq_id seq_id) { - if (pimpl->samplers.size() <= (size_t) seq_id) { - return nullptr; - } return pimpl->samplers[seq_id].get(); } diff --git a/tools/completion/completion.cpp b/tools/completion/completion.cpp index d9ac94babb..7f8da2a59d 100644 --- a/tools/completion/completion.cpp +++ b/tools/completion/completion.cpp @@ -143,16 +143,13 @@ int main(int argc, char ** argv) { ctx = llama_init->context(); model = llama_init->model(); - smpl = llama_init->sampler(0); - if (smpl == NULL) { - LOG_ERR("%s: error: unable to create sampler\n", __func__); - return 1; - } - if (ctx == NULL) { - LOG_ERR("%s: error: unable to create context\n", __func__); + if (model == nullptr || ctx == nullptr) { + LOG_ERR("%s : failed to init\n", __func__); return 1; } + + smpl = llama_init->sampler(0); llama_memory_t mem = llama_get_memory(ctx); const llama_vocab * vocab = llama_model_get_vocab(model);