Skip to content

Commit 56a8082

Browse files
authored
Avoid replying with CR or LF in simple string or error (#200)
1 parent 8bd942b commit 56a8082

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

src/context/mod.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ impl Context {
137137
}
138138
}
139139

140+
pub fn str_as_legal_resp_string(s: &str) -> CString {
141+
CString::new(
142+
s.chars()
143+
.map(|c| match c {
144+
'\r' | '\n' => ' ' as u8,
145+
_ => c as u8,
146+
})
147+
.collect::<Vec<_>>(),
148+
)
149+
.unwrap()
150+
}
151+
152+
pub fn reply_simple_string(&self, s: &str) -> raw::Status {
153+
let msg = Context::str_as_legal_resp_string(s);
154+
unsafe { raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into() }
155+
}
156+
157+
pub fn reply_error_string(&self, s: &str) -> raw::Status {
158+
let msg = Context::str_as_legal_resp_string(s);
159+
unsafe { raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into() }
160+
}
161+
140162
/// # Panics
141163
///
142164
/// Will panic if methods used are missing in redismodule.h
@@ -211,20 +233,13 @@ impl Context {
211233
}
212234
},
213235

214-
Err(RedisError::WrongType) => unsafe {
215-
let msg = CString::new(RedisError::WrongType.to_string()).unwrap();
216-
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
217-
},
236+
Err(RedisError::WrongType) => {
237+
self.reply_error_string(RedisError::WrongType.to_string().as_str())
238+
}
218239

219-
Err(RedisError::String(s)) => unsafe {
220-
let msg = CString::new(s).unwrap();
221-
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
222-
},
240+
Err(RedisError::String(s)) => self.reply_error_string(s.as_str()),
223241

224-
Err(RedisError::Str(s)) => unsafe {
225-
let msg = CString::new(s).unwrap();
226-
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
227-
},
242+
Err(RedisError::Str(s)) => self.reply_error_string(s),
228243
}
229244
}
230245

src/raw.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ extern crate enum_primitive_derive;
66
extern crate libc;
77
extern crate num_traits;
88

9-
use std::ffi::CString;
9+
use std::ffi::{CStr, CString};
1010
use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong};
1111
use std::ptr;
1212
use std::slice;
@@ -18,7 +18,7 @@ use num_traits::FromPrimitive;
1818

1919
use crate::error::Error;
2020
pub use crate::redisraw::bindings::*;
21-
use crate::RedisString;
21+
use crate::{Context, RedisString};
2222
use crate::{RedisBuffer, RedisError};
2323

2424
bitflags! {
@@ -256,7 +256,8 @@ pub fn reply_with_array(ctx: *mut RedisModuleCtx, len: c_long) -> Status {
256256
#[allow(clippy::not_unsafe_ptr_arg_deref)]
257257
pub fn reply_with_error(ctx: *mut RedisModuleCtx, err: *const c_char) {
258258
unsafe {
259-
RedisModule_ReplyWithError.unwrap()(ctx, err);
259+
let msg = Context::str_as_legal_resp_string(CStr::from_ptr(err).to_str().unwrap());
260+
RedisModule_ReplyWithError.unwrap()(ctx, msg.as_ptr());
260261
}
261262
}
262263

0 commit comments

Comments
 (0)