Skip to content

fix(cgo): add unsafe.Pointer intermediary in halPortWrite/halPortPeek casts#77

Merged
sittner merged 2 commits intomodusoft-2.9from
copilot/fix-cgo-type-conversion-error
Feb 27, 2026
Merged

fix(cgo): add unsafe.Pointer intermediary in halPortWrite/halPortPeek casts#77
sittner merged 2 commits intomodusoft-2.9from
copilot/fix-cgo-type-conversion-error

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 27, 2026

CGO does not allow direct conversion from *byte to *C.char; the cast must go through unsafe.Pointer. This caused a build failure in the passthrough example.

Changes

  • src/hal/hal-go/cgo.go: Wrap unsafe.SliceData(...) in unsafe.Pointer(...) before casting to *C.char in both halPortWrite and halPortPeek.
// Before (broken)
ret := C.go_hal_port_write(portPtr, (*C.char)(unsafe.SliceData(data)), C.uint(len(data)))

// After (fixed)
ret := C.go_hal_port_write(portPtr, (*C.char)(unsafe.Pointer(unsafe.SliceData(data))), C.uint(len(data)))

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • linuxcnc.org
    • Triggering command: /usr/bin/go go mod tidy -e (dns block)
    • Triggering command: /usr/bin/go go get -v ./... -O2 -g -Wall -Werror -fno-stack-protector -Wdeclaration-after-statement -ffile-prefix-map=/opt/hostedtoolcache/go/1.24.13/x64=/_/GOROOT -frandom-seed=mCpk0xsceg30uswMFUbN (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Build failure: CGO type conversion error in cgo.go

When building the passthrough example, the Go compiler fails with:

# linuxcnc.org/hal
../../cgo.go:193:48: cannot convert unsafe.SliceData(data) (value of type *byte) to type *_Ctype_char
../../cgo.go:204:47: cannot convert unsafe.SliceData(buf) (value of type *byte) to type *_Ctype_char

Root cause

In src/hal/hal-go/cgo.go, the halPortWrite and halPortPeek functions pass unsafe.SliceData(data) directly to C functions that expect *C.char. The Go compiler doesn't allow a direct conversion from *byte to *C.char — it must go through unsafe.Pointer first.

Fix

In src/hal/hal-go/cgo.go, change the two affected lines:

Line ~193 in halPortWrite:

// BEFORE (broken):
ret := C.go_hal_port_write(portPtr, (*C.char)(unsafe.SliceData(data)), C.uint(len(data)))

// AFTER (fixed):
ret := C.go_hal_port_write(portPtr, (*C.char)(unsafe.Pointer(unsafe.SliceData(data))), C.uint(len(data)))

Line ~204 in halPortPeek:

// BEFORE (broken):
ret := C.go_hal_port_peek(portPtr, (*C.char)(unsafe.SliceData(buf)), C.uint(count))

// AFTER (fixed):
ret := C.go_hal_port_peek(portPtr, (*C.char)(unsafe.Pointer(unsafe.SliceData(buf))), C.uint(count))

The fix wraps unsafe.SliceData(...) in unsafe.Pointer(...) before casting to *C.char, which is the standard CGO pattern for pointer type conversions.

This pull request was created from Copilot chat.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…r in cgo.go

Co-authored-by: sittner <1475582+sittner@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix CGO type conversion error in cgo.go fix(cgo): add unsafe.Pointer intermediary in halPortWrite/halPortPeek casts Feb 27, 2026
@sittner sittner marked this pull request as ready for review February 27, 2026 15:53
@sittner sittner merged commit 27984d7 into modusoft-2.9 Feb 27, 2026
@sittner sittner deleted the copilot/fix-cgo-type-conversion-error branch March 4, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants