Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"code": "incoming_too_long",
"group": "message",
"message": "Incoming message too long."
"message": "Incoming message too long"
}
23 changes: 14 additions & 9 deletions rivetkit-rust/packages/rivetkit-sqlite/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,15 @@ fn bind_params(
BindParam::Null => unsafe { sqlite3_bind_null(stmt, bind_index) },
BindParam::Integer(value) => unsafe { sqlite3_bind_int64(stmt, bind_index, *value) },
BindParam::Float(value) => unsafe { sqlite3_bind_double(stmt, bind_index, *value) },
BindParam::Text(value) => {
let text = CString::new(value.as_str()).map_err(|err| anyhow!(err.to_string()))?;
unsafe {
sqlite3_bind_text(stmt, bind_index, text.as_ptr(), -1, SQLITE_TRANSIENT())
}
}
BindParam::Text(value) => unsafe {
sqlite3_bind_text(
stmt,
bind_index,
value.as_ptr() as *const c_char,
value.len() as i32,
SQLITE_TRANSIENT(),
)
},
BindParam::Blob(value) => unsafe {
sqlite3_bind_blob(
stmt,
Expand Down Expand Up @@ -258,9 +261,11 @@ fn column_value(stmt: *mut libsqlite3_sys::sqlite3_stmt, index: i32) -> ColumnVa
if text_ptr.is_null() {
ColumnValue::Null
} else {
let text = unsafe { CStr::from_ptr(text_ptr as *const c_char) }
.to_string_lossy()
.into_owned();
let text_len = unsafe { sqlite3_column_bytes(stmt, index) } as usize;
let text = String::from_utf8_lossy(unsafe {
std::slice::from_raw_parts(text_ptr as *const u8, text_len)
})
.into_owned();
ColumnValue::Text(text)
}
}
Expand Down
64 changes: 64 additions & 0 deletions rivetkit-rust/packages/rivetkit-sqlite/tests/query_text_nul.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use std::ffi::CString;
use std::ptr;

use libsqlite3_sys::{SQLITE_OK, sqlite3, sqlite3_close, sqlite3_open};
use rivetkit_sqlite::query::{
BindParam, ColumnValue, exec_statements, execute_statement, query_statement,
};

struct MemoryDb(*mut sqlite3);

impl MemoryDb {
fn open() -> Self {
let name = CString::new(":memory:").unwrap();
let mut db = ptr::null_mut();
let rc = unsafe { sqlite3_open(name.as_ptr(), &mut db) };
assert_eq!(rc, SQLITE_OK);
Self(db)
}

fn as_ptr(&self) -> *mut sqlite3 {
self.0
}
}

impl Drop for MemoryDb {
fn drop(&mut self) {
unsafe {
sqlite3_close(self.0);
}
}
}

#[test]
fn text_with_embedded_nul_round_trips() {
let db = MemoryDb::open();
exec_statements(
db.as_ptr(),
"CREATE TABLE items(id INTEGER PRIMARY KEY, label TEXT);",
)
.unwrap();

execute_statement(
db.as_ptr(),
"INSERT INTO items(label) VALUES (?);",
Some(&[BindParam::Text("a\0b".to_owned())]),
)
.unwrap();

let rows = query_statement(
db.as_ptr(),
"SELECT label, hex(label), length(label) FROM items;",
None,
)
.unwrap();

assert_eq!(
rows.rows,
vec![vec![
ColumnValue::Text("a\0b".to_owned()),
ColumnValue::Text("610062".to_owned()),
ColumnValue::Integer(1),
]]
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ export const dbActorRaw = actor({
);
return { id: results[0].id };
},
insertValueAndReadBack: async (c, value: string) => {
await c.db.execute(
"INSERT INTO test_data (value, payload, created_at) VALUES (?, ?, ?)",
value,
"",
Date.now(),
);
const inserted = await c.db.execute<{
id: number;
value: string;
hex_value: string;
sqlite_length: number;
}>(
`SELECT id, value, hex(value) as hex_value, length(value) as sqlite_length FROM test_data ORDER BY id DESC LIMIT 1`,
);
return inserted[0] ?? null;
},
getValues: async (c) => {
const results = await c.db.execute<{
id: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ describeDriverMatrix("Actor Db Raw", (driverTestConfig) => {
expect(values[1].value).toBe("Bob");
});

test("round-trips text containing an embedded nul byte", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);

const instance = client.dbActorRaw.getOrCreate(["nul-text"]);
const input = "a\0b";

const row = await instance.insertValueAndReadBack(input);

expect(row).not.toBeNull();
expect(row!.value).toBe(input);
expect(row!.hex_value).toBe("610062");
expect(row!.sqlite_length).toBe(1);
});

test("persists data across actor instances", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);

Expand Down
Loading