Skip to content

Commit ddbd65b

Browse files
jakebaileyaykevl
authored andcommitted
builder, cgo, syscall: replace fixed-size array casts with unsafe.Slice
Several places used the (*[1 << N]T)(ptr)[:len:len] pattern to convert C pointers to Go slices. This has a hardcoded size limit that can panic if exceeded; RunTool hit this with >1024 linker arguments when many files are embedded. Replace all instances with unsafe.Slice, which handles any size.
1 parent 1a1506e commit ddbd65b

3 files changed

Lines changed: 5 additions & 5 deletions

File tree

builder/tools-builtin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func RunTool(tool string, args ...string) error {
2828
var cflag *C.char
2929
buf := C.calloc(C.size_t(len(args)), C.size_t(unsafe.Sizeof(cflag)))
3030
defer C.free(buf)
31-
cflags := (*[1 << 10]*C.char)(unsafe.Pointer(buf))[:len(args):len(args)]
31+
cflags := unsafe.Slice((**C.char)(buf), len(args))
3232
for i, flag := range args {
3333
cflag := C.CString(flag)
3434
cflags[i] = cflag

cgo/libclang.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (f *cgoFile) readNames(fragment string, cflags []string, filename string, c
130130
// convert Go slice of strings to C array of strings.
131131
cmdargsC := C.malloc(C.size_t(len(cflags)) * C.size_t(unsafe.Sizeof(uintptr(0))))
132132
defer C.free(cmdargsC)
133-
cmdargs := (*[1 << 16]*C.char)(cmdargsC)
133+
cmdargs := unsafe.Slice((**C.char)(cmdargsC), len(cflags))
134134
for i, cflag := range cflags {
135135
s := C.CString(cflag)
136136
cmdargs[i] = s
@@ -190,7 +190,7 @@ func (f *cgoFile) readNames(fragment string, cflags []string, filename string, c
190190
// Sanity check. This should (hopefully) never trigger.
191191
panic("libclang: file contents was not loaded")
192192
}
193-
data := (*[1 << 24]byte)(unsafe.Pointer(rawData))[:size]
193+
data := unsafe.Slice((*byte)(unsafe.Pointer(rawData)), size)
194194

195195
// Hash the contents if it isn't hashed yet.
196196
if _, ok := f.visitedFiles[path]; !ok {
@@ -624,7 +624,7 @@ func (p *cgoPackage) getClangLocationPosition(location C.CXSourceLocation, tu C.
624624
// now by reading the file from libclang.
625625
var size C.size_t
626626
sourcePtr := C.clang_getFileContents(tu, file, &size)
627-
source := ((*[1 << 28]byte)(unsafe.Pointer(sourcePtr)))[:size:size]
627+
source := unsafe.Slice((*byte)(unsafe.Pointer(sourcePtr)), size)
628628
lines := []int{0}
629629
for i := 0; i < len(source)-1; i++ {
630630
if source[i] == '\n' {

src/syscall/syscall_libc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
256256
if addr == unsafe.Pointer(^uintptr(0)) {
257257
return nil, getErrno()
258258
}
259-
return (*[1 << 30]byte)(addr)[:length:length], nil
259+
return unsafe.Slice((*byte)(addr), length), nil
260260
}
261261

262262
func Munmap(b []byte) (err error) {

0 commit comments

Comments
 (0)