From 64b0190a0a5df7abc35368535aa791f192bc65b4 Mon Sep 17 00:00:00 2001 From: Thomas Klemm Date: Fri, 6 Nov 2026 09:58:01 +0000 Subject: [PATCH] Avoid a redundant String when binding UTF-8 text `bind_param` converts text that is neither UTF-16 nor BLOB to UTF-8 before calling `sqlite3_bind_text`, guarded by if (!UTF8_P(value) || !USASCII_P(value)) No string is both UTF-8 and US-ASCII, so the condition is always true, and every UTF-8 string went through `rb_str_encode`. When the source and target encodings match and no flags are given, `rb_str_encode` returns a duplicate without checking the bytes, so the call only allocated a String that SQLite then copied again under `SQLITE_TRANSIENT`. A lookup such as `where(name: names).pluck(:id)` made one of these Strings per name. Convert only when the string is not UTF-8, as the guard did before f9005a2 made it always true. UTF-8 strings, including ones with invalid bytes, reach SQLite with the same bytes as before, and SQLite still takes its own copy, so the caller's string is neither kept nor modified. US-ASCII strings keep their conversion, which raises `Encoding::InvalidByteSequenceError` for bytes above 0x7F; skipping it too would bind those bytes silently. Other encodings convert as before. The tests cover what the new path must keep doing. Invalid UTF-8 still binds as TEXT with its exact bytes, so UTF-8 strings must not be validated or scrubbed on their way to SQLite. Invalid US-ASCII still raises, so its conversion must not be skipped as well. --- ext/sqlite3/statement.c | 2 +- test/test_statement.rb | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) 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)