打开/关闭文件时出现分段错误?
我正在开发一个多线程程序。它能够正确关闭所有线程,但是,它最后会出现段错误。通过注释掉代码的某些部分,我发现它在处理打开/关闭文件的代码区域内:
char *pid_fname;
FILE *file;
sprintf(pid_fname, "%s%d%s", "/proc/", pid, "/stat");
file = fopen(pid_fname, "r");
/* code */
fclose(file);
我尝试在 gdb 中进行调试,但是只有在段错误后打印出“where”后才得到此信息:
#0 0x2f312f63 in ?? ()
#1 0x74617473 in ?? ()
#2 0xbfaee700 in ?? ()
#3 0xbfaee77c in ?? ()
#4 0x006a7810 in ?? ()
#5 0x00000000 in ?? ()
谁能给我一些关于从这里去哪里的指示?
I am working on a multithreaded program. It's able to close all the threads properly, however, it segfaults at the end. Through commenting out certain parts of my code, I found that it is within this area of the code that deals with opening/closing a file:
char *pid_fname;
FILE *file;
sprintf(pid_fname, "%s%d%s", "/proc/", pid, "/stat");
file = fopen(pid_fname, "r");
/* code */
fclose(file);
I tried debugging in gdb, however I only get this after printing out 'where' after the segfault:
#0 0x2f312f63 in ?? ()
#1 0x74617473 in ?? ()
#2 0xbfaee700 in ?? ()
#3 0xbfaee77c in ?? ()
#4 0x006a7810 in ?? ()
#5 0x00000000 in ?? ()
Can anyone give me some pointers on where to go from here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您没有文件名的后备存储。您已经创建了一个指针,但没有分配任何空间。这意味着它几乎肯定指向您不想写入的某个地方:-)
假设您知道进程 ID 的最大范围(例如 5 位数字),最简单的修复方法类似于(并稍微更改参数,因为
/proc/
和/stat
是固定字符串):否则,您需要根据实际的
pid
值动态分配足够的空间,如果内存不足,则进行防御性编码。由于系统往往具有固定的进程 ID 范围,因此我会选择固定大小的缓冲区。如果你真的想防止错误,同时又不担心动态分配,你可以使用类似的东西:
You have no backing storage for the file name. You've created a pointer but have allocated no space. That means it's almost certainly pointing somewhere where you don't want to write to :-)
Assuming you know the maximum range of a process ID (say 5 digits for example), the simplest fix is something like (and changing the arguments slightly since
/proc/
and/stat
are fixed strings):Otherwise, you'll need to dynamically allocate enough space based on the actual
pid
value, and code defensively if you run out of memory.Since systems tend to have a fixed range for process IDs, I'd opt for the fixed size buffer. If you really want to protect from bugs whilst still not worrying about dynamic allocation, you could use something like:
sprintf 要求其第一个参数是指向调用者分配的缓冲区的指针。您甚至没有初始化
pid_fname
。sprintf
requires that its first argument be a pointer to a caller-allocated buffer. You're not even initializingpid_fname
.pid_frame
是一个未初始化的指针。在这种情况下,无法将提供的参数复制到缓冲区。pid_frame
is an uninitialized pointer. The arguments supplied cannot be copied to the buffer in this case.