是啥导致了这种分段错误?

Posted

技术标签:

【中文标题】是啥导致了这种分段错误?【英文标题】:What is causing this segmentation fault?是什么导致了这种分段错误? 【发布时间】:2014-04-15 03:33:50 【问题描述】:

这里是菜鸟。弹出打开我的虚拟机以编辑程序,但是(据我所知),在我进行任何更改之前,我遇到了以前没有发生的分段错误。我尝试评论可能的原因,但无济于事。

#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main (int argc, string argv[])


    int keyOk = 0;
    string keyword = argv[1];
    int keyLength = strlen(keyword);

    do
        
       if (argc != 2)
        
            printf("TOo many words\n");
            return 1; 
                
        else    
        
            for (int i = 0; i < keyLength; i++)
            
                if(isalpha(keyword[i] == 0 ))
                
                    printf("Only use alpha\n");
                    return 1;
                       
            
            keyOk = 1;
        
    
    while(keyOk == 0);

    string cipherInput = GetString();
    int cipherLength = strlen(cipherInput);
    int j = 0;


    for(int i = 0; i < cipherLength; i++)
        
            if(isalpha(cipherInput[i]))
            
                if(islower(keyword[j]))
                
                    if(islower(cipherInput[i]))
                    
                        printf("%c", ((((cipherInput[i] - 97)+(keyword[j]-97))%26)+97));
                    
                    else
                    
                        printf("%c", ((((cipherInput[i] - 65)+(keyword[j]-97))%26)+65));
                    

                
                else
                
                    if(islower(cipherInput[i]))
                    
                        printf("%c", ((((cipherInput[i] - 97)+(keyword[j]-65))%26)+97));
                    
                    else
                    
                        printf("%c", ((((cipherInput[i] - 65)+(keyword[j]-65))%26)+65));
                    
                

                j = i % cipherLength;             
            
            else
            
                printf("%c", cipherInput[i]);
            
             
    //printf("%s\n", keyword);
    printf("\n");

GDB 输出,我不明白它的含义。通常它只是给我一个行号...

New LWP 31749]
Core was generated by `./vigenere'.
Program terminated with signal 11, Segmentation fault.
#0  __strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50
50      movdqu  (%edi), %xmm1

【问题讨论】:

C? string?也许int main(int argc, char* argv[]) 您需要实际传递一个参数,例如./vigenere blabla。否则argv[1]NULL。您的代码应在访问 argv[1] 之前检查 argc &gt; 1 另外,当您发布使用像 cs50.h 这样的非标准标头的代码时,您应该发布该标头的内容,否则我们只是在猜测像 stringGetString 这样的东西. 请注意if(isalpha(keyword[i] == 0 )) 中的非预期括号应为if (isalpha((unsigned char)keyword[i]) == 0) 甚至if (!isalpha((unsigned char)keyword[i]))。不过,这不会导致崩溃。 &lt;cs50.h&gt; 标头在这里相当常见(搜索 [c] cs50.h 的结果有 87 个)。假设typedef char *string;。不是很好的typedef,但这不是学生的错。 【参考方案1】:

看起来它在 main...之前崩溃了?

curl 在我的电脑上刚刚开始这样做。我让它在后台循环中运行,检查网络状态,它刚刚开始像这样死去:

(gdb) thread apply all bt

Thread 1 (Thread 0xf69da780 (LWP 23683)):
#0  __strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50
#1  0xf6a064a6 in asn1_array2tree () from /usr/lib/i386-linux-gnu/libtasn1.so.6
#2  0xf6d04980 in ?? () from /usr/lib/i386-linux-gnu/libgnutls.so.30
#3  0xf6ce17f0 in ?? () from /usr/lib/i386-linux-gnu/libgnutls.so.30
#4  0xf775dc55 in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0xff8941a4, env=0xff8941ac) at dl-init.c:72
#5  0xf775dd7e in call_init (env=0xff8941ac, argv=0xff8941a4, argc=1, l=<optimized out>) at dl-init.c:30
#6  _dl_init (main_map=<optimized out>, argc=1, argv=0xff8941a4, env=0xff8941ac) at dl-init.c:120
#7  0xf774ea5f in _dl_start_user () from /lib/ld-linux.so.2

dmesg 也无济于事:

[185140.877300] curl[20212]: segfault at 2000 ip 00000000f74b6b06 sp 00000000ff9048a4 error 4 in libc-2.24.so[f7438000+1b1000]
[185226.272389] curl[20938]: segfault at 2000 ip 00000000f74ecb06 sp 00000000ff86a6d4 error 4 in libc-2.24.so[f746e000+1b1000]
[185311.507840] curl[21649]: segfault at 2000 ip 00000000f757bb06 sp 00000000ff9ae8a4 error 4 in libc-2.24.so[f74fd000+1b1000]

使用aptitude reinstall curl libgnutls30 libtasn1-6 libc6 重新安装为我修复了它:

root@eeyore:/tmp# curl
Segmentation fault (core dumped)
139 root@eeyore:/tmp$ aptitude reinstall curl libgnutls30 libtasn1-6 libc6
The following packages will be REINSTALLED:
  curl libc6 libc6:amd64 libgnutls30 libgnutls30:amd64 libtasn1-6 libtasn1-6:amd64 
0 packages upgraded, 0 newly installed, 7 reinstalled, 0 to remove and 0 not upgraded.
Need to get 7,306 kB of archives. After unpacking 0 B will be used.
Get: 1 http://deb.debian.org/debian stable/main i386 libc6 i386 2.24-11+deb9u3 [2,470 kB]
Get: 2 http://deb.debian.org/debian stable/main amd64 libc6 amd64 2.24-11+deb9u3 [2,693 kB]
Get: 3 http://deb.debian.org/debian stable/main i386 libgnutls30 i386 3.5.8-5+deb9u3 [913 kB]
Get: 4 http://deb.debian.org/debian stable/main amd64 libgnutls30 amd64 3.5.8-5+deb9u3 [895 kB]                                                                                                                      
Get: 5 http://deb.debian.org/debian stable/main i386 libtasn1-6 i386 4.10-1.1+deb9u1 [53.6 kB]                                                                                                                       
Get: 6 http://deb.debian.org/debian stable/main amd64 libtasn1-6 amd64 4.10-1.1+deb9u1 [50.6 kB]                                                                                                                     
Get: 7 http://deb.debian.org/debian-security stable/updates/main i386 curl i386 7.52.1-5+deb9u5 [230 kB]                                                                                                             
Fetched 7,306 kB in 7s (971 kB/s)                                                                                                                                                                                    
Preconfiguring packages ...
(Reading database ... 287422 files and directories currently installed.)
Preparing to unpack .../libc6_2.24-11+deb9u3_i386.deb ...
Unpacking libc6:i386 (2.24-11+deb9u3) over (2.24-11+deb9u3) ...
Preparing to unpack .../libc6_2.24-11+deb9u3_amd64.deb ...
Unpacking libc6:amd64 (2.24-11+deb9u3) over (2.24-11+deb9u3) ...
Setting up libc6:i386 (2.24-11+deb9u3) ...
Setting up libc6:amd64 (2.24-11+deb9u3) ...
(Reading database ... 287422 files and directories currently installed.)
Preparing to unpack .../libgnutls30_3.5.8-5+deb9u3_i386.deb ...
Unpacking libgnutls30:i386 (3.5.8-5+deb9u3) over (3.5.8-5+deb9u3) ...
Preparing to unpack .../libgnutls30_3.5.8-5+deb9u3_amd64.deb ...
Unpacking libgnutls30:amd64 (3.5.8-5+deb9u3) over (3.5.8-5+deb9u3) ...
Preparing to unpack .../libtasn1-6_4.10-1.1+deb9u1_i386.deb ...
Unpacking libtasn1-6:i386 (4.10-1.1+deb9u1) over (4.10-1.1+deb9u1) ...
Preparing to unpack .../libtasn1-6_4.10-1.1+deb9u1_amd64.deb ...
Unpacking libtasn1-6:amd64 (4.10-1.1+deb9u1) over (4.10-1.1+deb9u1) ...
Preparing to unpack .../curl_7.52.1-5+deb9u5_i386.deb ...
Unpacking curl (7.52.1-5+deb9u5) over (7.52.1-5+deb9u5) ...
Setting up curl (7.52.1-5+deb9u5) ...
Setting up libtasn1-6:i386 (4.10-1.1+deb9u1) ...
Setting up libtasn1-6:amd64 (4.10-1.1+deb9u1) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...
Processing triggers for man-db (2.7.6.1-2) ...
Setting up libgnutls30:i386 (3.5.8-5+deb9u3) ...
Setting up libgnutls30:amd64 (3.5.8-5+deb9u3) ...
Processing triggers for libc-bin (2.24-11+deb9u3) ...

root@eeyore:/tmp# curl
curl: try 'curl --help' or 'curl --manual' for more information

【讨论】:

天哪,我完全忘记了这个问题……我在将近 4 年前问过这个问题!我什至不...确定...如果这解决了它大声笑。对不起,如果我是你,我不会在这方面投入更多的精力!你是怎么找到这个的? @komali_2 在我得到堆栈跟踪后,我用谷歌搜索了有问题的行 __strlen_sse2_bsf () at ../sysdeps/i386/i686/multiarch/strlen-sse2-bsf.S:50,而你的是第一个结果。

以上是关于是啥导致了这种分段错误?的主要内容,如果未能解决你的问题,请参考以下文章

是啥导致发生分段错误?

OpenCV 分配导致 std::thread::join 中的段错误

为啥释放内存会导致分段错误?

确定导致分段错误的代码行?

带有 ifort 的 tracebackqq() 导致分段错误

为啥重新声明 std::cout 会导致分段错误?