Skip to content

Commit

Permalink
fix: memory leak in Statement#expanded_sql
Browse files Browse the repository at this point in the history
from https://www.sqlite.org/c3ref/expanded_sql.html

> The strings returned by sqlite3_sql(P) and sqlite3_normalized_sql(P)
> are managed by SQLite and are automatically freed when the prepared
> statement is finalized. The string returned by
> sqlite3_expanded_sql(P), on the other hand, is obtained from
> sqlite3_malloc() and must be freed by the application by passing it
> to sqlite3_free().
  • Loading branch information
flavorjones committed Feb 2, 2024
1 parent e8bd742 commit 979e8b2
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ext/sqlite3/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,10 +622,17 @@ static VALUE
get_expanded_sql(VALUE self)
{
sqlite3StmtRubyPtr ctx;
char *expanded_sql;
VALUE rb_expanded_sql;

TypedData_Get_Struct(self, sqlite3StmtRuby, &statement_type, ctx);
REQUIRE_OPEN_STMT(ctx);

return rb_obj_freeze(SQLITE3_UTF8_STR_NEW2(sqlite3_expanded_sql(ctx->st)));
expanded_sql = sqlite3_expanded_sql(ctx->st);
rb_expanded_sql = rb_obj_freeze(SQLITE3_UTF8_STR_NEW2(expanded_sql));
sqlite3_free(expanded_sql);

return rb_expanded_sql;
}

void
Expand Down

0 comments on commit 979e8b2

Please sign in to comment.