Merge pull request #60 from goccy/feature/fix-unexpected-free-pointer

Fix a bugs where pointers are unexpectedly freed
This commit is contained in:
Masaaki Goshima 2020-11-19 03:36:56 +09:00 committed by GitHub
commit 5ab988208f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -19,7 +19,12 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Test - name: Test
run: go test -v ./ run: go test -v ./ -count=1
- name: Test with race detector - name: Test with GC
run: go test -v -race ./ run: go test -v ./ -count=1
env:
GOGC: 1
# - name: Test with race detector
# run: go test -v -race ./ -count=1

View File

@ -216,6 +216,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} }
seenPtr[ptr] = struct{}{} seenPtr[ptr] = struct{}{}
v := e.ptrToInterface(code, ptr) v := e.ptrToInterface(code, ptr)
ctx.keepRefs = append(ctx.keepRefs, unsafe.Pointer(&v))
rv := reflect.ValueOf(v) rv := reflect.ValueOf(v)
if rv.IsNil() { if rv.IsNil() {
e.encodeNull() e.encodeNull()
@ -292,6 +293,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, code *opcode) error {
} }
seenPtr[ptr] = struct{}{} seenPtr[ptr] = struct{}{}
v := e.ptrToInterface(code, ptr) v := e.ptrToInterface(code, ptr)
ctx.keepRefs = append(ctx.keepRefs, unsafe.Pointer(&v))
rv := reflect.ValueOf(v) rv := reflect.ValueOf(v)
if rv.IsNil() { if rv.IsNil() {
e.encodeNull() e.encodeNull()

View File

@ -225,9 +225,6 @@ var (
) )
func initBig() { func initBig() {
if len(jsonBig) > 0 {
return
}
n := 10000 n := 10000
if testing.Short() { if testing.Short() {
n = 100 n = 100