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

Use proper sizes when casting to Go slice #102

Merged
merged 1 commit into from
Aug 22, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ package util

import (
"bufio"
"math"
"os"
"os/user"
"strconv"
Expand All @@ -43,15 +42,15 @@ func Ptr(slice []byte) unsafe.Pointer {
// ByteSlice takes a pointer to some data and views it as a slice of bytes.
// Note, indexing into this slice is unsafe.
func ByteSlice(ptr unsafe.Pointer) []byte {
// Silce must fix in 32-bit address space to build on 32-bit platforms.
return (*[math.MaxInt32]byte)(ptr)[:]
// Silce must fit in the smallest address space go suppports.
return (*[1 << 30]byte)(ptr)[:]
}

// PointerSlice takes a pointer to an array of pointers and views it as a slice
// of pointers. Note, indexing into this slice is unsafe.
func PointerSlice(ptr unsafe.Pointer) []unsafe.Pointer {
// Silce must fix in 32-bit address space to build on 32-bit platforms.
return (*[math.MaxInt32 / 4]unsafe.Pointer)(ptr)[:]
// Silce must fit in the smallest address space go suppports.
return (*[1 << 28]unsafe.Pointer)(ptr)[:]
}

// Index returns the first index i such that inVal == inArray[i].
Expand Down