Skip to content

Commit

Permalink
avoid leak in define_aggregator2
Browse files Browse the repository at this point in the history
Call StringValueCStr before xcalloc but last of all calls into ruby.
This way we do not leak memory should name.to_s raise or return a
String with interior null.
  • Loading branch information
jmetter committed Mar 29, 2017
1 parent 387d061 commit c5a01a0
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ext/sqlite3/aggregator.c
Expand Up @@ -271,6 +271,7 @@ rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator)
sqlite3RubyPtr ctx;
int arity, status;
VALUE ruby_name;
const char *name;
aggregator_wrapper_t *aw;

Data_Get_Struct(self, sqlite3Ruby, ctx);
Expand Down Expand Up @@ -299,14 +300,16 @@ rb_sqlite3_define_aggregator2(VALUE self, VALUE aggregator)
#endif
}

name = StringValueCStr(ruby_name);

aw = xcalloc((size_t)1, sizeof(aggregator_wrapper_t));
aw->handler_klass = aggregator;
rb_sqlite3_list_elem_init(&aw->list);
rb_sqlite3_list_head_init(&aw->instances);

status = rb_sqlite3_create_function_v1or2(
ctx->db,
StringValueCStr(ruby_name),
name,
arity,
SQLITE_UTF8,
aw,
Expand Down

0 comments on commit c5a01a0

Please sign in to comment.