Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #68 from jdberry/patch-65

Revised fixes for #65
  • Loading branch information...
commit b7e3e167c843d7998651b99e172c84e7fdf959e5 2 parents 369e51c + c1caf7b
@tenderlove tenderlove authored
Showing with 23 additions and 5 deletions.
  1. +9 −2 ext/sqlite3/database.c
  2. +14 −3 test/test_database.rb
View
11 ext/sqlite3/database.c
@@ -281,9 +281,16 @@ static VALUE sqlite3val2rb(sqlite3_value * val)
case SQLITE_TEXT:
return rb_tainted_str_new2((const char *)sqlite3_value_text(val));
break;
- case SQLITE_BLOB:
- return rb_tainted_str_new2((const char *)sqlite3_value_blob(val));
+ case SQLITE_BLOB: {
+ /* Sqlite warns calling sqlite3_value_bytes may invalidate pointer from sqlite3_value_blob,
+ so we explicitly get the length before getting blob pointer.
+ Note that rb_str_new and rb_tainted_str_new apparently create string with ASCII-8BIT (BINARY) encoding,
+ which is what we want, as blobs are binary
+ */
+ int len = sqlite3_value_bytes(val);
+ return rb_tainted_str_new((const char *)sqlite3_value_blob(val), len);
break;
+ }
case SQLITE_NULL:
return Qnil;
break;
View
17 test/test_database.rb
@@ -22,9 +22,9 @@ def test_bignum
def test_blob
@db.execute("CREATE TABLE blobs ( id INTEGER, hash BLOB(10) )")
- str = "\0foo"
- @db.execute("INSERT INTO blobs VALUES (0, ?)", [str])
- assert_equal [[0, str]], @db.execute("SELECT * FROM blobs")
+ blob = Blob.new("foo\0bar")
+ @db.execute("INSERT INTO blobs VALUES (0, ?)", [blob])
+ assert_equal [[0, blob, blob.length, blob.length*2]], @db.execute("SELECT id, hash, length(hash), length(hex(hash)) FROM blobs")
end
def test_get_first_row
@@ -227,6 +227,17 @@ def test_define_varargs
assert_equal [2.2, 'foo', nil], called_with
end
+ def test_call_func_blob
+ called_with = nil
+ @db.define_function("hello") do |a, b, c|
+ called_with = [a, b, c]
+ nil
+ end
+ blob = Blob.new("a\0fine\0kettle\0of\0fish")
+ @db.execute("select hello(?, length(?), length(?))", [blob, blob, blob])
+ assert_equal [blob, blob.length, 21], called_with
+ end
+
def test_function_return
@db.define_function("hello") { |a| 10 }
assert_equal [10], @db.execute("select hello('world')").first
Please sign in to comment.
Something went wrong with that request. Please try again.