diff --git a/internal/xcnv/eda2lcio.go b/internal/xcnv/eda2lcio.go index aaac554..6031499 100644 --- a/internal/xcnv/eda2lcio.go +++ b/internal/xcnv/eda2lcio.go @@ -11,7 +11,6 @@ import ( "fmt" "io" "log" - "reflect" "unsafe" "github.com/go-lpc/mim/internal/eformat" @@ -105,10 +104,8 @@ func i32sFrom(w *bytes.Buffer, d *eformat.DIF) []int32 { // FIXME(sbinet): use a special io.Writer that writes to // a []int32 instead of this unsafe business ? - hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&raw)) - hdr.Len /= 4 - hdr.Cap /= 4 + ptr := (*int32)(unsafe.Pointer(&raw[0])) + sli := unsafe.Slice(ptr, len(raw)/i32sz) - data := *(*[]int32)(unsafe.Pointer(&hdr)) - return data + return sli } diff --git a/internal/xcnv/lcio2eda.go b/internal/xcnv/lcio2eda.go index 66d20eb..e831ede 100644 --- a/internal/xcnv/lcio2eda.go +++ b/internal/xcnv/lcio2eda.go @@ -9,7 +9,6 @@ import ( "fmt" "io" "log" - "reflect" "unsafe" "github.com/go-lpc/mim/internal/eformat" @@ -53,10 +52,12 @@ func LCIO2EDA(w io.Writer, r *lcio.Reader, freq int, msg *log.Logger) error { } func bytesFromI32s(raw []int32) []byte { + n := len(raw) + if n == 0 { + return nil + } const i32sz = 4 - hdr := *(*reflect.SliceHeader)(unsafe.Pointer(&raw)) - hdr.Len *= i32sz - hdr.Cap *= i32sz - - return *(*[]byte)(unsafe.Pointer(&hdr)) + ptr := (*byte)(unsafe.Pointer(&raw[0])) + sli := unsafe.Slice(ptr, i32sz*n) + return sli }