Skip to content

Commit

Permalink
Merge pull request #26 from loopholelabs/staging
Browse files Browse the repository at this point in the history
Release v1.1.2
  • Loading branch information
ShivanshVij authored Aug 26, 2023
2 parents 1aba1c2 + 1b3efc7 commit f96ef00
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 39 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [v1.1.2] - 2023-08-26

### Fixes

- Fixes an issue where decoding certain i32 or i64 values would result in an incorrect value being returned.

## [v1.1.1] - 2023-06-12

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

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

[unreleased]: https://github.com/loopholelabs/scale/compare/v1.1.1...HEAD
[unreleased]: https://github.com/loopholelabs/scale/compare/v1.1.2...HEAD
[v1.1.2]: https://github.com/loopholelabs/scale/compare/v1.1.2
[v1.1.1]: https://github.com/loopholelabs/scale/compare/v1.1.1
[v1.1.0]: https://github.com/loopholelabs/scale/compare/v1.1.0
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "polyglot_rs"
version = "1.1.1"
version = "1.1.2"
edition = "2021"
description="A high-performance serialization framework used for encoding and decoding arbitrary datastructures across languages."
license = "Apache-2.0"
Expand All @@ -26,8 +26,8 @@ byteorder = "1"

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.96"
base64 = "0.21.2"
serde_json = "1.0.105"
base64 = "0.21.3"
num_enum = "0.6.1"

[profile.release]
Expand Down
20 changes: 12 additions & 8 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,13 @@ func decodeInt32(b []byte) ([]byte, int32, error) {
if i > VarIntLen32 && cb > 1 {
return b, 0, InvalidInt32
}
// End of varint, add the last bits and cast to signed integer
x := int32((ux | uint32(cb)<<s) >> 1)
// Flip the bits if the sign bit is set
// End of varint, add the last bits
ux |= uint32(cb) << s
// Separate value and sign
x := int32(ux >> 1)
// If sign bit is set, negate the number
if ux&1 != 0 {
x = ^x
x = -(x + 1)
}
return b[i+1:], x, nil
}
Expand All @@ -271,11 +273,13 @@ func decodeInt64(b []byte) ([]byte, int64, error) {
if i > VarIntLen64 && cb > 1 {
return b, 0, InvalidInt64
}
// End of varint, add the last bits and cast to signed integer
x := int64((ux | uint64(cb)<<s) >> 1)
// Flip the bits if the sign bit is set
// End of varint, add the last bits
ux |= uint64(cb) << s
// Separate value and sign
x := int64(ux >> 1)
// If sign bit is set, negate the number
if ux&1 != 0 {
x = ^x
x = -(x + 1)
}
return b[i+1:], x, nil
}
Expand Down
22 changes: 20 additions & 2 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestDecodeInt32(t *testing.T) {
t.Parallel()

p := NewBuffer()
v := int32(-2147483648)
v := int32(2147483647)
encodeInt32(p, v)

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

v = int32(-2147483647)
p.Reset()
encodeInt32(p, v)

remaining, value, err = decodeInt32(p.Bytes())
assert.NoError(t, err)
assert.Equal(t, v, value)
assert.Equal(t, 0, len(remaining))

_, _, err = decodeInt32((p.Bytes())[1:])
assert.ErrorIs(t, err, InvalidInt32)

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

p := NewBuffer()
v := int64(-9223372036854775808)
v := int64(9223372036854775807)
encodeInt64(p, v)

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

v = int64(-9223372036854775807)
p.Reset()
encodeInt64(p, v)

remaining, value, err = decodeInt64(p.Bytes())
assert.NoError(t, err)
assert.Equal(t, v, value)
assert.Equal(t, 0, len(remaining))

_, _, err = decodeInt64((p.Bytes())[1:])
assert.ErrorIs(t, err, InvalidInt64)

Expand Down
17 changes: 12 additions & 5 deletions decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,10 @@ impl Decoder for Cursor<&mut Vec<u8>> {
for _ in 0..VARINT_LEN32 {
let byte = self.read_u8().ok().ok_or(DecodingError::InvalidI32)?;
if byte < CONTINUATION {
let mut x = ((ux | (byte as u32) << s) >> 1) as i32;
ux |= (byte as u32) << s;
let mut x = (ux >> 1) as i32;
if ux & 1 != 0 {
x = !x
x = x.wrapping_add(1).wrapping_neg();
}
return Ok(x);
}
Expand All @@ -277,9 +278,10 @@ impl Decoder for Cursor<&mut Vec<u8>> {
for _ in 0..VARINT_LEN64 {
let byte = self.read_u8().ok().ok_or(DecodingError::InvalidI64)?;
if byte < CONTINUATION {
let mut x = ((ux | (byte as u64) << s) >> 1) as i64;
ux |= (byte as u64) << s;
let mut x = (ux >> 1) as i64;
if ux & 1 != 0 {
x = !x
x = x.wrapping_add(1).wrapping_neg();
}
return Ok(x);
}
Expand Down Expand Up @@ -503,13 +505,18 @@ mod tests {
#[test]
fn test_decode_i32() {
let mut encoder = Cursor::new(Vec::with_capacity(512));
let v = -2147483648 as i32;
let v = -2147483648;
let vneg = -32;
encoder.encode_i32(v).unwrap();
encoder.encode_i32(vneg).unwrap();

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

let val = decoder.decode_i32().unwrap();
assert_eq!(val, vneg);

let error = decoder.decode_i32().unwrap_err();
assert_eq!(error, DecodingError::InvalidI32);
}
Expand Down
14 changes: 10 additions & 4 deletions decoder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,34 @@ describe("Decoder", () => {
});

it("Can decode Int32", () => {
const expected = -2147483648;
const expected = 2147483647;
const expectedNegative = -2147483647;

const encoded = new Encoder().int32(expected).bytes;
const encoded = new Encoder().int32(expected).int32(expectedNegative).bytes;
const decoder = new Decoder(encoded);

const value = decoder.int32();
const valueNegative = decoder.int32();

expect(value).toBe(expected);
expect(valueNegative).toBe(expectedNegative);
expect(decoder.length).toBe(0);

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

it("Can decode Int64", () => {
const expected = -9223372036854775808n;
const expected = 9223372036854775807n;
const expectedNegative = -9223372036854775807n;

const encoded = new Encoder().int64(expected).bytes;
const encoded = new Encoder().int64(expected).int64(expectedNegative).bytes;
const decoder = new Decoder(encoded);

const value = decoder.int64();
const valueNegative = decoder.int64();

expect(value).toBe(expected);
expect(valueNegative).toBe(expectedNegative);
expect(decoder.length).toBe(0);

expect(() => decoder.int64()).toThrowError(InvalidInt64Error);
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module github.com/loopholelabs/polyglot
go 1.18

require (
github.com/stretchr/testify v1.7.5
google.golang.org/protobuf v1.28.0
github.com/stretchr/testify v1.8.4
google.golang.org/protobuf v1.31.0
)

require (
Expand Down
13 changes: 4 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@loopholelabs/polyglot",
"version": "1.1.1",
"version": "1.1.2",
"license": "Apache-2.0",
"description": "A high-performance serialization framework used for encoding and decoding arbitrary datastructures across languages.",
"homepage": "https://github.com/loopholelabs/polyglot",
Expand Down
2 changes: 1 addition & 1 deletion version/current_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.1.1
v1.1.2

0 comments on commit f96ef00

Please sign in to comment.