Skip to content

Commit df54ac5

Browse files
committed
Merge branch 'de-bound-checks' into 'master'
zv: Ensure padding bytes exist before indexing into them Closes #59 See merge request zeenix/zbus!140
2 parents 6f347a8 + 736e4f8 commit df54ac5

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

zvariant/src/de.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,13 @@ where
206206
fn parse_padding(&mut self, alignment: usize) -> Result<usize> {
207207
let padding = padding_for_n_bytes(self.abs_pos(), alignment);
208208
if padding > 0 {
209+
if self.pos + padding > self.bytes.len() {
210+
return Err(serde::de::Error::invalid_length(
211+
self.bytes.len(),
212+
&format!(">= {}", self.pos + padding).as_str(),
213+
));
214+
}
215+
209216
for i in 0..padding {
210217
let byte = self.bytes[self.pos + i];
211218
if byte != 0 {

zvariant/src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod tests {
184184
use crate::{to_bytes, to_bytes_fds, to_bytes_for_signature};
185185

186186
use crate::{Array, Dict, EncodingContext as Context};
187-
use crate::{Basic, Type, Value};
187+
use crate::{Basic, Result, Type, Value};
188188
use crate::{Fd, ObjectPath, Signature, Str, Structure};
189189

190190
// Test through both generic and specific API (wrt byte order)
@@ -871,4 +871,13 @@ mod tests {
871871
let f: Foo = from_slice_fds(&encoded, None, ctxt).unwrap();
872872
assert_eq!(f, foo);
873873
}
874+
875+
#[test]
876+
fn issue_59() {
877+
// Ensure we don't panic on deserializing tuple of smaller than expected length.
878+
let ctxt = Context::<LE>::new_dbus(0);
879+
let (encoded, _) = to_bytes_fds(ctxt, &("hello",)).unwrap();
880+
let result: Result<(&str, &str)> = from_slice(&encoded, ctxt);
881+
assert!(result.is_err());
882+
}
874883
}

0 commit comments

Comments
 (0)