diff --git a/ext/sqlite3/statement.c b/ext/sqlite3/statement.c index 67ede656..d55d0608 100644 --- a/ext/sqlite3/statement.c +++ b/ext/sqlite3/statement.c @@ -295,7 +295,7 @@ bind_param(VALUE self, VALUE key, VALUE value) SQLITE_TRANSIENT ); } else { - if (!UTF8_P(value) || !USASCII_P(value)) { + if (!UTF8_P(value)) { value = rb_str_encode(value, rb_enc_from_encoding(rb_utf8_encoding()), 0, Qnil); } status = sqlite3_bind_text( diff --git a/test/test_statement.rb b/test/test_statement.rb index 7fb5d155..51393bcc 100644 --- a/test/test_statement.rb +++ b/test/test_statement.rb @@ -201,6 +201,26 @@ def test_bind_param_string stmt.close end + def test_bind_invalid_utf8_as_text + ["\xff", "\xe2\x82"].each do |value| + @db.prepare("select typeof(?), ?") do |stmt| + stmt.bind_params(value, value) + type, text = stmt.step + assert_equal "text", type + assert_equal value.bytes, text.bytes + assert_equal Encoding::UTF_8, text.encoding + end + end + end + + def test_bind_invalid_us_ascii_text_raises + @db.prepare("select ?") do |stmt| + assert_raises(Encoding::InvalidByteSequenceError) do + stmt.bind_param(1, "\xff".b.force_encoding(Encoding::US_ASCII)) + end + end + end + def test_bind_param_int stmt = SQLite3::Statement.new(@db, "select ?") stmt.bind_param(1, 10)