提问人:DravStart 提问时间:11/1/2023 最后编辑:DravStart 更新时间:11/1/2023 访问量:56
Syscall 参数 getcwd(buf) 指向不可寻址的字节
Syscall param getcwd(buf) points to unaddressable byte(s)
问:
我是第一次使用 valgrind 测试程序。我没有找到有关此错误的任何帮助。我做错了什么?我该如何解决?
最小可重复示例:
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
char* dir = "testdir";
mkdir(dir,0777);
char* buf = calloc(1, sizeof(char)*255);
char* directory = realpath(dir, buf);
printf("%s\n", directory);
free(buf);
}
valgrind 报告:
==18518== Syscall param getcwd(buf) points to unaddressable byte(s)
==18518== at 0x4969AF2: getcwd (getcwd.c:78)
==18518== by 0x48AD520: realpath@@GLIBC_2.3 (canonicalize.c:88)
==18518== by 0x109216: main (in /home/sol/Documents/SOLProject/.test/a.out)
==18518== Address 0x4a5013f is 0 bytes after a block of size 255 alloc'd
==18518== at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==18518== by 0x1091FF: main (in /home/sol/Documents/SOLProject/.test/a.out)
==18518==
/home/sol/Documents/SOLProject/.test/testdir
==18518==
==18518== HEAP SUMMARY:
==18518== in use at exit: 0 bytes in 0 blocks
==18518== total heap usage: 2 allocs, 2 frees, 1,279 bytes allocated
==18518==
==18518== All heap blocks were freed -- no leaks are possible
==18518==
==18518== For lists of detected and suppressed errors, rerun with: -s
==18518== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
答:
1赞
Paul Floyd
11/1/2023
#1
在 FreeBSD 13.2 上使用热的 valgrind 3.22 我得到
==12420== Syscall param __realpathat(buf) points to unaddressable byte(s)
==12420== at 0x4994B1A: ??? (in /lib/libc.so.7)
==12420== by 0x49CDC16: realpath (in /lib/libc.so.7)
==12420== by 0x2019D3: main (so12.c:10)
==12420== Address 0x545f13f is 0 bytes after a block of size 255 alloc'd
==12420== at 0x4851725: calloc (vg_replace_malloc.c:1599)
==12420== by 0x2019C2: main (so12.c:9)
正如评论中提到的,期望realpath
The resolved_path argument must point to a buffer capable of storing at
least PATH_MAX characters, or be NULL.
如果我更改为将 NULL 作为缓冲区realpath
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
char* dir = "testdir";
mkdir(dir,0777);
char* directory = realpath(dir, NULL);
printf("%s\n", directory);
free(directory);
}
然后我没有错误。
评论
realpath
手册页。PATH_MAX
PATH_MAX
255
buf
/home/sol/Documents/SOLProject/.test/testdir
PATH_MAX
PATH_MAX
char *
char [N]
char *dir
char dir[]
const
const char *dir