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: Read fails with slice of fixed-size values #64384

Closed
1 task done
3052 opened this issue Nov 25, 2023 · 1 comment
Closed
1 task done

encoding/binary: Read fails with slice of fixed-size values #64384

3052 opened this issue Nov 25, 2023 · 1 comment

Comments

@3052
Copy link

3052 commented Nov 25, 2023

Go version

go version go1.21.0 windows/amd64

Reproducibility

  • Does this issue reproduce with the latest release?

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

set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Steven\AppData\Local\go-build
set GOENV=C:\Users\Steven\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Steven\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Steven\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=D:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=D:\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.21.0
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Windows\TEMP\go-build154863652=/tmp/go-build -gno-record-gcc-switches

What did you do?

this code gives expected result:

package main

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

func main() {
   r := bytes.NewReader([]byte{1,2})
   var i [2]int8
   err := binary.Read(r, binary.BigEndian, &i)
   if err != nil {
      panic(err)
   }
   fmt.Println(i) // [1 2]
}

but if I use a slice instead, I get no error and nil output:

[]int8(nil)

What did you expect to see?

this behavior seems to disagree with the docs:

What did you see instead?

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

https://godocs.io/encoding/binary#Read

@3052 3052 closed this as completed Nov 25, 2023
@3052
Copy link
Author

3052 commented Nov 25, 2023

my mistake, you have to allocate:

package main

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

func main() {
   r := bytes.NewReader([]byte{1,2})
   i := make([]int8, 2)
   err := binary.Read(r, binary.BigEndian, i)
   if err != nil {
      panic(err)
   }
   fmt.Println(i) // [1 2]
}

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

No branches or pull requests

1 participant