Skip to content

Commit f96ef00

Browse files
authored
Merge pull request #26 from loopholelabs/staging
Release v1.1.2
2 parents 1aba1c2 + 1b3efc7 commit f96ef00

File tree

11 files changed

+76
-39
lines changed

11 files changed

+76
-39
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [v1.1.2] - 2023-08-26
11+
12+
### Fixes
13+
14+
- Fixes an issue where decoding certain i32 or i64 values would result in an incorrect value being returned.
15+
1016
## [v1.1.1] - 2023-06-12
1117

1218
### Fixes
@@ -27,6 +33,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2733

2834
- Merging Typescript, Golang, and Rust implementations into a single repository
2935

30-
[unreleased]: https://github.com/loopholelabs/scale/compare/v1.1.1...HEAD
36+
[unreleased]: https://github.com/loopholelabs/scale/compare/v1.1.2...HEAD
37+
[v1.1.2]: https://github.com/loopholelabs/scale/compare/v1.1.2
3138
[v1.1.1]: https://github.com/loopholelabs/scale/compare/v1.1.1
3239
[v1.1.0]: https://github.com/loopholelabs/scale/compare/v1.1.0

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "polyglot_rs"
3-
version = "1.1.1"
3+
version = "1.1.2"
44
edition = "2021"
55
description="A high-performance serialization framework used for encoding and decoding arbitrary datastructures across languages."
66
license = "Apache-2.0"
@@ -26,8 +26,8 @@ byteorder = "1"
2626

2727
[dev-dependencies]
2828
serde = { version = "1.0", features = ["derive"] }
29-
serde_json = "1.0.96"
30-
base64 = "0.21.2"
29+
serde_json = "1.0.105"
30+
base64 = "0.21.3"
3131
num_enum = "0.6.1"
3232

3333
[profile.release]

decode.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,13 @@ func decodeInt32(b []byte) ([]byte, int32, error) {
245245
if i > VarIntLen32 && cb > 1 {
246246
return b, 0, InvalidInt32
247247
}
248-
// End of varint, add the last bits and cast to signed integer
249-
x := int32((ux | uint32(cb)<<s) >> 1)
250-
// Flip the bits if the sign bit is set
248+
// End of varint, add the last bits
249+
ux |= uint32(cb) << s
250+
// Separate value and sign
251+
x := int32(ux >> 1)
252+
// If sign bit is set, negate the number
251253
if ux&1 != 0 {
252-
x = ^x
254+
x = -(x + 1)
253255
}
254256
return b[i+1:], x, nil
255257
}
@@ -271,11 +273,13 @@ func decodeInt64(b []byte) ([]byte, int64, error) {
271273
if i > VarIntLen64 && cb > 1 {
272274
return b, 0, InvalidInt64
273275
}
274-
// End of varint, add the last bits and cast to signed integer
275-
x := int64((ux | uint64(cb)<<s) >> 1)
276-
// Flip the bits if the sign bit is set
276+
// End of varint, add the last bits
277+
ux |= uint64(cb) << s
278+
// Separate value and sign
279+
x := int64(ux >> 1)
280+
// If sign bit is set, negate the number
277281
if ux&1 != 0 {
278-
x = ^x
282+
x = -(x + 1)
279283
}
280284
return b[i+1:], x, nil
281285
}

decode_test.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ func TestDecodeInt32(t *testing.T) {
456456
t.Parallel()
457457

458458
p := NewBuffer()
459-
v := int32(-2147483648)
459+
v := int32(2147483647)
460460
encodeInt32(p, v)
461461

462462
var value int32
@@ -466,6 +466,15 @@ func TestDecodeInt32(t *testing.T) {
466466
assert.Equal(t, v, value)
467467
assert.Equal(t, 0, len(remaining))
468468

469+
v = int32(-2147483647)
470+
p.Reset()
471+
encodeInt32(p, v)
472+
473+
remaining, value, err = decodeInt32(p.Bytes())
474+
assert.NoError(t, err)
475+
assert.Equal(t, v, value)
476+
assert.Equal(t, 0, len(remaining))
477+
469478
_, _, err = decodeInt32((p.Bytes())[1:])
470479
assert.ErrorIs(t, err, InvalidInt32)
471480

@@ -491,7 +500,7 @@ func TestDecodeInt64(t *testing.T) {
491500
t.Parallel()
492501

493502
p := NewBuffer()
494-
v := int64(-9223372036854775808)
503+
v := int64(9223372036854775807)
495504
encodeInt64(p, v)
496505

497506
var value int64
@@ -501,6 +510,15 @@ func TestDecodeInt64(t *testing.T) {
501510
assert.Equal(t, v, value)
502511
assert.Equal(t, 0, len(remaining))
503512

513+
v = int64(-9223372036854775807)
514+
p.Reset()
515+
encodeInt64(p, v)
516+
517+
remaining, value, err = decodeInt64(p.Bytes())
518+
assert.NoError(t, err)
519+
assert.Equal(t, v, value)
520+
assert.Equal(t, 0, len(remaining))
521+
504522
_, _, err = decodeInt64((p.Bytes())[1:])
505523
assert.ErrorIs(t, err, InvalidInt64)
506524

decoder.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,10 @@ impl Decoder for Cursor<&mut Vec<u8>> {
254254
for _ in 0..VARINT_LEN32 {
255255
let byte = self.read_u8().ok().ok_or(DecodingError::InvalidI32)?;
256256
if byte < CONTINUATION {
257-
let mut x = ((ux | (byte as u32) << s) >> 1) as i32;
257+
ux |= (byte as u32) << s;
258+
let mut x = (ux >> 1) as i32;
258259
if ux & 1 != 0 {
259-
x = !x
260+
x = x.wrapping_add(1).wrapping_neg();
260261
}
261262
return Ok(x);
262263
}
@@ -277,9 +278,10 @@ impl Decoder for Cursor<&mut Vec<u8>> {
277278
for _ in 0..VARINT_LEN64 {
278279
let byte = self.read_u8().ok().ok_or(DecodingError::InvalidI64)?;
279280
if byte < CONTINUATION {
280-
let mut x = ((ux | (byte as u64) << s) >> 1) as i64;
281+
ux |= (byte as u64) << s;
282+
let mut x = (ux >> 1) as i64;
281283
if ux & 1 != 0 {
282-
x = !x
284+
x = x.wrapping_add(1).wrapping_neg();
283285
}
284286
return Ok(x);
285287
}
@@ -503,13 +505,18 @@ mod tests {
503505
#[test]
504506
fn test_decode_i32() {
505507
let mut encoder = Cursor::new(Vec::with_capacity(512));
506-
let v = -2147483648 as i32;
508+
let v = -2147483648;
509+
let vneg = -32;
507510
encoder.encode_i32(v).unwrap();
511+
encoder.encode_i32(vneg).unwrap();
508512

509513
let mut decoder = Cursor::new(encoder.get_mut());
510514
let val = decoder.decode_i32().unwrap();
511515
assert_eq!(val, v);
512516

517+
let val = decoder.decode_i32().unwrap();
518+
assert_eq!(val, vneg);
519+
513520
let error = decoder.decode_i32().unwrap_err();
514521
assert_eq!(error, DecodingError::InvalidI32);
515522
}

decoder.test.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,28 +149,34 @@ describe("Decoder", () => {
149149
});
150150

151151
it("Can decode Int32", () => {
152-
const expected = -2147483648;
152+
const expected = 2147483647;
153+
const expectedNegative = -2147483647;
153154

154-
const encoded = new Encoder().int32(expected).bytes;
155+
const encoded = new Encoder().int32(expected).int32(expectedNegative).bytes;
155156
const decoder = new Decoder(encoded);
156157

157158
const value = decoder.int32();
159+
const valueNegative = decoder.int32();
158160

159161
expect(value).toBe(expected);
162+
expect(valueNegative).toBe(expectedNegative);
160163
expect(decoder.length).toBe(0);
161164

162165
expect(() => decoder.int32()).toThrowError(InvalidInt32Error);
163166
});
164167

165168
it("Can decode Int64", () => {
166-
const expected = -9223372036854775808n;
169+
const expected = 9223372036854775807n;
170+
const expectedNegative = -9223372036854775807n;
167171

168-
const encoded = new Encoder().int64(expected).bytes;
172+
const encoded = new Encoder().int64(expected).int64(expectedNegative).bytes;
169173
const decoder = new Decoder(encoded);
170174

171175
const value = decoder.int64();
176+
const valueNegative = decoder.int64();
172177

173178
expect(value).toBe(expected);
179+
expect(valueNegative).toBe(expectedNegative);
174180
expect(decoder.length).toBe(0);
175181

176182
expect(() => decoder.int64()).toThrowError(InvalidInt64Error);

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module github.com/loopholelabs/polyglot
33
go 1.18
44

55
require (
6-
github.com/stretchr/testify v1.7.5
7-
google.golang.org/protobuf v1.28.0
6+
github.com/stretchr/testify v1.8.4
7+
google.golang.org/protobuf v1.31.0
88
)
99

1010
require (

go.sum

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
21
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
32
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
43
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
54
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
65
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
76
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
87
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9-
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10-
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
11-
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12-
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
13-
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
8+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
9+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
1410
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
1511
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
1612
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
17-
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
18-
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
13+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
14+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
1915
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
2016
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
21-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
2217
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
2318
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@loopholelabs/polyglot",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"license": "Apache-2.0",
55
"description": "A high-performance serialization framework used for encoding and decoding arbitrary datastructures across languages.",
66
"homepage": "https://github.com/loopholelabs/polyglot",

0 commit comments

Comments
 (0)