diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-04-01 12:49:10 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-04-01 13:02:05 +0200 |
commit | ffb9174f86581e006904262265e1abd3fdf7ff54 (patch) | |
tree | 517e4ce3fa05e742cb767cece2c46369a6472f4f | |
parent | 9e02f2654169690f82348fbf11a620d6f2fa353e (diff) |
openpgp: Fix worst-case estimate of compressed data size.
-rw-r--r-- | openpgp/src/serialize/mod.rs | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/openpgp/src/serialize/mod.rs b/openpgp/src/serialize/mod.rs index e514a2c1..7049f421 100644 --- a/openpgp/src/serialize/mod.rs +++ b/openpgp/src/serialize/mod.rs @@ -382,6 +382,8 @@ fn generic_serialize_into<T: Marshal + MarshalInto>(o: &T, buf: &mut [u8]) false }; return if short_write { + assert!(buf_len < o.serialized_len(), + "o.serialized_len() underestimated the required space"); Err(Error::InvalidArgument( format!("Invalid buffer size, expected {}, got {}", o.serialized_len(), buf_len)).into()) @@ -412,6 +414,8 @@ fn generic_export_into<T: Marshal + MarshalInto>(o: &T, buf: &mut [u8]) false }; return if short_write { + assert!(buf_len < o.serialized_len(), + "o.serialized_len() underestimated the required space"); Err(Error::InvalidArgument( format!("Invalid buffer size, expected {}, got {}", o.serialized_len(), buf_len)).into()) @@ -1971,7 +1975,10 @@ impl Marshal for CompressedData { impl NetLength for CompressedData { fn net_len(&self) -> usize { // Worst case, the data gets larger. Account for that. - let compressed = |l| l + cmp::max(l / 2, 128); + // Experiments suggest that the overhead of compressing random + // data is worse for BZIP2, but it converges to 20% starting + // at ~2k of random data. + let compressed = |l| l + cmp::max(l / 5, 4096); match self.body() { Body::Unprocessed(bytes) => 1 /* Algo */ + bytes.len(), |