Strsep在我的Mac OS X x86_64系统上返回一个32位指针

ben*_*ers 2 c macos assembly x86-64 32bit-64bit

编辑:找到解决方案(见底部)

我正在尝试在Mac OS X 10.8 x86_64系统上构建软件包(owfs).这个软件包主要是在linux上开发的,但是我已经有了一个旧版本来编译和运行正常,并且在互联网上有很多帖子运行各种版本的人,所以我希望它是可能的.我已经能够成功完成编译过程,但是当我尝试运行它时程序会出现段错误.我在GDB中调查过,这是我发现的:

令人讨厌的代码:

void FS_dir_entry_aliased(void (*dirfunc) (void *, const struct parsedname *), void *v, const struct parsedname *pn)
{
if ( ( pn->state & ePS_unaliased ) == 0 ) {
    // Want alias substituted
    struct parsedname s_pn_copy ;
    struct parsedname * pn_copy = & s_pn_copy ;

    ASCII path[PATH_MAX+3] ;
    ASCII * path_pointer = path ; // current location in original path

    // Shallow copy
    memcpy( pn_copy, pn, sizeof(struct parsedname) ) ;
    pn_copy->path[0] = '\0' ;

    // path copy to use for separation
    strcpy( path, pn->path ) ;

    // copy segments of path (delimitted by "/") to copy
    while( path_pointer != NULL ) {
                    ASCII * path_segment = NULL;
        path_segment = strsep( &path_pointer, "/" ) ;
        BYTE sn[SERIAL_NUMBER_SIZE] ;

        if ( PATH_MAX < strlen(pn_copy->path) + strlen(path_segment) ) {
Run Code Online (Sandbox Code Playgroud)

要在通话strlen(path_segment)if,因为块失败path_segment是一个彻头彻尾的越界地址.

通过GDB逐步采取措施,这就是我发现的.进入初始化通话path_segmentstrsep,GDB给我:

path_pointer = (ASCII *) 0x7fff5fbf99a5 "/uncached/bus.0/interface"
Run Code Online (Sandbox Code Playgroud)

在我执行下一行之后,path_pointer已经提升了,正如我所期望的那样:

path_pointer = (ASCII *) 0x7fff5fbf99a6 "uncached/bus.0/interface"
Run Code Online (Sandbox Code Playgroud)

但是gdb报道:

path_segment = (ASCII *) 0x5fbf99a5 <Address 0x5fbf99a5 out of bounds>
Run Code Online (Sandbox Code Playgroud)

这是地址的正确"开始",但它是一个32位指针地址.由于我的系统是一个64位系统,当strlen被调用时,我得到一个EXC_BAD_ACCESS.

我知道这里有很多内容,如果问题深深地隐藏在包构建过程中,我还没有提供足够的信息.在我看来,虽然这可能有一个简单的解决方案,因为该函数基本上正常工作,但只是返回错误的大小指针.我对64位与32位系统的错综复杂的经验很少,所以我想知道是否有人能发现一些明显的东西,或者提供下一步调试此问题的说明.有趣的是,当我strsep在gdb(p (ASCII *) strsep (&path_pointer, "/"))中手动运行命令时,它们看起来工作正常,给我64位指针符合我的预期.

最后,如果它有用,我认为这是strsep调用的装配线:

0x0000000100036eee <FS_dir_entry_aliased+318>:  callq  0x1000a56a0 <dyld_stub_strsep>
0x0000000100036ef3 <FS_dir_entry_aliased+323>:  mov    %eax,%ecx
0x0000000100036ef5 <FS_dir_entry_aliased+325>:  movslq %ecx,%rcx
0x0000000100036ef8 <FS_dir_entry_aliased+328>:  mov    %rcx,-0x1d10(%rbp)
Run Code Online (Sandbox Code Playgroud)

并在那个callq地址反汇编strsep:

0x00007fff915c0fc7 <strsep+0>:  push   %rbp
0x00007fff915c0fc8 <strsep+1>:  mov    %rsp,%rbp
0x00007fff915c0fcb <strsep+4>:  mov    (%rdi),%r8
0x00007fff915c0fce <strsep+7>:  xor    %eax,%eax
0x00007fff915c0fd0 <strsep+9>:  test   %r8,%r8
0x00007fff915c0fd3 <strsep+12>: je     0x7fff915c1007 <strsep+64>
0x00007fff915c0fd5 <strsep+14>: mov    %r8,%r10
0x00007fff915c0fd8 <strsep+17>: jmp    0x7fff915c0fe4 <strsep+29>
0x00007fff915c0fda <strsep+19>: inc    %rdx
0x00007fff915c0fdd <strsep+22>: test   %al,%al
0x00007fff915c0fdf <strsep+24>: jne    0x7fff915c0fee <strsep+39>
0x00007fff915c0fe1 <strsep+26>: mov    %r9,%r10
0x00007fff915c0fe4 <strsep+29>: mov    (%r10),%cl
0x00007fff915c0fe7 <strsep+32>: lea    0x1(%r10),%r9
0x00007fff915c0feb <strsep+36>: mov    %rsi,%rdx
0x00007fff915c0fee <strsep+39>: mov    (%rdx),%al
0x00007fff915c0ff0 <strsep+41>: cmp    %cl,%al
0x00007fff915c0ff2 <strsep+43>: jne    0x7fff915c0fda <strsep+19>
0x00007fff915c0ff4 <strsep+45>: xor    %edx,%edx
0x00007fff915c0ff6 <strsep+47>: test   %cl,%cl
0x00007fff915c0ff8 <strsep+49>: je     0x7fff915c1001 <strsep+58>
0x00007fff915c0ffa <strsep+51>: movb   $0x0,(%r10)
0x00007fff915c0ffe <strsep+55>: mov    %r9,%rdx
0x00007fff915c1001 <strsep+58>: mov    %rdx,(%rdi)
0x00007fff915c1004 <strsep+61>: mov    %r8,%rax
0x00007fff915c1007 <strsep+64>: pop    %rbp
0x00007fff915c1008 <strsep+65>: retq
Run Code Online (Sandbox Code Playgroud)

编辑:string.h肯定是包含在内的,有一个包含它的系统范围的包含文件,但只是为了确保我尝试添加它这个特定的文件.-D_BSD_SOURCE=1是自动添加的编译器选项之一,所以这种情况正在发生.-D_ISOC99_SOURCE=1默认情况下还有一个编译器选项.我尝试添加-std=gnu99(我的编译器,llvm-gcc 4.2,不喜欢-std=gnu90),但它没有解决它.我收到以下编译器错误:

以下是我为此文件获取的编译器警告:

ow_alias.c: In function 'ReadAliasFile':
ow_alias.c:40: warning: implicit declaration of function 'getline'
ow_alias.c:48: warning: implicit declaration of function 'strsep'
ow_alias.c:48: warning: assignment makes pointer from integer without a cast
ow_alias.c:64: warning: assignment makes pointer from integer without a cast
ow_alias.c: In function 'FS_dir_entry_aliased':
ow_alias.c:177: warning: initialization makes pointer from integer without a cast
Run Code Online (Sandbox Code Playgroud)

第二次编辑:在做了一点挖掘之后(感谢gcc -E | grep strsep提示!),我发现问题出在编译器标志上-D_POSIX_C_SOURCE=200112L.此编译器标志阻止了strsep的定义,使编译器进行隐式声明.我从配置脚本中删除了这一切,似乎一切正常.谢谢您的帮助!

Mic*_*urr 5

你需要确保string.h包含它.

如果已经完成,您还需要定义一个功能测试宏来启用声明strsep().添加-D_GNU_SOURCE-D_BSD_SOURCE应该做的伎俩.但是,您可能希望了解如何立即配置内容 - _BSD_SOURCE通常默认情况下启用.也许你告诉gcc用-std=c90或编译-std=c99,这将关闭许多功能测试宏.使用-std=gnu90-std=gnu99代替.

您是否看到诸如"初始化使得指针来自整数而没有强制转换"或"隐式声明函数"的警告?