如何处理 cgo 中打包结构中的 char *?

How to handle char * from packed struct in cgo?

由于 Go 不支持打包结构,我发现这篇很棒的文章通过示例解释了所有内容,说明如何在 go 中使用打包结构。 https://medium.com/@liamkelly17/working-with-packed-c-structs-in-cgo-224a0a3b708b

问题是当我尝试用 char * 代替 [10]char 时它不起作用。我不确定这种转换如何与 [10]char 一起使用而不是与 char * 一起使用。这是从上面的文章中获取并使用 char * .

修改的示例代码
package main

/*
#include "stdio.h"
#pragma pack(1)
typedef struct{
    unsigned char a;
    char b;
    int c;
    unsigned int d;
    char *e; // changed from char[10] to char *
}packed;

void PrintPacked(packed p){
    printf("\nFrom C\na:%d\nb:%d\nc:%d\nd:%d\ne:%s\n", p.a, p.b, p.c, p.d, p.e);
}

*/
import "C"
import (
    "bytes"
    "encoding/binary"
)

//GoPack is the go version of the c packed structure
type GoPack struct {
    a uint8
    b int8
    c int32
    d uint32
    e [10]uint8
}

//Pack Produces a packed version of the go struct
func (g *GoPack) Pack(out *C.packed) {
    buf := &bytes.Buffer{}
    binary.Write(buf, binary.LittleEndian, g)
    *out = *(*C.packed)(C.CBytes(buf.Bytes()))
}

func main() {
    pack := &GoPack{1, 2, 3, 4, [10]byte{}}
    copy(pack.e[:], "TEST123")
    cpack := C.packed{} //just to allocate the memory, still under GC control
    pack.Pack(&cpack)
    C.PrintPacked(cpack)
}

我是第一次使用 cgo,如有错误请指正。

您正在将 GoPack.e 的十(零)个字节写入类型为 char *packed.e。这是行不通的,因为指针将是 4 或 8 个字节,具体取决于您的系统,所以即使这些字节代表一个有效的指针,您也会溢出分配的内存量。

如果你想创建一个有效的结构,其中包含一个有效的packed.e字段,你需要在C堆中分配10个字节的内存,将字节复制到其中,然后指向packed.e到这个分配的内存。 (当您释放相应的 packed 结构时,您还需要释放此内存)。您不能直接使用 binary.Write.

执行此操作

你可以以此为起点:

buf := &bytes.Buffer{}
binary.Write(buf, binary.LittleEndian, g.a)
binary.Write(buf, binary.LittleEndian, g.b)
binary.Write(buf, binary.LittleEndian, g.c)
binary.Write(buf, binary.LittleEndian, g.d)
binary.Write(buf, binary.LittleEndian, uintptr(C.CBytes(g.e))
*out = *(*C.packed)(C.CBytes(buf.Bytes()))

函数C.CBytes(b)在C堆中分配len(b)个字节,并将b中的字节复制到其中,返回一个unsafe.Pointer

请注意,我已经从您的代码中复制了您的 *out = *(*C.packed)... 行。这实际上会导致内存泄漏和不必要的复制。可能最好使用将字节直接写入 out.

指向的内存的写入器

也许是这个?

const N = 10000 // should be sizeof(*out) or larger
buf := bytes.NewBuffer((*[N]byte)(unsafe.Pointer(out))[:])

这使得 bytes.Buffer 直接写入 out 结构而不经过任何中间存储器。请注意,由于不安全的恶作剧,如果您写入的数据字节数多于 out.

所指向的字节数,则容易发生缓冲区溢出

警告的话:这一切都非常讨厌,并且容易出现您在 C 中发现的相同类型的问题,您需要检查 cgo 指针规则以确保您不会受到攻击到垃圾收集交互。一个建议:鉴于你说你“在指针和内存分配方面没有太多经验”,你可能应该避免编写或包含这样的代码,因为它可能引入的问题是邪恶的并且可能不会立即显而易见。