-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make zero-sized value counting more fine grained #516
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -256,10 +256,7 @@ impl<'de> Deserializer<'de> { | |||||||||
gamma: Gamma::default(), | ||||||||||
field_name: None, | ||||||||||
is_untyped: false, | ||||||||||
#[cfg(not(target_arch = "wasm32"))] | ||||||||||
zero_sized_values: 2_000_000, | ||||||||||
#[cfg(target_arch = "wasm32")] | ||||||||||
zero_sized_values: 0, | ||||||||||
#[cfg(not(target_arch = "wasm32"))] | ||||||||||
full_error_message: true, | ||||||||||
#[cfg(target_arch = "wasm32")] | ||||||||||
|
@@ -337,16 +334,13 @@ impl<'de> Deserializer<'de> { | |||||||||
} | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
fn is_zero_sized_type(&self, t: &Type) -> bool { | ||||||||||
match t.as_ref() { | ||||||||||
TypeInner::Null | TypeInner::Reserved => true, | ||||||||||
TypeInner::Record(fs) => fs.iter().all(|f| { | ||||||||||
let t = self.table.trace_type(&f.ty).unwrap(); | ||||||||||
// recursive records have been replaced with empty already, it's safe to call without memoization. | ||||||||||
self.is_zero_sized_type(&t) | ||||||||||
}), | ||||||||||
_ => false, | ||||||||||
fn dec_zero_sized_value(&mut self) -> Result<()> { | ||||||||||
//eprintln!("{}", self.zero_sized_values); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this comment be dropped? |
||||||||||
if self.zero_sized_values == 0 { | ||||||||||
return Err(Error::msg("Too many zero sized values")); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Suggested change
|
||||||||||
} | ||||||||||
self.zero_sized_values -= 1; | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
// Should always call set_field_name to set the field_name. After deserialize_identifier | ||||||||||
// processed the field_name, field_name will be reset to None. | ||||||||||
|
@@ -413,6 +407,7 @@ impl<'de> Deserializer<'de> { | |||||||||
where | ||||||||||
V: Visitor<'de>, | ||||||||||
{ | ||||||||||
self.dec_zero_sized_value()?; | ||||||||||
let bytes = vec![3u8]; | ||||||||||
visitor.visit_byte_buf(bytes) | ||||||||||
} | ||||||||||
|
@@ -655,6 +650,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | |||||||||
&& matches!(*self.wire_type, TypeInner::Null | TypeInner::Reserved), | ||||||||||
"unit" | ||||||||||
); | ||||||||||
self.dec_zero_sized_value()?; | ||||||||||
visitor.visit_unit() | ||||||||||
} | ||||||||||
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value> | ||||||||||
|
@@ -715,7 +711,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | |||||||||
{ | ||||||||||
self.unroll_type()?; | ||||||||||
match (self.wire_type.as_ref(), self.expect_type.as_ref()) { | ||||||||||
(TypeInner::Null | TypeInner::Reserved, TypeInner::Opt(_)) => visitor.visit_none(), | ||||||||||
(TypeInner::Null | TypeInner::Reserved, TypeInner::Opt(_)) => { | ||||||||||
self.dec_zero_sized_value()?; | ||||||||||
visitor.visit_none() | ||||||||||
} | ||||||||||
(TypeInner::Opt(t1), TypeInner::Opt(t2)) => { | ||||||||||
self.wire_type = t1.clone(); | ||||||||||
self.expect_type = t2.clone(); | ||||||||||
|
@@ -755,12 +754,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | |||||||||
let expect = e.clone(); | ||||||||||
let wire = self.table.trace_type(w)?; | ||||||||||
let len = Len::read(&mut self.input)?.0; | ||||||||||
if self.is_zero_sized_type(&wire) { | ||||||||||
if self.zero_sized_values < len { | ||||||||||
return Err(Error::msg("vec length of zero sized values too large")); | ||||||||||
} | ||||||||||
self.zero_sized_values -= len; | ||||||||||
} | ||||||||||
visitor.visit_seq(Compound::new(self, Style::Vector { len, expect, wire })) | ||||||||||
} | ||||||||||
(TypeInner::Record(e), TypeInner::Record(w)) => { | ||||||||||
|
@@ -773,6 +766,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | |||||||||
self.wire_type | ||||||||||
))); | ||||||||||
} | ||||||||||
if w.is_empty() { | ||||||||||
self.dec_zero_sized_value()?; | ||||||||||
} | ||||||||||
let value = | ||||||||||
visitor.visit_seq(Compound::new(self, Style::Struct { expect, wire }))?; | ||||||||||
Ok(value) | ||||||||||
|
@@ -879,6 +875,9 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> { | |||||||||
(TypeInner::Record(e), TypeInner::Record(w)) => { | ||||||||||
let expect = e.clone().into(); | ||||||||||
let wire = w.clone().into(); | ||||||||||
if w.is_empty() { | ||||||||||
self.dec_zero_sized_value()?; | ||||||||||
} | ||||||||||
let value = | ||||||||||
visitor.visit_map(Compound::new(self, Style::Struct { expect, wire }))?; | ||||||||||
Ok(value) | ||||||||||
|
@@ -1143,6 +1142,7 @@ impl<'de, 'a> de::VariantAccess<'de> for Compound<'a, 'de> { | |||||||||
*self.de.expect_type == TypeInner::Null && *self.de.wire_type == TypeInner::Null, | ||||||||||
"unit_variant" | ||||||||||
); | ||||||||||
self.de.dec_zero_sized_value()?; | ||||||||||
Ok(()) | ||||||||||
} | ||||||||||
|
||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Candid Specification | ||
|
||
Version: 0.1.7 | ||
Version: 0.1.8 | ||
|
||
Date: Dec 12, 2024 | ||
Date: Jan 25, 2024 | ||
|
||
## Motivation | ||
|
||
|
@@ -1255,7 +1255,7 @@ M(id(v*) : principal) = i8(1) M(v* : vec nat8) | |
|
||
Note: | ||
|
||
* Since `null`, `reserved`, `record {}`, and records of such values, take no space, to prevent unbounded sized message, we limit the total vector length of such zero-sized values in a messagev (on the wire) to be 2,000,000 elements. For example, if a message contains two vectors, one at type `vec null` and one at type `vec record {}`, then the length of both vectors combined cannot exceed 2,000,000 elements. | ||
* Since `null`, `reserved` and `record {}` take no space, to prevent unbounded sized message, we limit the total number of such zero-sized values in a message (on the wire) to be 2,000,000 elements. For example, a value of type `record { null; reserved; record { record {} } }` contains 3 zero-sized values; a value of type `vec opt record { null; null }` with length 100 contains at most 200 zero-sized values (it is 200 when the vector contains no `null` values); if a message contains two vectors, one at type `vec null` and one at type `vec record {}`, then the length of both vectors combined cannot exceed 2,000,000 elements. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And in fact also the entire There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will try to rephrase the spec. We only count the occurrences of |
||
|
||
#### References | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conceptually, we should check this condition for every (nested) value and limit all such (nested) zero-sized values at 2M in total.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See above. That would be overcounting, e.g.,
record { null; null }
would be counted as 3 instead of 2.