Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/binary: panic when Write a nil. #60814

Closed
laushunyu opened this issue Jun 15, 2023 · 6 comments
Closed

encoding/binary: panic when Write a nil. #60814

laushunyu opened this issue Jun 15, 2023 · 6 comments

Comments

@laushunyu
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.20.5 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/macoo/.cache/go-build"
GOENV="/home/macoo/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/macoo/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/macoo/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/home/macoo/go/go1.20.5"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/macoo/go/go1.20.5/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/macoo/GolandProjects/binbug/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build3159415642=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
	"bytes"
	"crypto/md5"
	"encoding/binary"
	"fmt"
)

type MyStruct struct {
	Name string
}

func main() {
	var a *MyStruct
	hash := md5.New()
	binary.Write(bytes.NewBuffer(nil), binary.LittleEndian, a)
	fmt.Printf("%x", hash.Sum(nil))
}

then:

$ go run .

What did you expect to see?

nothing will be write to bytes.NewBuffer(nil)

What did you see instead?


panic: reflect: call of reflect.Value.Type on zero Value

goroutine 1 [running]:
reflect.Value.typeSlow({0x0?, 0x0?, 0x524400?})
        /home/macoo/go/go1.20.5/src/reflect/value.go:2610 +0x12e
reflect.Value.Type(...)
        /home/macoo/go/go1.20.5/src/reflect/value.go:2605
encoding/binary.dataSize({0x0?, 0x0?, 0xc00004a5c0?})
        /home/macoo/go/go1.20.5/src/encoding/binary/binary.go:490 +0x17f
encoding/binary.Write({0x4c06a8, 0xc00007e150}, {0x4c0ae8, 0x567230}, {0x48dd80?, 0x0})
        /home/macoo/go/go1.20.5/src/encoding/binary/binary.go:448 +0xea5
main.main()
        /home/macoo/GolandProjects/binbug/main.go:17 +0xb5
@laushunyu laushunyu changed the title binary.Write: panic when write a nil. encoding/binary: panic when Write a nil. Jun 15, 2023
@laushunyu
Copy link
Author

The struct pointed to by the data pointer may be acceptable by dataSize,
but since this pointer does not point to any data, no data should be written to it and no error should be returned(maybe?).

@gopherbot
Copy link

Change https://go.dev/cl/503695 mentions this issue: encoding/binary: fix panic when Write a nil

@bcmills
Copy link
Contributor

bcmills commented Jun 15, 2023

The documentation at https://pkg.go.dev/encoding/binary#Write says:

Data must be a fixed-size value or a slice of fixed-size values, or a pointer to such data.

The nil interface value is not any of those things, so this is a user error that should have been caught during testing and/or code review; a panic is appropriate in that case.

@bcmills
Copy link
Contributor

bcmills commented Jun 15, 2023

but since this pointer does not point to any data, no data should be written to it and no error should be returned(maybe?).

The nil interface value is not a pointer.

@ianlancetaylor
Copy link
Contributor

The standard library does not do explicit checks for all possible incorrect values. Panicking on an invalid value is acceptable behavior.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@ianlancetaylor @bcmills @gopherbot @laushunyu and others