Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
polkadot-sdk
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
parity
Mirrored projects
polkadot-sdk
Commits
a6c95cbb
Commit
a6c95cbb
authored
2 years ago
by
Shawn Tabrizi
Committed by
GitHub
2 years ago
Browse files
Options
Downloads
Patches
Plain Diff
add decode with depth limit to opaque types (#11947)
Co-authored-by: parity-processbot <>
parent
98252cb9
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
substrate/Cargo.lock
+1
-0
1 addition, 0 deletions
substrate/Cargo.lock
substrate/frame/support/Cargo.toml
+2
-0
2 additions, 0 deletions
substrate/frame/support/Cargo.toml
substrate/frame/support/src/traits/misc.rs
+30
-3
30 additions, 3 deletions
substrate/frame/support/src/traits/misc.rs
with
33 additions
and
3 deletions
substrate/Cargo.lock
+
1
−
0
View file @
a6c95cbb
...
...
@@ -2250,6 +2250,7 @@ dependencies = [
"serde",
"serde_json",
"smallvec",
"sp-api",
"sp-arithmetic",
"sp-core",
"sp-core-hashing-proc-macro",
...
...
This diff is collapsed.
Click to expand it.
substrate/frame/support/Cargo.toml
+
2
−
0
View file @
a6c95cbb
...
...
@@ -17,6 +17,7 @@ serde = { version = "1.0.136", optional = true, features = ["derive"] }
codec
=
{
package
=
"parity-scale-codec"
,
version
=
"3.0.0"
,
default-features
=
false
,
features
=
[
"derive"
,
"max-encoded-len"
]
}
scale-info
=
{
version
=
"2.1.1"
,
default-features
=
false
,
features
=
[
"derive"
]
}
frame-metadata
=
{
version
=
"15.0.0"
,
default-features
=
false
,
features
=
[
"v14"
]
}
sp-api
=
{
version
=
"4.0.0-dev"
,
default-features
=
false
,
path
=
"../../primitives/api"
}
sp-std
=
{
version
=
"4.0.0"
,
default-features
=
false
,
path
=
"../../primitives/std"
}
sp-io
=
{
version
=
"6.0.0"
,
default-features
=
false
,
path
=
"../../primitives/io"
}
sp-runtime
=
{
version
=
"6.0.0"
,
default-features
=
false
,
path
=
"../../primitives/runtime"
}
...
...
@@ -49,6 +50,7 @@ default = ["std"]
std
=
[
"once_cell"
,
"serde"
,
"sp-api/std"
,
"sp-io/std"
,
"codec/std"
,
"scale-info/std"
,
...
...
This diff is collapsed.
Click to expand it.
substrate/frame/support/src/traits/misc.rs
+
30
−
3
View file @
a6c95cbb
...
...
@@ -18,7 +18,7 @@
//! Smaller traits used in FRAME which don't need their own file.
use
crate
::
dispatch
::
Parameter
;
use
codec
::{
CompactLen
,
Decode
,
Decode
All
,
Encode
,
EncodeLike
,
Input
,
MaxEncodedLen
};
use
codec
::{
CompactLen
,
Decode
,
Decode
Limit
,
Encode
,
EncodeLike
,
Input
,
MaxEncodedLen
};
use
scale_info
::{
build
::
Fields
,
meta_type
,
Path
,
Type
,
TypeInfo
,
TypeParameter
};
use
sp_arithmetic
::
traits
::{
CheckedAdd
,
CheckedMul
,
CheckedSub
,
Saturating
};
#[doc(hidden)]
...
...
@@ -745,7 +745,10 @@ impl<T: Encode> Encode for WrapperOpaque<T> {
impl
<
T
:
Decode
>
Decode
for
WrapperOpaque
<
T
>
{
fn
decode
<
I
:
Input
>
(
input
:
&
mut
I
)
->
Result
<
Self
,
codec
::
Error
>
{
Ok
(
Self
(
T
::
decode_all
(
&
mut
&<
Vec
<
u8
>>
::
decode
(
input
)
?
[
..
])
?
))
Ok
(
Self
(
T
::
decode_all_with_depth_limit
(
sp_api
::
MAX_EXTRINSIC_DEPTH
,
&
mut
&<
Vec
<
u8
>>
::
decode
(
input
)
?
[
..
],
)
?
))
}
fn
skip
<
I
:
Input
>
(
input
:
&
mut
I
)
->
Result
<
(),
codec
::
Error
>
{
...
...
@@ -807,7 +810,7 @@ impl<T: Decode> WrapperKeepOpaque<T> {
///
/// Returns `None` if the decoding failed.
pub
fn
try_decode
(
&
self
)
->
Option
<
T
>
{
T
::
decode_all
(
&
mut
&
self
.data
[
..
])
.ok
()
T
::
decode_all
_with_depth_limit
(
sp_api
::
MAX_EXTRINSIC_DEPTH
,
&
mut
&
self
.data
[
..
])
.ok
()
}
/// Returns the length of the encoded `T`.
...
...
@@ -939,6 +942,30 @@ impl<Hash> PreimageRecipient<Hash> for () {
#[cfg(test)]
mod
test
{
use
super
::
*
;
use
sp_std
::
marker
::
PhantomData
;
#[derive(Encode,
Decode)]
enum
NestedType
{
Nested
(
Box
<
Self
>
),
Done
,
}
#[test]
fn
test_opaque_wrapper_decode_limit
()
{
let
limit
=
sp_api
::
MAX_EXTRINSIC_DEPTH
as
usize
;
let
mut
ok_bytes
=
vec!
[
0u8
;
limit
];
ok_bytes
.push
(
1u8
);
let
mut
err_bytes
=
vec!
[
0u8
;
limit
+
1
];
err_bytes
.push
(
1u8
);
assert!
(
<
WrapperOpaque
<
NestedType
>>
::
decode
(
&
mut
&
ok_bytes
.encode
()[
..
])
.is_ok
());
assert!
(
<
WrapperOpaque
<
NestedType
>>
::
decode
(
&
mut
&
err_bytes
.encode
()[
..
])
.is_err
());
let
ok_keep_opaque
=
WrapperKeepOpaque
{
data
:
ok_bytes
,
_phantom
:
PhantomData
};
let
err_keep_opaque
=
WrapperKeepOpaque
{
data
:
err_bytes
,
_phantom
:
PhantomData
};
assert!
(
<
WrapperKeepOpaque
<
NestedType
>>
::
try_decode
(
&
ok_keep_opaque
)
.is_some
());
assert!
(
<
WrapperKeepOpaque
<
NestedType
>>
::
try_decode
(
&
err_keep_opaque
)
.is_none
());
}
#[test]
fn
test_opaque_wrapper
()
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment