Skip to content

Use *byte instead of unsafe.Pointer for C bridge methods #12

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions internal/native/cgo_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import "C"
import (
"io"
"log"
"unsafe"
)

//export cgo_read_buffer
func cgo_read_buffer(
bufferIndex C.int,
bufPtr unsafe.Pointer,
bufPtr *byte,
length C.int,
) C.int {
goLength := int(length)
Expand Down
3 changes: 1 addition & 2 deletions internal/native/cgo_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ package native
#include "bsdiff.h"
*/
import "C"
import "unsafe"

//export cgo_write_buffer
func cgo_write_buffer(
bufferIndex C.int,
dataPtr unsafe.Pointer,
dataPtr *byte,
size C.int,
) C.int {
buffer := writers.Get(int(bufferIndex))
Expand Down
4 changes: 2 additions & 2 deletions internal/native/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func bytesToUint8PtrAndSize(bytes []byte) (ptr *C.uint8_t, size C.int64_t) {
return
}

func cPtrToSlice(ptr unsafe.Pointer, size int) []byte {
func cPtrToSlice(ptr *byte, size int) []byte {
var slice []byte
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&slice))
sliceHeader.Cap = size
sliceHeader.Len = size
sliceHeader.Data = uintptr(ptr)
sliceHeader.Data = uintptr(unsafe.Pointer(ptr))

return slice
}
38 changes: 38 additions & 0 deletions internal/native/native_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright (c) 2017-2025 Carl Kittelberger.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE.txt file.

//go:build cgo && go1.20
// +build cgo,go1.20

package native

import (
"crypto/rand"
"testing"
"unsafe"

"github.com/stretchr/testify/assert"
)

const testBytesLen = 512 * 1024

func makeTestBytes() []byte {
b := make([]byte, testBytesLen)
_, _ = rand.Read(b)
return b
}

func Test_bytesToUint8PtrAndSize(t *testing.T) {
b := makeTestBytes()
ptr, size := bytesToUint8PtrAndSize(b)
assert.EqualValues(t, &b[0], ptr)
assert.EqualValues(t, testBytesLen, size)
}

func Test_cPtrToSlice(t *testing.T) {
b := makeTestBytes()
bytePtr := unsafe.SliceData(b)

Check failure on line 35 in internal/native/native_internal_test.go

View workflow job for this annotation

GitHub Actions / test (1.20)

unsafe.SliceData requires go1.20 or later (-lang was set to go1.18; check go.mod) (typecheck)

Check failure on line 35 in internal/native/native_internal_test.go

View workflow job for this annotation

GitHub Actions / test (1.20)

unsafe.SliceData requires go1.20 or later (-lang was set to go1.18; check go.mod) (typecheck)
b2 := cPtrToSlice(bytePtr, testBytesLen)
assert.Equal(t, b, b2)
}
Loading