Skip to content

Commit 7168776

Browse files
committed
define: disallow exec after free (#141)
1 parent 3666596 commit 7168776

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/define/manage.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
// Manage defined functions.
55

6+
#include <stdbool.h>
67
#include <stdio.h>
78
#include <stdlib.h>
89

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

2122
static cache_node* cache_head = NULL;
2223
static cache_node* cache_tail = NULL;
24+
static bool cache_freed = false;
2325

2426
static int cache_add(sqlite3_stmt* stmt) {
2527
if (cache_head == NULL) {
@@ -43,6 +45,10 @@ static int cache_add(sqlite3_stmt* stmt) {
4345
}
4446

4547
static void cache_print() {
48+
if (cache_freed) {
49+
printf("cache is freed");
50+
return;
51+
}
4652
if (cache_head == NULL) {
4753
printf("cache is empty");
4854
return;
@@ -67,6 +73,7 @@ static void cache_free() {
6773
free(prev);
6874
}
6975
cache_head = cache_tail = NULL;
76+
cache_freed = true;
7077
}
7178

7279
/*
@@ -186,13 +193,13 @@ static void define_free(sqlite3_context* ctx, int argc, sqlite3_value** argv) {}
186193
* Executes compiled prepared statement from the context.
187194
*/
188195
static void define_exec(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
189-
int ret = SQLITE_OK;
190-
sqlite3_stmt* stmt = sqlite3_user_data(ctx);
191-
if (cache_head == NULL) {
196+
if (cache_freed) {
192197
// Calling defined functions after define_free is not allowed.
193198
sqlite3_result_error_code(ctx, SQLITE_MISUSE);
194199
return;
195200
}
201+
int ret = SQLITE_OK;
202+
sqlite3_stmt* stmt = sqlite3_user_data(ctx);
196203
for (int i = 0; i < argc; i++) {
197204
if ((ret = sqlite3_bind_value(stmt, i + 1, argv[i])) != SQLITE_OK) {
198205
sqlite3_reset(stmt);
@@ -250,6 +257,11 @@ static int define_create(sqlite3* db, const char* name, const char* body) {
250257
* Creates compiled user-defined function and saves it to the database.
251258
*/
252259
static void define_function(sqlite3_context* ctx, int argc, sqlite3_value** argv) {
260+
if (cache_freed) {
261+
// Calling defined functions after define_free is not allowed.
262+
sqlite3_result_error_code(ctx, SQLITE_MISUSE);
263+
return;
264+
}
253265
sqlite3* db = sqlite3_context_db_handle(ctx);
254266
const char* name = (const char*)sqlite3_value_text(argv[0]);
255267
const char* body = (const char*)sqlite3_value_text(argv[1]);

0 commit comments

Comments
 (0)