Skip to content

Commit

Permalink
refactor: remove some unsafe usage
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Jul 16, 2024
1 parent a59b532 commit 5df0df9
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
//go:build !1.23rc2

package phpserialize

import (
"fmt"

_ "go4.org/unsafe/assume-no-moving-gc"

"github.com/trim21/go-phpserialize/internal/encoder"
)

Expand Down
Binary file modified docs/benchmark.txt
Binary file not shown.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/sergi/go-diff v1.3.1
github.com/stretchr/testify v1.9.0
github.com/volatiletech/null/v9 v9.0.0
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/volatiletech/null/v9 v9.0.0 h1:JCdlHEiSRVxOi7/MABiEfdsqmuj9oTV20Ao7VvZ0JkE=
github.com/volatiletech/null/v9 v9.0.0/go.mod h1:zRFghPVahaiIMRXiUJrc6gsoG83Cm3ZoAfSTw7VHGQc=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 h1:lGdhQUN/cnWdSH3291CUuxSEqc+AsGTiDxPP3r2J0l4=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
Expand Down
4 changes: 1 addition & 3 deletions internal/decoder/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package decoder
import (
"reflect"
"strconv"
"unsafe"

"github.com/trim21/go-phpserialize/internal/errors"
)
Expand Down Expand Up @@ -70,8 +69,7 @@ func (d *floatDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv refle
}

func (d *floatDecoder) processBytes(bytes []byte, cursor int64, rv reflect.Value) (int64, error) {
s := *(*string)(unsafe.Pointer(&bytes))
f64, err := strconv.ParseFloat(s, 64)
f64, err := strconv.ParseFloat(unsafeStr(bytes), 64)
if err != nil {
return 0, errors.ErrSyntax(err.Error(), cursor)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/decoder/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (d *uintDecoder) parseUint(b []byte) (uint64, error) {
return sum, nil
}

func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error) {
func (d *uintDecoder) decodeBytes(buf []byte, cursor int64) ([]byte, int64, error) {
b := (*sliceHeader)(unsafe.Pointer(&buf)).data
if char(b, cursor) != 'i' {
return nil, cursor, errors.ErrExpected("int", cursor)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (d *uintDecoder) decodeByte(buf []byte, cursor int64) ([]byte, int64, error
}

func (d *uintDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, rv reflect.Value) (int64, error) {
bytes, c, err := d.decodeByte(ctx.Buf, cursor)
bytes, c, err := d.decodeBytes(ctx.Buf, cursor)
if err != nil {
return 0, err
}
Expand Down
9 changes: 9 additions & 0 deletions internal/decoder/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package decoder

import (
"unsafe"
)

func unsafeStr(b []byte) string {
return unsafe.String(unsafe.SliceData(b), len(b))
}
5 changes: 1 addition & 4 deletions internal/decoder/wrapped_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package decoder
import (
"reflect"
"strconv"
"unsafe"

"github.com/trim21/go-phpserialize/internal/errors"
)
Expand Down Expand Up @@ -77,9 +76,7 @@ type stringBoolDecoder struct {
}

func (s stringBoolDecoder) DecodeString(ctx *RuntimeContext, bytes []byte, topCursor int64, rv reflect.Value) error {
str := *(*string)(unsafe.Pointer(&bytes))

value, err := strconv.ParseBool(str)
value, err := strconv.ParseBool(unsafeStr(bytes))
if err != nil {
return errors.ErrSyntax(err.Error(), topCursor)
}
Expand Down
7 changes: 0 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ If you have any thought about how to support these types, please create an issue
- 1.22
- 1.23rc2

This repo have `//go:build ...` to disallow downstream users compiling this package on newer version of go.

This is because the usage of unsafe package, and unsafe doesn't follow Go 1 promise of compatibility,
so new version of golang may break this package.

When a new version of go is released, I will test on new go version and create a new release.

## Install

```console
Expand Down

0 comments on commit 5df0df9

Please sign in to comment.