Skip to content

Commit

Permalink
avoid using unsafe.Pointer() when working with syscall.Utsname{}
Browse files Browse the repository at this point in the history
  • Loading branch information
mvo5 committed Jun 22, 2017
1 parent dde3e76 commit 3b35996
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions arch/arch.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@
package arch

import (
"bytes"
"log"
"runtime"
"syscall"
"unsafe"
)

// ArchitectureType is the type for a supported snappy architecture
Expand Down Expand Up @@ -90,8 +88,18 @@ func UbuntuKernelArchitecture() string {
log.Panicf("cannot get kernel architecture: %v", err)
}

p := (*[len(utsname.Machine)]byte)(unsafe.Pointer(&utsname.Machine))
kernelArch := bytes.SplitN(p[:], []byte("\x00"), 2)[0]
// syscall.Utsname{} is using [65]int8 for all char[] inside it,
// this makes converting it so awkward. The alternative would be
// to use a unsafe.Pointer() to cast it to a [65]byte slice.
// see https://github.com/golang/go/issues/20753
kernelArch := make([]byte, 0, len(utsname.Machine))
for _, c := range utsname.Machine {
if c == 0 {
break
}
kernelArch = append(kernelArch, byte(c))
}

return ubuntuArchFromKernelArch(string(kernelArch))
}

Expand Down

0 comments on commit 3b35996

Please sign in to comment.