Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
Geth-Modification
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
张蕾
Geth-Modification
Commits
d63e2924
Commit
d63e2924
authored
Apr 01, 2016
by
Jeffrey Wilcke
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
common: added Hash unmarshal json length validation
parent
10d3466c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
0 deletions
+35
-0
types.go
common/types.go
+13
-0
types_test.go
common/types_test.go
+22
-0
No files found.
common/types.go
View file @
d63e2924
...
@@ -19,10 +19,12 @@ package common
...
@@ -19,10 +19,12 @@ package common
import
(
import
(
"encoding/hex"
"encoding/hex"
"encoding/json"
"encoding/json"
"errors"
"fmt"
"fmt"
"math/big"
"math/big"
"math/rand"
"math/rand"
"reflect"
"reflect"
"strings"
)
)
const
(
const
(
...
@@ -30,6 +32,8 @@ const (
...
@@ -30,6 +32,8 @@ const (
AddressLength
=
20
AddressLength
=
20
)
)
var
hashJsonLengthErr
=
errors
.
New
(
"common: unmarshalJSON failed: hash must be exactly 32 bytes"
)
type
(
type
(
Hash
[
HashLength
]
byte
Hash
[
HashLength
]
byte
Address
[
AddressLength
]
byte
Address
[
AddressLength
]
byte
...
@@ -58,6 +62,15 @@ func (h *Hash) UnmarshalJSON(input []byte) error {
...
@@ -58,6 +62,15 @@ func (h *Hash) UnmarshalJSON(input []byte) error {
if
length
>=
2
&&
input
[
0
]
==
'"'
&&
input
[
length
-
1
]
==
'"'
{
if
length
>=
2
&&
input
[
0
]
==
'"'
&&
input
[
length
-
1
]
==
'"'
{
input
=
input
[
1
:
length
-
1
]
input
=
input
[
1
:
length
-
1
]
}
}
// strip "0x" for length check
if
len
(
input
)
>
1
&&
strings
.
ToLower
(
string
(
input
[
:
2
]))
==
"0x"
{
input
=
input
[
2
:
]
}
// validate the length of the input hash
if
len
(
input
)
!=
HashLength
*
2
{
return
hashJsonLengthErr
}
h
.
SetBytes
(
FromHex
(
string
(
input
)))
h
.
SetBytes
(
FromHex
(
string
(
input
)))
return
nil
return
nil
}
}
...
...
common/types_test.go
View file @
d63e2924
...
@@ -29,3 +29,25 @@ func TestBytesConversion(t *testing.T) {
...
@@ -29,3 +29,25 @@ func TestBytesConversion(t *testing.T) {
t
.
Errorf
(
"expected %x got %x"
,
exp
,
hash
)
t
.
Errorf
(
"expected %x got %x"
,
exp
,
hash
)
}
}
}
}
func
TestHashJsonValidation
(
t
*
testing
.
T
)
{
var
h
Hash
var
tests
=
[]
struct
{
Prefix
string
Size
int
Error
error
}{
{
""
,
2
,
hashJsonLengthErr
},
{
""
,
62
,
hashJsonLengthErr
},
{
""
,
66
,
hashJsonLengthErr
},
{
""
,
65
,
hashJsonLengthErr
},
{
"0X"
,
64
,
nil
},
{
"0x"
,
64
,
nil
},
{
"0x"
,
62
,
hashJsonLengthErr
},
}
for
i
,
test
:=
range
tests
{
if
err
:=
h
.
UnmarshalJSON
(
append
([]
byte
(
test
.
Prefix
),
make
([]
byte
,
test
.
Size
)
...
));
err
!=
test
.
Error
{
t
.
Error
(
i
,
"expected"
,
test
.
Error
,
"got"
,
err
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment