Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not try to access vector items if (re)allocation failed #2081

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sqlite3_stmt_vec *new_sqlite3_stmt_vec(unsigned int initial_size)
return v;
}

static void resize_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int capacity)
static bool resize_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int capacity)
{
log_debug(DEBUG_VECTORS, "Resizing sqlite3_stmt* vector %p from %u to %u", v, v->capacity, capacity);

Expand All @@ -48,7 +48,7 @@ static void resize_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int capacity)
{
log_err("Memory allocation failed in resize_sqlite3_stmt_vec(%p, %u)",
v, capacity);
return;
return false;
}

// Update items pointer
Expand All @@ -60,6 +60,8 @@ static void resize_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int capacity)

// Update capacity
v->capacity = capacity;

return true;
}

void set_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int index, sqlite3_stmt *item)
Expand All @@ -78,7 +80,8 @@ void set_sqlite3_stmt_vec(sqlite3_stmt_vec *v, unsigned int index, sqlite3_stmt
// Allocate more memory when trying to set a statement vector entry with
// an index larger than the current array size (this makes set an
// equivalent alternative to append)
resize_sqlite3_stmt_vec(v, index + VEC_ALLOC_STEP);
if(!resize_sqlite3_stmt_vec(v, index + VEC_ALLOC_STEP))
return;
}

// Set item
Expand All @@ -94,13 +97,15 @@ sqlite3_stmt * __attribute__((pure)) get_sqlite3_stmt_vec(sqlite3_stmt_vec *v, u
{
log_err("Passed NULL vector to get_sqlite3_stmt_vec(%p, %u)",
v, index);
return 0;
return NULL;
}

if(index >= v->capacity)
{
// Silently increase size of vector if trying to read out-of-bounds
resize_sqlite3_stmt_vec(v, index + VEC_ALLOC_STEP);
// Return NULL if the allocation fails
if(!resize_sqlite3_stmt_vec(v, index + VEC_ALLOC_STEP))
return NULL;
}

sqlite3_stmt* item = v->items[index];
Expand Down
Loading