Skip to content

Commit acd19da

Browse files
committed
Merge pull request #179 from tenderlove/rb_apply
use rb_apply so we don't need to maintain two arrays
2 parents 040fc63 + 98944d5 commit acd19da

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

ext/sqlite3/database.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,18 @@ static void set_sqlite3_func_result(sqlite3_context * ctx, VALUE result)
359359
static void rb_sqlite3_func(sqlite3_context * ctx, int argc, sqlite3_value **argv)
360360
{
361361
VALUE callable = (VALUE)sqlite3_user_data(ctx);
362-
VALUE * params = NULL;
363-
VALUE guard = rb_ary_new2(argc); /* Prevent garbage collection of params during call. */
362+
VALUE params = rb_ary_new2(argc);
364363
VALUE result;
365364
int i;
366365

367366
if (argc > 0) {
368-
params = xcalloc((size_t)argc, sizeof(VALUE *));
369367
for(i = 0; i < argc; i++) {
370368
VALUE param = sqlite3val2rb(argv[i]);
371-
rb_ary_push(guard, param);
372-
params[i] = param;
369+
rb_ary_push(params, param);
373370
}
374371
}
375372

376-
result = rb_funcall2(callable, rb_intern("call"), argc, params);
377-
xfree(params);
373+
result = rb_apply(callable, rb_intern("call"), params);
378374

379375
set_sqlite3_func_result(ctx, result);
380376
}

test/test_database.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,11 @@ def test_function_gc_segfault
283283
@db.create_function("bug", -1) { |func, *values| func.result = values.join }
284284
# With a lot of data and a lot of threads, try to induce a GC segfault.
285285
params = Array.new(127, "?" * 28000)
286-
proc = Proc.new { db.execute("select bug(#{Array.new(params.length, "?").join(",")})", params) }
287-
Mutex.new.tap { |m| threads = (0..30).inject([]) { |a| a << Thread.new { m.synchronize { proc.call } } }.each { |thread| thread.join } }
286+
proc = Proc.new {
287+
db.execute("select bug(#{Array.new(params.length, "?").join(",")})", params)
288+
}
289+
m = Mutex.new
290+
threads = 30.times.map { Thread.new { m.synchronize { proc.call } } }.each(&:join)
288291
end
289292

290293
def test_function_return_type_round_trip

0 commit comments

Comments
 (0)