Skip to content

Commit

Permalink
return EOF error kind when Decoder.decode read len is 0, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
0xzen committed Aug 19, 2022
1 parent 107d804 commit c14d021
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "resp"
version = "1.0.2"
version = "1.0.3"
authors = ["Qing Yan <[email protected]>"]
description = "RESP(REdis Serialization Protocol) Serialization for Rust."
documentation = "https://docs.rs/resp"
Expand All @@ -11,4 +11,4 @@ keywords = ["redis", "resp", "serialization"]
license = "MIT/Apache-2.0"

[dev-dependencies]
rand = "0.3"
rand = "0.8"
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MIT/Apache-2.0

Copyright (c) 2016-2017 IORust Developers
Copyright (c) 2016-2022 IORust Developers

See LICENSE-APACHE and LICENSE-MIT.
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016-2017 IORust Developers
Copyright (c) 2016-2022 IORust Developers

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
Expand Down
13 changes: 12 additions & 1 deletion src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ impl<R: Read> Decoder<R> {
self.reader.read_until(b'\n', &mut res)?;

let len = res.len();
if len == 0 {
return Err(Error::new(ErrorKind::UnexpectedEof, "unexpected EOF"));
}
if len < 3 {
return Err(Error::new(ErrorKind::InvalidInput, format!("too short: {}", len)));
}
Expand Down Expand Up @@ -486,7 +489,15 @@ mod tests {
fn struct_decoder_with_invalid_data() {
let buf: &[u8] = &[];
let mut decoder = Decoder::new(BufReader::new(buf));
assert!(decoder.decode().is_err());
let rt = decoder.decode();
assert!(rt.is_err());
assert_eq!(rt.unwrap_err().kind(), ErrorKind::UnexpectedEof);

let buf: &[u8] = &[1, 2];
let mut decoder = Decoder::new(BufReader::new(buf));
let rt = decoder.decode();
assert!(rt.is_err());
assert_eq!(rt.unwrap_err().kind(), ErrorKind::InvalidInput);


let buf = Value::String("OK正".to_string()).encode();
Expand Down
9 changes: 3 additions & 6 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io;
use std::io::{Read, BufReader};
use resp::{Value, encode, encode_slice, Decoder};
use std::{thread, time};
use rand::{StdRng, Rng};
use rand::{thread_rng, Rng};

struct Case {
data: Vec<u8>,
Expand All @@ -22,11 +22,8 @@ struct FakeNetIO {
impl Read for FakeNetIO {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
if self.offset < self.buf.len() {
let mut rng = StdRng::new().unwrap();
let mut n: usize = rng.gen_range(0, std::cmp::min(buf.len(), 1024));
if n == 0 {
n = buf.len();
}
let mut rng = thread_rng();
let mut n: usize = rng.gen_range(1..=std::cmp::min(buf.len(), 1024));
if self.offset + n > self.buf.len() {
n = self.buf.len() - self.offset;
}
Expand Down

0 comments on commit c14d021

Please sign in to comment.