Skip to content

Commit

Permalink
fix: invalid allocation requests
Browse files Browse the repository at this point in the history
Invalid data can lead to allocating too much memory. Only preallocate up
to 1024 `Value` objects.

Fixes: #115

Signed-off-by: Ahmed Charles <[email protected]>
  • Loading branch information
ahmedcharles authored and rjzak committed Feb 26, 2024
1 parent 88cf117 commit e305f1c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions ciborium/src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::{Error, Integer, Value};

use alloc::{boxed::Box, string::String, vec::Vec};

Check warning on line 5 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly debug ciborium std

the item `Box` is imported redundantly

Check warning on line 5 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly debug ciborium std

the item `String` is imported redundantly

Check warning on line 5 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly release ciborium std

the item `Box` is imported redundantly

Check warning on line 5 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly release ciborium std

the item `String` is imported redundantly
use core::iter::Peekable;
use core::{iter::Peekable, mem::size_of};

Check failure on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / cargo clippy

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test beta debug ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 debug ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test beta debug ciborium std

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test beta release ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 debug ciborium std

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 release ciborium std

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test beta release ciborium std

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test 1.70.0 release ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly debug ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test nightly release ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test stable debug ciborium std

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test stable release ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test stable debug ciborium

unused import: `mem::size_of`

Check warning on line 6 in ciborium/src/value/de.rs

View workflow job for this annotation

GitHub Actions / test stable release ciborium std

unused import: `mem::size_of`

use ciborium_ll::tag;
use serde::de::{self, Deserializer as _};
Expand Down Expand Up @@ -124,7 +124,9 @@ impl<'de> serde::de::Visitor<'de> for Visitor {

#[inline]
fn visit_map<A: de::MapAccess<'de>>(self, mut acc: A) -> Result<Self::Value, A::Error> {
let mut map = Vec::<(Value, Value)>::with_capacity(acc.size_hint().unwrap_or(0));
let mut map = Vec::<(Value, Value)>::with_capacity(
acc.size_hint().filter(|&l| l < 1024).unwrap_or(0),
);

while let Some(kv) = acc.next_entry()? {
map.push(kv);
Expand Down

0 comments on commit e305f1c

Please sign in to comment.