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.
This commit is contained in:
Jake Bailey
2026-05-10 10:23:48 -07:00
committed by Ayke
parent 1a1506ef79
commit ddbd65beec
3 changed files with 5 additions and 5 deletions
+1 -1
View File
@@ -28,7 +28,7 @@ func RunTool(tool string, args ...string) error {
var cflag *C.char
buf := C.calloc(C.size_t(len(args)), C.size_t(unsafe.Sizeof(cflag)))
defer C.free(buf)
cflags := (*[1 << 10]*C.char)(unsafe.Pointer(buf))[:len(args):len(args)]
cflags := unsafe.Slice((**C.char)(buf), len(args))
for i, flag := range args {
cflag := C.CString(flag)
cflags[i] = cflag
+3 -3
View File
@@ -130,7 +130,7 @@ func (f *cgoFile) readNames(fragment string, cflags []string, filename string, c
// convert Go slice of strings to C array of strings.
cmdargsC := C.malloc(C.size_t(len(cflags)) * C.size_t(unsafe.Sizeof(uintptr(0))))
defer C.free(cmdargsC)
cmdargs := (*[1 << 16]*C.char)(cmdargsC)
cmdargs := unsafe.Slice((**C.char)(cmdargsC), len(cflags))
for i, cflag := range cflags {
s := C.CString(cflag)
cmdargs[i] = s
@@ -190,7 +190,7 @@ func (f *cgoFile) readNames(fragment string, cflags []string, filename string, c
// Sanity check. This should (hopefully) never trigger.
panic("libclang: file contents was not loaded")
}
data := (*[1 << 24]byte)(unsafe.Pointer(rawData))[:size]
data := unsafe.Slice((*byte)(unsafe.Pointer(rawData)), size)
// Hash the contents if it isn't hashed yet.
if _, ok := f.visitedFiles[path]; !ok {
@@ -624,7 +624,7 @@ func (p *cgoPackage) getClangLocationPosition(location C.CXSourceLocation, tu C.
// now by reading the file from libclang.
var size C.size_t
sourcePtr := C.clang_getFileContents(tu, file, &size)
source := ((*[1 << 28]byte)(unsafe.Pointer(sourcePtr)))[:size:size]
source := unsafe.Slice((*byte)(unsafe.Pointer(sourcePtr)), size)
lines := []int{0}
for i := 0; i < len(source)-1; i++ {
if source[i] == '\n' {
+1 -1
View File
@@ -256,7 +256,7 @@ func Mmap(fd int, offset int64, length int, prot int, flags int) (data []byte, e
if addr == unsafe.Pointer(^uintptr(0)) {
return nil, getErrno()
}
return (*[1 << 30]byte)(addr)[:length:length], nil
return unsafe.Slice((*byte)(addr), length), nil
}
func Munmap(b []byte) (err error) {