You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/07/11 13:14:15 UTC
[avro] 01/01: Memory handling improvements
This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch memory-handling-improvements
in repository https://gitbox.apache.org/repos/asf/avro.git
commit c9f8a676c9fe1c2d643c18d35dba981b811ecb4f
Author: Evan Richter <ev...@gmail.com>
AuthorDate: Sun Jul 10 11:04:43 2022 -0500
Memory handling improvements
Return errors instead of panicing when the read bytes is not as expected
---
lang/rust/avro/src/decode.rs | 2 +-
lang/rust/avro/src/reader.rs | 6 +++++-
lang/rust/avro/src/util.rs | 5 +++--
3 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/lang/rust/avro/src/decode.rs b/lang/rust/avro/src/decode.rs
index 6017d9165..cb32ebdb7 100644
--- a/lang/rust/avro/src/decode.rs
+++ b/lang/rust/avro/src/decode.rs
@@ -59,7 +59,7 @@ fn decode_seq_len<R: Read>(reader: &mut R) -> AvroResult<usize> {
std::cmp::Ordering::Equal => return Ok(0),
std::cmp::Ordering::Less => {
let _size = zag_i64(reader)?;
- -raw_len
+ raw_len.checked_neg().ok_or(Error::IntegerOverflow)?
}
std::cmp::Ordering::Greater => raw_len,
})
diff --git a/lang/rust/avro/src/reader.rs b/lang/rust/avro/src/reader.rs
index 193449e12..de68c18da 100644
--- a/lang/rust/avro/src/reader.rs
+++ b/lang/rust/avro/src/reader.rs
@@ -114,7 +114,7 @@ impl<R: Read> Block<R> {
// We need to resize to ensure that the buffer len is safe to read `n` elements.
//
// TODO: Figure out a way to avoid having to truncate for the second case.
- self.buf.resize(n, 0);
+ self.buf.resize(util::safe_len(n)?, 0);
self.reader
.read_exact(&mut self.buf)
.map_err(Error::ReadIntoBuf)?;
@@ -179,6 +179,10 @@ impl<R: Read> Block<R> {
let mut block_bytes = &self.buf[self.buf_idx..];
let b_original = block_bytes.len();
let item = from_avro_datum(&self.writer_schema, &mut block_bytes, read_schema)?;
+ if b_original == block_bytes.len() {
+ // from_avro_datum did not consume any bytes, so return an error to avoid an infinite loop
+ return Err(Error::ReadBlock);
+ }
self.buf_idx += b_original - block_bytes.len();
self.message_count -= 1;
Ok(Some(item))
diff --git a/lang/rust/avro/src/util.rs b/lang/rust/avro/src/util.rs
index 5929c06dd..e18b5641f 100644
--- a/lang/rust/avro/src/util.rs
+++ b/lang/rust/avro/src/util.rs
@@ -23,7 +23,8 @@ use std::{convert::TryFrom, i64, io::Read, sync::Once};
/// Avro-encoded values. This is a protection against ill-formed
/// data, whose length field might be interpreted as enormous.
/// See max_allocation_bytes to change this limit.
-pub static mut MAX_ALLOCATION_BYTES: usize = 512 * 1024 * 1024;
+pub const DEFAULT_MAX_ALLOCATION_BYTES: usize = 512 * 1024 * 1024;
+static mut MAX_ALLOCATION_BYTES: usize = DEFAULT_MAX_ALLOCATION_BYTES;
static MAX_ALLOCATION_BYTES_ONCE: Once = Once::new();
pub trait MapHelper {
@@ -140,7 +141,7 @@ pub fn max_allocation_bytes(num_bytes: usize) -> usize {
}
pub fn safe_len(len: usize) -> AvroResult<usize> {
- let max_bytes = max_allocation_bytes(512 * 1024 * 1024);
+ let max_bytes = max_allocation_bytes(DEFAULT_MAX_ALLOCATION_BYTES);
if len <= max_bytes {
Ok(len)