You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If the size portion of a serialized Vec gets corrupted, it can lead to postcard expecting the size to be much much larger than it actually is.
Example on Teensy 4.0:
from_bytes([8,255,255,255,0,0,0,0,0]) => /* Vec with capacity of 8 and elements [255, 255, 255, 0, 0, 0, 0, 0] */from_bytes([(1 << 7) | 8,255,255,255,0,0,0,0,0]) => /* Vec with capacity of 268,435,336 and elements ??? */
Serde ends up preventing panics on most devices because it limits the amount of pre-allocated bytes to 1,048,576, but this is still too large for many low-memory embedded devices.
I propose adding the following function to Flavor:
/// Returns the number of bytes remaining in the message, if known.fnsize_hint(&self) -> Option<usize>;
This removes the pre-alloc optimization in cases where postcard expects more elements than there are bytes in the remaining message. This will likely result in reduced performance when the de-optimization condition is met except in the case of zero-sized types. However, it will reduce the likelihood of allocation panics, instead returning Err(Error::DeserializeUnexpectedEnd).
The text was updated successfully, but these errors were encountered:
Yes, this is with alloc Vec. Heapless Vec doesn't attempt this optimization.
Vecs of ZSTs aren't affected because it ends up pre-allocating 0 bytes regardless, but I'll add in some tests anyways.
The optimization code is in the serde crate itself. The only way to go any further here is to either know the size of the remaining byte message in the serde crate context or to know the Vec element type in the SeqAccess or Deserializer contexts. All in all, I think this is the best solution.
If the size portion of a serialized Vec gets corrupted, it can lead to postcard expecting the size to be much much larger than it actually is.
Example on Teensy 4.0:
Serde ends up preventing panics on most devices because it limits the amount of pre-allocated bytes to 1,048,576, but this is still too large for many low-memory embedded devices.
I propose adding the following function to
Flavor
:and using it in
SeqAccess
like so:This removes the pre-alloc optimization in cases where postcard expects more elements than there are bytes in the remaining message. This will likely result in reduced performance when the de-optimization condition is met except in the case of zero-sized types. However, it will reduce the likelihood of allocation panics, instead returning
Err(Error::DeserializeUnexpectedEnd)
.The text was updated successfully, but these errors were encountered: