【发布时间】:2015-05-10 20:01:05
【问题描述】:
我正在尝试将 getline 支持添加到 http-fs-wrapper,但我遇到了一些 malloc 问题。
ssize_t _intercept_getdelim(int fd, char **lineptr, size_t *n, int delim)
{
intercept_t *obj = intercept[fd];
int counter;
size_t nc = sizeof(char);
counter = -1;
while (obj->offset < obj->size)
{
++counter;
if (*lineptr) {
*lineptr = realloc(*lineptr, (counter + 2) * nc);
}
else {
*lineptr = malloc(nc);
}
_intercept_read(fd, lineptr[counter], nc);
if (*lineptr[counter] == delim)
{
break;
}
}
*n = counter ? counter + 1 : counter;
*lineptr[counter + 2] = '\0';
// Why do we need a *n when the return value is the same??
return *n;
}
这里是_intercept_read的相关部分:
size_t _intercept_read(int fd, void *buf, size_t count)
{
memcpy(buf, obj->ra_buf+bo, count);
当我在 gdb 中逐步执行此操作时,第二次迭代会抛出一个 SIGSEGV(来自 memcpy——它不是结尾 \0,它仍在循环内)。我也不太明白getline/getdelim的*n和返回值有什么区别。
【问题讨论】:
-
不是一个答案,但尝试包装/替换每个 io 函数听起来是实现虚拟 http fs 的一种非常糟糕的方法。只有 open 文件的函数才需要被钩子,它们需要做的就是打开一个管道或本地套接字到一个执行 http 的线程或进程。