Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
61cc8728
Commit
61cc8728
authored
Sep 29, 2011
by
Robert Griesemer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/binary: PutX functions require buffer of sufficient size.
R=rsc CC=golang-dev
https://golang.org/cl/5163041
parent
092a211f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
7 additions
and
32 deletions
+7
-32
src/pkg/encoding/binary/varint.go
src/pkg/encoding/binary/varint.go
+5
-19
src/pkg/encoding/binary/varint_test.go
src/pkg/encoding/binary/varint_test.go
+2
-13
No files found.
src/pkg/encoding/binary/varint.go
View file @
61cc8728
...
...
@@ -37,27 +37,15 @@ const (
)
// PutUvarint encodes a uint64 into buf and returns the number of bytes written.
// If the buffer is too small, the result is the negated number of bytes required
// (that is, -PutUvarint(nil, x) is the number of bytes required to encode x).
func
PutUvarint
(
buf
[]
byte
,
x
uint64
)
int
{
var
i
int
for
i
=
range
buf
{
if
x
<
0x80
{
buf
[
i
]
=
byte
(
x
)
return
i
+
1
}
i
:=
0
for
x
>=
0x80
{
buf
[
i
]
=
byte
(
x
)
|
0x80
x
>>=
7
}
// buffer too small; compute number of bytes required
for
x
>=
0x4000
{
x
>>=
2
*
7
i
+=
2
}
if
x
>=
0x80
{
i
++
}
return
-
(
i
+
1
)
buf
[
i
]
=
byte
(
x
)
return
i
+
1
}
// Uvarint decodes a uint64 from buf and returns that value and the
...
...
@@ -85,8 +73,6 @@ func Uvarint(buf []byte) (uint64, int) {
}
// PutVarint encodes an int64 into buf and returns the number of bytes written.
// If the buffer is too small, the result is the negated number of bytes required
// (that is, -PutVarint(nil, x) is the number of bytes required to encode x).
func
PutVarint
(
buf
[]
byte
,
x
int64
)
int
{
ux
:=
uint64
(
x
)
<<
1
if
x
<
0
{
...
...
@@ -115,7 +101,7 @@ func Varint(buf []byte) (int64, int) {
// WriteUvarint encodes x and writes the result to w.
func
WriteUvarint
(
w
io
.
Writer
,
x
uint64
)
os
.
Error
{
var
buf
[
MaxVarintLen64
]
byte
n
:=
PutUvarint
(
buf
[
:
],
x
)
// won't fail
n
:=
PutUvarint
(
buf
[
:
],
x
)
_
,
err
:=
w
.
Write
(
buf
[
0
:
n
])
return
err
}
...
...
src/pkg/encoding/binary/varint_test.go
View file @
61cc8728
...
...
@@ -11,7 +11,8 @@ import (
)
func
testConstant
(
t
*
testing
.
T
,
w
uint
,
max
int
)
{
n
:=
-
PutUvarint
(
nil
,
1
<<
w
-
1
)
var
buf
[
MaxVarintLen64
]
byte
n
:=
PutUvarint
(
buf
[
:
],
1
<<
w
-
1
)
if
n
!=
max
{
t
.
Errorf
(
"MaxVarintLen%d = %d; want %d"
,
w
,
max
,
n
)
}
...
...
@@ -121,18 +122,6 @@ func TestUvarint(t *testing.T) {
}
func
TestBufferTooSmall
(
t
*
testing
.
T
)
{
for
i
:=
0
;
i
<
10
;
i
++
{
buf
:=
make
([]
byte
,
i
)
x
:=
uint64
(
1
)
<<
(
uint
(
i
)
*
7
)
n0
:=
-
i
if
i
==
0
{
n0
=
-
1
// encoding 0 takes one byte
}
if
n
:=
PutUvarint
(
buf
,
x
);
n
!=
n0
{
t
.
Errorf
(
"PutUvarint([%d]byte, %d): got n = %d; want %d"
,
len
(
buf
),
x
,
n
,
n0
)
}
}
buf
:=
[]
byte
{
0x80
,
0x80
,
0x80
,
0x80
}
for
i
:=
0
;
i
<=
len
(
buf
);
i
++
{
buf
:=
buf
[
0
:
i
]
...
...
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