Skip to content

Commit

Permalink
Same limit for serializing and deserializing type tags when in releas… (
Browse files Browse the repository at this point in the history
#15301)

* Same limit for serializing and deserializing type tags when in release mode

* Updated usages of MAX_TYPE_TAG_NESTING constant

* addressing review comments
  • Loading branch information
ziaptos authored Nov 21, 2024
1 parent eeca64b commit 9fa7bae
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions third_party/move/move-core/types/src/language_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ mod tests {
fn test_nested_type_tag_struct_serde() {
let mut type_tags = vec![make_type_tag_struct(TypeTag::U8)];

let limit = MAX_TYPE_TAG_NESTING - 1;
let limit = MAX_TYPE_TAG_NESTING;
while type_tags.len() < limit.into() {
type_tags.push(make_type_tag_struct(type_tags.last().unwrap().clone()));
}
Expand All @@ -403,7 +403,7 @@ mod tests {
fn test_nested_type_tag_vector_serde() {
let mut type_tags = vec![make_type_tag_struct(TypeTag::U8)];

let limit = MAX_TYPE_TAG_NESTING - 1;
let limit = MAX_TYPE_TAG_NESTING;
while type_tags.len() < limit.into() {
type_tags.push(make_type_tag_vector(type_tags.last().unwrap().clone()));
}
Expand Down
2 changes: 1 addition & 1 deletion third_party/move/move-core/types/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl<I: Iterator<Item = Token>> Parser<I> {
}

fn parse_type_tag(&mut self, depth: u8) -> Result<TypeTag> {
if depth >= crate::safe_serialize::MAX_TYPE_TAG_NESTING {
if depth > crate::safe_serialize::MAX_TYPE_TAG_NESTING {
bail!("Exceeded TypeTag nesting limit during parsing: {}", depth);
}

Expand Down
12 changes: 7 additions & 5 deletions third_party/move/move-core/types/src/safe_serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::cell::RefCell;

pub(crate) const MAX_TYPE_TAG_NESTING: u8 = 9;
pub(crate) const MAX_TYPE_TAG_NESTING: u8 = 8;

thread_local! {
static TYPE_TAG_DEPTH: RefCell<u8> = const { RefCell::new(0) };
Expand All @@ -24,10 +24,13 @@ where
{
use serde::ser::Error;

// For testability, we allow to serialize one more level than deserialize.
const MAX_TYPE_TAG_NESTING_WHEN_SERIALIZING: u8 =
MAX_TYPE_TAG_NESTING + if cfg!(test) { 1 } else { 0 };

TYPE_TAG_DEPTH.with(|depth| {
let mut r = depth.borrow_mut();
if *r >= MAX_TYPE_TAG_NESTING {
// for testability, we allow one level more
if *r >= MAX_TYPE_TAG_NESTING_WHEN_SERIALIZING {
return Err(S::Error::custom(
"type tag nesting exceeded during serialization",
));
Expand All @@ -51,8 +54,7 @@ where
use serde::de::Error;
TYPE_TAG_DEPTH.with(|depth| {
let mut r = depth.borrow_mut();
// For testability, we allow to serialize one more level than deserialize.
if *r >= MAX_TYPE_TAG_NESTING - 1 {
if *r >= MAX_TYPE_TAG_NESTING {
return Err(D::Error::custom(
"type tag nesting exceeded during deserialization",
));
Expand Down

0 comments on commit 9fa7bae

Please sign in to comment.