Skip to content

Commit

Permalink
define: disallow exec after free (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Feb 11, 2025
1 parent 3666596 commit 7168776
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/define/manage.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Manage defined functions.

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

Expand All @@ -20,6 +21,7 @@ typedef struct cache_node {

static cache_node* cache_head = NULL;
static cache_node* cache_tail = NULL;
static bool cache_freed = false;

static int cache_add(sqlite3_stmt* stmt) {
if (cache_head == NULL) {
Expand All @@ -43,6 +45,10 @@ static int cache_add(sqlite3_stmt* stmt) {
}

static void cache_print() {
if (cache_freed) {
printf("cache is freed");
return;
}
if (cache_head == NULL) {
printf("cache is empty");
return;
Expand All @@ -67,6 +73,7 @@ static void cache_free() {
free(prev);
}
cache_head = cache_tail = NULL;
cache_freed = true;
}

/*
Expand Down Expand Up @@ -186,13 +193,13 @@ static void define_free(sqlite3_context* ctx, int argc, sqlite3_value** argv) {}
* Executes compiled prepared statement from the context.
*/
static void define_exec(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
int ret = SQLITE_OK;
sqlite3_stmt* stmt = sqlite3_user_data(ctx);
if (cache_head == NULL) {
if (cache_freed) {
// Calling defined functions after define_free is not allowed.
sqlite3_result_error_code(ctx, SQLITE_MISUSE);
return;
}
int ret = SQLITE_OK;
sqlite3_stmt* stmt = sqlite3_user_data(ctx);
for (int i = 0; i < argc; i++) {
if ((ret = sqlite3_bind_value(stmt, i + 1, argv[i])) != SQLITE_OK) {
sqlite3_reset(stmt);
Expand Down Expand Up @@ -250,6 +257,11 @@ static int define_create(sqlite3* db, const char* name, const char* body) {
* Creates compiled user-defined function and saves it to the database.
*/
static void define_function(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
if (cache_freed) {
// Calling defined functions after define_free is not allowed.
sqlite3_result_error_code(ctx, SQLITE_MISUSE);
return;
}
sqlite3* db = sqlite3_context_db_handle(ctx);
const char* name = (const char*)sqlite3_value_text(argv[0]);
const char* body = (const char*)sqlite3_value_text(argv[1]);
Expand Down

0 comments on commit 7168776

Please sign in to comment.