【问题标题】:Why passing Go bytes to C, the bytes length doesn't match in cgo in this code below?为什么在下面的代码中将 Go 字节传递给 C,字节长度与 cgo 中的字节长度不匹配?
【发布时间】:2021-09-12 13:46:44
【问题描述】:

将 golang 字节传递给 C 时,字节长度不匹配。

生成的 strlen(key) 和 keylen 不匹配。

使用“go build file.go”构建

你可以在这里下载下面的 go 文件: https://pastebin.com/raw/hnMfXJKq

预期的输出应该是相同的密钥长度。有时有效,有时无效。

package main
/*
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>

void replaceK(void *key, size_t keylen, void *value, size_t valuelen);
void replaceK(void *key, size_t keylen, void *value, size_t valuelen)
{
        printf("replaceK : key = %s = %lu = %lu = %s = %lu\n",(char*) key,keylen,strlen(key),(char*) value,valuelen);
        if (keylen != strlen(key)){
                printf("ERROR!!! keylen : %lu != strlen(key) : %lu!!!\n",keylen,strlen(key));
                exit(1);
        }
}
*/
import "C"
import (
        "fmt"
        "unsafe"
        "math/rand"
        "time"
)
func Set(key,value []byte) {
        cKey := cByteSlice(key)
        cValue := cByteSlice(value)
        C.replaceK(unsafe.Pointer(cKey),C.ulong(len(key)),unsafe.Pointer(cValue),C.ulong(len(value)))
        C.free(unsafe.Pointer(cKey))
        C.free(unsafe.Pointer(cValue))
}
func byteToChar(b []byte) *C.char {
        var c *C.char
        if len(b) > 0 {
                c = (*C.char)(unsafe.Pointer(&b[0]))
        }
        return c
}
var letterRunes = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) []byte {
        randNum := rand.Intn(n)+1
        b := make([]byte, randNum)
        for i := range b {
                b[i] = letterRunes[rand.Intn(len(letterRunes))]
        }
        return b
}
func cByteSlice(b []byte) *C.char {
        var c *C.char
        if len(b) > 0 {
                c = (*C.char)(C.CBytes(b))
        }
        return c
}
func main() {
        rand.Seed(time.Now().UnixNano())
        var key []byte
        var value []byte
        for i:=0;i<10000000;i++ {
                key = RandStringRunes(10)
                value = RandStringRunes(20)
                randnum := 1
                if randnum == 1 {
                        fmt.Printf(">>> = %s = %s\n",key, value)
                        Set(key,value)
                }
        }
}                                                                                  

【问题讨论】:

  • 你记得在你的字符串中包含 C 字符串 null-terminator 吗?否则你不能在 C 中使用strlen
  • 请创建一个minimal reproducible example。您有一堆损坏或注释掉的代码,我们无法判断您打算这样做。
  • @Someprogrammerdude 是的,你是对的。我知道,但我对来自 golang 的 []byte 等效项更感兴趣,但无论如何,长度的输出应该是相同的。
  • 其实不必如此。请记住,C 中的 strlen 函数会查找空终止符。如果它在字符串中不存在,则strlen 将超出范围并继续计数,直到它在内存中的某处找到与空终止符相对应的内容。如果字符串没有空终止符,那么strlen 几乎总是会报告比预期更长的长度(除非您un幸运的是字符串恰好在之后有一个空终止符它)。传递未明确以 null 结尾的字符串会导致 未定义的行为
  • @superdolt:您正在使用 CBytes,它将确切的字节复制到 C 字节数组中。您需要使用 CString,或附加您自己的空字节。

标签: c go


【解决方案1】:

C strlen 函数用于以空字符结尾的字符串,而不是指针+长度字符串。

您可以使用 C printf 函数使用 %.*s 而不是 %s 打印指针+长度字符串。由于 Go 的 string[]byte 变量都使用指针+长度编码,这可能是打印从 Go 获得的字符串的正确方法。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2019-01-13
  • 1970-01-01
  • 1970-01-01
  • 2019-08-04
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多