summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-09-10 11:09:56 -0400
committerJesse Luehrs <doy@tozt.net>2014-09-10 11:09:56 -0400
commitb994081e521cc07e57518cd974a62a7a9a180759 (patch)
treed893abb3e7076f2afc857379b1888a548a7964e6
parented7945948ed9effdc26b31edfe27799a9f6abc0d (diff)
downloadrust-irc-b994081e521cc07e57518cd974a62a7a9a180759.tar.gz
rust-irc-b994081e521cc07e57518cd974a62a7a9a180759.zip
also only read 512 bytes when reading messages
-rw-r--r--src/client.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/client.rs b/src/client.rs
index c8f0d7b..a971bcd 100644
--- a/src/client.rs
+++ b/src/client.rs
@@ -43,6 +43,7 @@ use constants::{
Ison,
RawCommand,
Reply,
+ MAX_MESSAGE_LENGTH,
};
use message::Message;
@@ -142,16 +143,23 @@ impl Client {
// \n isn't valid inside a message, so this should be fine. if the \n
// we find isn't preceded by a \r, this will be caught by the message
// parser.
- // XXX we should only be reading 512 bytes here, and throwing an error
- // otherwise - or else we could end up reading an unbounded amount of
- // data into memory
- let buf = match self.conn().read_until(b'\n') {
- Ok(b) => b,
- Err(e) => return Err(IoError(e)),
- };
+ let mut buf = [0, ..MAX_MESSAGE_LENGTH];
+ let mut len = 0;
+ for (res, i) in self.conn().bytes().zip(range(0, MAX_MESSAGE_LENGTH - 1)) {
+ match res {
+ Ok(b) => {
+ buf[i] = b;
+ if b == b'\n' {
+ len = i + 1;
+ break;
+ }
+ },
+ Err(e) => return Err(IoError(e)),
+ }
+ }
// XXX handle different encodings
- match Message::parse(String::from_utf8_lossy(buf.as_slice()).as_slice()) {
+ match Message::parse(String::from_utf8_lossy(buf.slice(0, len)).as_slice()) {
Ok(m) => Ok(m),
Err(s) => Err(ParseError(s)),
}