通过 cgo 调用fts_open时出现分段冲突错误

我正在测试cgo,每个简单的你好世界,如代码,工作得很好。

但我对下面的C代码有问题。

C 代码是遍历目录树并对文件大小求和的代码。

如果我用go命令构建,那么构建是可以的,没有错误。

但是在运行时,发生了“分段冲突”错误


bash$./walkdir 

fatal error: unexpected signal during runtime execution

[signal SIGSEGV: segmentation violation code=0x1 addr=0x1 pc=0x7f631e077c1a]

. . . .


-------------------------------------------------------------


package main

/*

#include <stdint.h>

#include <fts.h>

#include <sys/stat.h>


uintmax_t get_total_size(char *path)

{

    uintmax_t total_size = 0;

    FTS *fts = fts_open(&path, FTS_PHYSICAL, NULL);

    FTSENT *fent;

    while ((fent = fts_read(fts)) != NULL)

        if (fent->fts_info == FTS_F)

            total_size += fent->fts_statp->st_size;

    fts_close(fts);

    return total_size;

}

*/

import "C"

import "fmt"


func main() {

    fmt.Println(C.get_total_size(C.CString("/usr")))

}


SMILET
浏览 133回答 2
2回答

侃侃无极

fts_open&nbsp;定义如下:fts_open()该函数获取指向一个字符指针数组的指针,这些指针命名一个或多个路径,这些路径构成了要遍历的逻辑文件层次结构。数组必须由指针终止。fts_open()nullC没有对数组的直接支持;它只有指针。在你的情况下,你传递一个有效的指针,但它不在一个数组中,该数组有一个指针作为紧随其后的元素,所以继续扫描过去的内存 - 寻找一个指针, 并最终尝试在某个地址读取内存,这是禁止这样做的(通常是因为该地址的页面没有被分配)。fts_openNULLfts_open&pathNULL修复它的一种方法是创建该数组并在C端初始化它。看起来你正在使用一个相当最新的C标准,所以让我们只使用直接文字来初始化数组:package main/*#include <stddef.h> // for NULL#include <stdint.h>#include <stdlib.h> // for C.free#include <fts.h>#include <sys/stat.h>uintmax_t get_total_size(char *path){&nbsp; &nbsp; uintmax_t total_size = 0;&nbsp; &nbsp; char * path_argv[2] = {path, NULL};&nbsp; &nbsp; FTS *fts = fts_open(path_argv, FTS_PHYSICAL, NULL);&nbsp; &nbsp; FTSENT *fent;&nbsp; &nbsp; while ((fent = fts_read(fts)) != NULL)&nbsp; &nbsp; &nbsp; &nbsp; if (fent->fts_info == FTS_F)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total_size += fent->fts_statp->st_size;&nbsp; &nbsp; fts_close(fts);&nbsp; &nbsp; return total_size;}*/import "C"import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unsafe")func main() {&nbsp; &nbsp; cpath := C.CString("/usr")&nbsp; &nbsp; defer C.free(unsafe.Pointer(cpath))&nbsp; &nbsp; fmt.Println(C.get_total_size(cpath))}请注意,您的程序有一个错误和一个可能的问题:一个错误是,调用通过从链接的 C 库执行调用来分配内存块,而您没有释放该内存块。C.CStringmalloc(3)该符号定义在“标准定义.h”中;编译时可能会或可能不会收到错误。NULL我已经在我的示例中修复了这两个问题。对我们的示例的进一步改进可能是利用函数在单次运行中扫描多个路径的能力;如果我们要实现它,那么为Go端的第一个参数分配数组会更有意义:fts_*fts_openpackage main/*#include <stddef.h>#include <stdint.h>#include <stdlib.h>#include <fts.h>#include <sys/stat.h>uintmax_t get_total_size(char * const *path_argv){&nbsp; &nbsp; uintmax_t total_size = 0;&nbsp; &nbsp; FTS *fts = fts_open(path_argv, FTS_PHYSICAL, NULL);&nbsp; &nbsp; FTSENT *fent;&nbsp; &nbsp; while ((fent = fts_read(fts)) != NULL)&nbsp; &nbsp; &nbsp; &nbsp; if (fent->fts_info == FTS_F)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total_size += fent->fts_statp->st_size;&nbsp; &nbsp; fts_close(fts);&nbsp; &nbsp; return total_size;}*/import "C"import (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "unsafe")func main() {&nbsp; &nbsp; fmt.Println(getTotalSize("/usr", "/etc"))}func getTotalSize(paths ...string) uint64 {&nbsp; &nbsp; argv := make([]*C.char, len(paths)+1)&nbsp; &nbsp; for i, path := range paths {&nbsp; &nbsp; &nbsp; &nbsp; argv[i] = C.CString(path)&nbsp; &nbsp; &nbsp; &nbsp; defer C.free(unsafe.Pointer(argv[i]))&nbsp; &nbsp; }&nbsp; &nbsp; return uint64(C.get_total_size(&argv[0]))}请注意,这里我们没有显式地将最后一个参数清零,因为与C相反,Go用零初始化每个分配的内存块,因此一旦分配,其所有内存都已归零。argvargv

杨魅力

您收到错误,因为“fts_open”需要一个指向数组的字符指针,该数组以 NULL 结尾,如 char *argv[] = { 路径,NULL };.。(https://linux.die.net/man/3/fts_open)package main/*#include <stdint.h>#include <fts.h>#include <sys/stat.h>uintmax_t get_total_size(char *path){&nbsp; &nbsp; uintmax_t total_size = 0;&nbsp; &nbsp; char *argv[] = { path, NULL };&nbsp; &nbsp; FTS *fts = fts_open(argv, FTS_PHYSICAL, NULL);&nbsp; &nbsp; if (fts == NULL)&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; FTSENT *fent;&nbsp; &nbsp; while ((fent = fts_read(fts)) != NULL)&nbsp; &nbsp; &nbsp; &nbsp; if (fent->fts_info == FTS_F)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total_size += fent->fts_statp->st_size;&nbsp; &nbsp; fts_close(fts);&nbsp; &nbsp; return total_size;}*/import "C"import "fmt"func main() {&nbsp; &nbsp; fmt.Println(C.get_total_size(C.CString("/usr")))}因此添加数组指针将修复代码。使用 GCC 编译时,相同的代码也有效,但fts_open返回 NULL。我猜gcc和cgo之间的优化有一些区别(不是很确定)我尝试了一些测试结果,并能够发现,当使用GCC编译时,字符**指针被空终止,但在cgo的情况下,它没有以空值终止,所以你得到“SIGSEGV”,因为你的代码正在读取无效的内存引用。#include <stdio.h>#include <string.h>void try(char **p){&nbsp; &nbsp;while (*p != NULL)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; printf("%zu\n", strlen(*p));&nbsp; &nbsp; &nbsp; ++p;&nbsp; &nbsp;}}void get_total_size(char *path){&nbsp; &nbsp;try(&path);}int main(){&nbsp; &nbsp;get_total_size("/usr");}c 代码(有效)package main/*#include <stdio.h>#include <string.h>void try(char **p){&nbsp; &nbsp;while (*p != NULL)&nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; printf("%zu\n", strlen(*p));&nbsp; &nbsp; &nbsp; ++p;&nbsp; &nbsp;}}void get_total_size(char *path){&nbsp; &nbsp;try(&path);}*/import "C"func main() {&nbsp; &nbsp; C.get_total_size(C.CString("/usr"))}相同的去代码,你会面临错误
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go