Skip to content
This repository was archived by the owner on Jul 11, 2021. It is now read-only.

Commit a1eaeb0

Browse files
committed
fix #66
1 parent 7fde6c0 commit a1eaeb0

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed

redisql_lib/src/community_statement.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,14 @@ pub fn generate_statements(
6868
db: Arc<Mutex<RawConnection>>,
6969
query: &str,
7070
) -> Result<MultiStatement, SQLite3Error> {
71-
let raw_query = CString::new(query.to_string()).unwrap();
71+
let raw_query = match CString::new(query.to_string()) {
72+
Ok(r) => r,
73+
Err(e) => return Err(SQLite3Error{
74+
code: 999,
75+
error_message: "Trying to create a statement with a NULL byte.".to_string(),
76+
error_string: format!("Find NULL byte in position {} while trying to create a statement", e.nul_position()),
77+
}),
78+
};
7279
let mut next_query = raw_query.as_ptr();
7380
let mut stmts = Vec::new();
7481

redisql_lib/src/redis.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -437,20 +437,21 @@ fn parse_args(
437437
Vec::with_capacity(argc as usize);
438438
for i in 0..argc {
439439
let redis_str = unsafe { *argv.offset(i as isize) };
440-
let arg = unsafe { string_ptr_len(redis_str)? };
440+
let arg = string_ptr_len(redis_str)?;
441441
args.push(arg);
442442
}
443443
Ok(args)
444444
}
445445

446-
pub unsafe fn string_ptr_len(
446+
pub fn string_ptr_len(
447447
str: *mut rm::ffi::RedisModuleString,
448448
) -> Result<&'static str, std::str::Utf8Error> {
449449
let mut len = 0;
450-
let base =
450+
let base = unsafe {
451451
rm::ffi::RedisModule_StringPtrLen.unwrap()(str, &mut len)
452-
as *mut u8;
453-
let slice = slice::from_raw_parts(base, len);
452+
as *mut u8
453+
};
454+
let slice = unsafe { slice::from_raw_parts(base, len) };
454455
let s = str::from_utf8(slice)?;
455456
Ok(s.trim_end_matches(char::from(0)))
456457
}

test/correctness/test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,13 @@ def test_newline_after_semicolon_then_query(self):
925925
one = self.exec_naked("REDISQL.EXEC", "NULL", "SELECT 1;\nSELECT 2;")
926926
self.assertEquals(one, [[2]])
927927

928+
class TestWithNullChars(TestRediSQLWithExec):
929+
def test_with_null_at_the_end(self):
930+
with DB(self, 'A'):
931+
self.assertRaises(redis.ResponseError, self.exec_naked, "REDISQL.EXEC", "A", "SELECT \0 1;")
932+
with DB(self, 'B'):
933+
self.assertRaises(redis.ResponseError, self.exec_naked, "REDISQL.EXEC", "A", "SELECT 1;\0")
934+
928935
if __name__ == '__main__':
929936
unittest.main()
930937

0 commit comments

Comments
 (0)