Skip to content

Commit 2fb8761

Browse files
committed
fix: add context validation to llm_chat_create function
Added checks to ensure a valid context exists before proceeding in llm_chat_check_context and llm_chat_create. This prevents misuse by requiring llm_context_create to be called prior to llm_chat_create, improving error handling and robustness.
1 parent ec505d7 commit 2fb8761

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/sqlite-ai.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,11 @@ static void llm_text_generate (sqlite3_context *context, int argc, sqlite3_value
14761476
// MARK: - Chat -
14771477

14781478
static bool llm_chat_check_context (ai_context *ai) {
1479+
if (!ai || !ai->ctx) {
1480+
sqlite_common_set_error(ai ? ai->context : NULL, ai ? ai->vtab : NULL, SQLITE_MISUSE, "No context found. Please call llm_context_create() before llm_chat_create().");
1481+
return false;
1482+
}
1483+
14791484
// check sampler
14801485
if (!ai->sampler) {
14811486
llm_sampler_check(ai);
@@ -1862,6 +1867,8 @@ static void llm_chat_free (sqlite3_context *context, int argc, sqlite3_value **a
18621867
}
18631868

18641869
static void llm_chat_create (sqlite3_context *context, int argc, sqlite3_value **argv) {
1870+
if (llm_check_context(context) == false) return;
1871+
18651872
ai_context *ai = (ai_context *)sqlite3_user_data(context);
18661873

18671874
// clean-up old chat (if any)

0 commit comments

Comments
 (0)