Is the SQLite3 gem known to have memory corruption?

Running the following produces bizarre results (trimmed down from
an actual app):


require ‘rubygems’
require_gem ‘sqlite3-ruby’

def each_batch(size=1)
until false
batch = []
(1…20).each {|i| batch << “x”}
yield batch

database ="/tmp/test.db")
counter = 0
each_batch {|batch|
database.transaction {
counter += batch.length
print “OK\n”

For a while it prints “OK”, but then eventually:

./test_bad_batch.rb:20: undefined method length' for 4085314:Fixnum (NoMethodError) from /opt/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.1.0/ lib/sqlite3/database.rb:593:intransaction’
from ./test_bad_batch.rb:19
from ./test_bad_batch.rb:11:in each_batch' from ./test_bad_batch.rb:11:ineach_batch’
from ./test_bad_batch.rb:18

That is the ‘batch’ is getting yielded as random pointer instead
of the array generated inside each_batch!

I’ve tried narrowing this further (particularly getting rid of
SQLite usage), but this is the simplest case I can come up with so far.

I’m running on Mac OS X 10.4.4 with ruby 1.8.4 installed via
Darwin Ports (as well as rubygems installed from there and then
sqlite3 installed via that copy of gems).

% /opt/local/bin/ruby -v
ruby 1.8.4 (2005-12-24) [powerpc-darwin8.3.0]

% /opt/local/bin/gem list sqlite3-ruby

*** LOCAL GEMS ***

sqlite3-ruby (1.1.0)
SQLite3/Ruby is a module to allow Ruby scripts to interface with a
SQLite3 database.

I’ve tried both the system supplied sqlite (v3.1.3) and the latest
version (installed via Darwin Ports), 3.3.3.

Any info would be helpful!


Any info would be helpful!
and the patch I submitted, which should fix the problem


Thanks; I’ve applied this patch in my /opt/local/lib/ruby/gems/1.8/

It isn’t clear to me how to get the native code re-applied to the
gem. After patching sqlite3_api.i, I did:

% sudo ruby extconf.rb install sqlite3-ruby

but just reported there being nothing to do and re-running the
test case yielded the same problem.

I’m sorry if this is a basic question, but I’m new to patching
ruby itself. Any idea from the sqlite3 gem provider when this patch
might be included?


Thanks; this patch did in fact fix my issue. Of course, it took
me a while to figure out that I needed swig installed to get your
patch to do anything. Luckily I noticed the SWIG bug in the tracker :slight_smile: