From e8bd74255abab4d84721170fb173804f3a7de212 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 2 Feb 2024 16:58:26 -0500 Subject: [PATCH 1/2] style: format C files --- ext/sqlite3/exception.c | 18 +++++++++--------- ext/sqlite3/exception.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/ext/sqlite3/exception.c b/ext/sqlite3/exception.c index 4631d304..4889fd28 100644 --- a/ext/sqlite3/exception.c +++ b/ext/sqlite3/exception.c @@ -102,16 +102,16 @@ rb_sqlite3_raise(sqlite3 *db, int status) * accepts a sqlite3 error message as the final argument, which will be `sqlite3_free`d */ void -rb_sqlite3_raise_msg(sqlite3 *db, int status, const char* msg) +rb_sqlite3_raise_msg(sqlite3 *db, int status, const char *msg) { - VALUE exception; + VALUE exception; - if (status == SQLITE_OK) { - return; - } + if (status == SQLITE_OK) { + return; + } - exception = rb_exc_new2(rb_path2class("SQLite3::Exception"), msg); - sqlite3_free((void*)msg); - rb_iv_set(exception, "@code", INT2FIX(status)); - rb_exc_raise(exception); + exception = rb_exc_new2(rb_path2class("SQLite3::Exception"), msg); + sqlite3_free((void *)msg); + rb_iv_set(exception, "@code", INT2FIX(status)); + rb_exc_raise(exception); } diff --git a/ext/sqlite3/exception.h b/ext/sqlite3/exception.h index 235694ab..6cb200cf 100644 --- a/ext/sqlite3/exception.h +++ b/ext/sqlite3/exception.h @@ -5,6 +5,6 @@ #define CHECK_MSG(_db, _status, _msg) rb_sqlite3_raise_msg(_db, _status, _msg); void rb_sqlite3_raise(sqlite3 *db, int status); -void rb_sqlite3_raise_msg(sqlite3 *db, int status, const char* msg); +void rb_sqlite3_raise_msg(sqlite3 *db, int status, const char *msg); #endif From 979e8b206a46068ae02de892cb65fb088952f5be Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Fri, 2 Feb 2024 16:59:12 -0500 Subject: [PATCH 2/2] fix: memory leak in Statement#expanded_sql 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(). --- ext/sqlite3/statement.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ext/sqlite3/statement.c b/ext/sqlite3/statement.c index 26285ac8..29bb2779 100644 --- a/ext/sqlite3/statement.c +++ b/ext/sqlite3/statement.c @@ -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