c - SIGABRT 在 fopen 调用

标签 c linux gcc file-io gdb

我正在编写一段代码来检查文件是否存在。当用户传入一个只有名称(无斜杠)的文件参数时,我将使用 PATH 环境变量中的路径检查该文件是否存在。我在用 gcc 编译的 Ubuntu 上,一直在使用 gdb。我找到了导致 SIGABRT 的行,但不确定信息告诉我的是什么。这是我第一次使用 gdb。这是我的片段:

char* PATH = NULL;
PATH = (char*)getenv( "PATH" );
char* pathtoken = strtok( PATH, ":" );
int index = 0;

while ( pathtoken != NULL )
{
    index++;
    printf( "%s\n", pathtoken );
    char* pathdir = (char*)malloc( sizeof(pathtoken) );
    strcpy( pathdir, pathtoken );
    GetFullPath( pathdir, filename );
    inputstream = fopen( pathdir, "r" );

    if ( inputstream != NULL)
    {
        free( pathdir );
        break;
        }

        free( pathdir );
        pathtoken = strtok( NULL, ":" );
    }

char* GetFullPath( char* dirpath, char* filename )
{
    //Append back slash
    int len = strlen(dirpath);
    dirpath[len] = '/';
    dirpath[len+1] = '\0';

    return strcat( dirpath, filename );
}

它发生在 fopen 行的第一次迭代中。这是我的 gdb 输出

Starting program: /home/seapoe/Documents/OsClass/CodePractice/a.out text1 /usr/lib/lightdm/lightdm

Breakpoint 1, main (argc=2, argv=0xbffff3a4) at xssh.c:87

87 GetFullPath( pathdir, filename );

(gdb) next

89 inputstream = fopen( pathdir, "r" );

(gdb) print pathdir

$5 = 0x804c008 "/usr/lib/lightdm/lightdm/text1"

(gdb) print pathdir[strlen(pathdir)]

$6 = 0 '\000'

(gdb) print pathdir[strlen(pathdir)+1]

$7 = 0 '\000'

(gdb) print pathdir[strlen(pathdir)+2]

$8 = 0 '\000'

(gdb) print pathdir[strlen(pathdir)-1]

$9 = 49 '1'

(gdb) next

* glibc detected /home/seapoe/Documents/OsClass/CodePractice/a.out: free(): invalid next size (normal): 0x0804c018 **

======= Backtrace: =========

/lib/i386-linux-gnu/libc.so.6(+0x75ee2)[0xb7e97ee2]

/lib/i386-linux-gnu/libc.so.6(+0x65db5)[0xb7e87db5]

/lib/i386-linux-gnu/libc.so.6(fopen+0x2b)[0xb7e87deb]

/home/seapoe/Documents/OsClass/CodePractice/a.out[0x8048a77]

/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb7e3b4d3]

/home/seapoe/Documents/OsClass/CodePractice/a.out[0x8048771]

======= Memory map: ========

08048000-0804a000 r-xp 00000000 08:01 536263 /home/seapoe/Documents/OsClass /CodePractice/a.out

0804a000-0804b000 r--p 00001000 08:01 536263 /home/seapoe/Documents/OsClass/CodePractice/a.out

0804b000-0804c000 rw-p 00002000 08:01 536263 /home/seapoe/Documents/OsClass/CodePractice/a.out

0804c000-0806d000 rw-p 00000000 00:00 0 [heap]

b7df3000-b7e0f000 r-xp 00000000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1

b7e0f000-b7e10000 r--p 0001b000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1

b7e10000-b7e11000 rw-p 0001c000 08:01 394136 /lib/i386-linux-gnu/libgcc_s.so.1

b7e21000-b7e22000 rw-p 00000000 00:00 0

b7e22000-b7fc5000 r-xp 00000000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so

b7fc5000-b7fc7000 r--p 001a3000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so

b7fc7000-b7fc8000 rw-p 001a5000 08:01 394115 /lib/i386-linux-gnu/libc-2.15.so

b7fc8000-b7fcb000 rw-p 00000000 00:00 0

b7fd9000-b7fdd000 rw-p 00000000 00:00 0

b7fdd000-b7fde000 r-xp 00000000 00:00 0 [vdso]

b7fde000-b7ffe000 r-xp 00000000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so

b7ffe000-b7fff000 r--p 0001f000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so

b7fff000-b8000000 rw-p 00020000 08:01 394095 /lib/i386-linux-gnu/ld-2.15.so

bffdf000-c0000000 rw-p 00000000 00:00 0 [stack]

Program received signal SIGABRT, Aborted.

0xb7fdd424 in __kernel_vsyscall ()

在调用 GetFullPath 之后,您可以看到在 fopen 调用之前返回了一个有效路径

最佳答案

char* pathdir = (char*)malloc( sizeof(pathtoken) );

错了。 sizeof(pathtoken) 将为您提供系统指针的大小(4 或 8)。 你想要

pathdir = strdup(pathtoken)

这也是错误的,因为您正在尝试附加到它。只需将 pathdir 设为 MAX_PATH(或 PATH_MAX,具体取决于您的操作系统)长度的字符数组。

关于c - SIGABRT 在 fopen 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19060729/

相关文章:

linux - shell 脚本 : How to read standard output of a program from console

c - 没有关于错误函数参数的编译错误

c++ - 如何为 C++ 程序定义 MSYS Makefile 的路径?

c - 使用 IPv6 进行套接字编程,其中客户端和服务器程序驻留在不同的计算机上

C++ 本地库和相对路径

为双向链表创建标题

linux - 挂载点归属

正则表达式 -> 匹配 000001 和 999999 之间的数字

c - 期望结构指针但获取结构,反之亦然

linux - ld 找不到已安装的库