Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
cbor-gen
Commits
f6390fed
Commit
f6390fed
authored
Aug 04, 2020
by
Steven Allen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make ScanForLinks non-recursive
This way, we can't blow out our stack.
parent
cdf4113c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
52 deletions
+40
-52
utils.go
utils.go
+40
-52
No files found.
utils.go
View file @
f6390fed
...
...
@@ -17,67 +17,55 @@ const maxCidLength = 100
const
maxHeaderSize
=
9
func
ScanForLinks
(
br
io
.
Reader
,
cb
func
(
cid
.
Cid
))
error
{
buf
:=
make
([]
byte
,
maxCidLength
)
return
scanForLinksRec
(
br
,
cb
,
buf
)
}
func
scanForLinksRec
(
br
io
.
Reader
,
cb
func
(
cid
.
Cid
),
scratch
[]
byte
)
error
{
maj
,
extra
,
err
:=
CborReadHeaderBuf
(
br
,
scratch
)
if
err
!=
nil
{
return
err
}
switch
maj
{
case
MajUnsignedInt
,
MajNegativeInt
,
MajOther
:
case
MajByteString
,
MajTextString
:
_
,
err
:=
io
.
CopyN
(
ioutil
.
Discard
,
br
,
int64
(
extra
))
scratch
:=
make
([]
byte
,
maxCidLength
)
for
remaining
:=
uint64
(
1
);
remaining
>
0
;
remaining
--
{
maj
,
extra
,
err
:=
CborReadHeaderBuf
(
br
,
scratch
)
if
err
!=
nil
{
return
err
}
case
MajTag
:
if
extra
==
42
{
maj
,
extra
,
err
=
CborReadHeaderBuf
(
br
,
scratch
)
if
err
!=
nil
{
return
err
}
if
maj
!=
MajByteString
{
return
fmt
.
Errorf
(
"expected cbor type 'byte string' in input"
)
}
if
extra
>
maxCidLength
{
return
fmt
.
Errorf
(
"string in cbor input too long"
)
}
if
_
,
err
:=
io
.
ReadAtLeast
(
br
,
scratch
[
:
extra
],
int
(
extra
));
err
!=
nil
{
return
err
}
c
,
err
:=
cid
.
Cast
(
scratch
[
1
:
extra
])
switch
maj
{
case
MajUnsignedInt
,
MajNegativeInt
,
MajOther
:
case
MajByteString
,
MajTextString
:
_
,
err
:=
io
.
CopyN
(
ioutil
.
Discard
,
br
,
int64
(
extra
))
if
err
!=
nil
{
return
err
}
cb
(
c
)
}
else
{
if
err
:=
scanForLinksRec
(
br
,
cb
,
scratch
);
err
!=
nil
{
return
err
}
}
case
MajArray
:
for
i
:=
0
;
i
<
int
(
extra
);
i
++
{
if
err
:=
scanForLinksRec
(
br
,
cb
,
scratch
);
err
!=
nil
{
return
err
}
}
case
MajMap
:
for
i
:=
0
;
i
<
int
(
extra
*
2
);
i
++
{
if
err
:=
scanForLinksRec
(
br
,
cb
,
scratch
);
err
!=
nil
{
return
err
case
MajTag
:
if
extra
==
42
{
maj
,
extra
,
err
=
CborReadHeaderBuf
(
br
,
scratch
)
if
err
!=
nil
{
return
err
}
if
maj
!=
MajByteString
{
return
fmt
.
Errorf
(
"expected cbor type 'byte string' in input"
)
}
if
extra
>
maxCidLength
{
return
fmt
.
Errorf
(
"string in cbor input too long"
)
}
if
_
,
err
:=
io
.
ReadAtLeast
(
br
,
scratch
[
:
extra
],
int
(
extra
));
err
!=
nil
{
return
err
}
c
,
err
:=
cid
.
Cast
(
scratch
[
1
:
extra
])
if
err
!=
nil
{
return
err
}
cb
(
c
)
}
else
{
remaining
++
}
case
MajArray
:
remaining
+=
extra
case
MajMap
:
remaining
+=
(
extra
*
2
)
default
:
return
fmt
.
Errorf
(
"unhandled cbor type: %d"
,
maj
)
}
default
:
return
fmt
.
Errorf
(
"unhandled cbor type: %d"
,
maj
)
}
return
nil
}
...
...
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