Skip to content

Commit

Permalink
Avoid replying with CR or LF in simple string or error (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
oshadmi authored Dec 9, 2021
1 parent 8bd942b commit 56a8082
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
39 changes: 27 additions & 12 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,28 @@ impl Context {
}
}

pub fn str_as_legal_resp_string(s: &str) -> CString {
CString::new(
s.chars()
.map(|c| match c {
'\r' | '\n' => ' ' as u8,
_ => c as u8,
})
.collect::<Vec<_>>(),
)
.unwrap()
}

pub fn reply_simple_string(&self, s: &str) -> raw::Status {
let msg = Context::str_as_legal_resp_string(s);
unsafe { raw::RedisModule_ReplyWithSimpleString.unwrap()(self.ctx, msg.as_ptr()).into() }
}

pub fn reply_error_string(&self, s: &str) -> raw::Status {
let msg = Context::str_as_legal_resp_string(s);
unsafe { raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into() }
}

/// # Panics
///
/// Will panic if methods used are missing in redismodule.h
Expand Down Expand Up @@ -211,20 +233,13 @@ impl Context {
}
},

Err(RedisError::WrongType) => unsafe {
let msg = CString::new(RedisError::WrongType.to_string()).unwrap();
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
},
Err(RedisError::WrongType) => {
self.reply_error_string(RedisError::WrongType.to_string().as_str())
}

Err(RedisError::String(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
},
Err(RedisError::String(s)) => self.reply_error_string(s.as_str()),

Err(RedisError::Str(s)) => unsafe {
let msg = CString::new(s).unwrap();
raw::RedisModule_ReplyWithError.unwrap()(self.ctx, msg.as_ptr()).into()
},
Err(RedisError::Str(s)) => self.reply_error_string(s),
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern crate enum_primitive_derive;
extern crate libc;
extern crate num_traits;

use std::ffi::CString;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_double, c_int, c_long, c_longlong};
use std::ptr;
use std::slice;
Expand All @@ -18,7 +18,7 @@ use num_traits::FromPrimitive;

use crate::error::Error;
pub use crate::redisraw::bindings::*;
use crate::RedisString;
use crate::{Context, RedisString};
use crate::{RedisBuffer, RedisError};

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

Expand Down

0 comments on commit 56a8082

Please sign in to comment.