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

时间:2012-12-12 06:41:36

标签: c macos assembly 32bit-64bit x86-64

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

我正在尝试在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) ) {

strlen(path_segment)块中对if的调用失败,因为path_segment是一个越界地址。

使用GDB逐步采取措施,这是我发现的内容。进入使用path_segment初始化strsep的调用,gdb会给我:

path_pointer = (ASCII *) 0x7fff5fbf99a5 "/uncached/bus.0/interface"

执行下一行后,path_pointer已提升,正如我所料:

path_pointer = (ASCII *) 0x7fff5fbf99a6 "uncached/bus.0/interface"

但gdb报道:

path_segment = (ASCII *) 0x5fbf99a5 <Address 0x5fbf99a5 out of bounds>

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

我知道这里有很多内容,如果问题深深地隐藏在软件包构建过程中,我还没有提供足够的信息。在我看来,虽然这可能有一个简单的解决方案,因为该函数基本上正常工作,但只是返回错误的大小指针。我对64位与32位系统的错综复杂的经验很少,所以我想知道是否有人能发现明显的东西,或者提供下一步调试此问题的说明。有趣的是,当我在gdb(strsep)中手动运行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)

并在那个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

编辑: 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

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

1 个答案:

答案 0 :(得分:5)

您需要确保包含string.h

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

您是否看到过诸如“初始化从没有强制转换的整数生成指针”或“隐式函数声明”等警告?

相关问题