c - 是什么导致了这个段错误?

标签 c segmentation-fault cs50 vigenere

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

#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

最佳答案

看起来它在 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

关于c - 是什么导致了这个段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074190/

相关文章:

python - 我如何从 python 中的字符串中读取定义的值,就像我在 C 中使用 scanf 所做的那样

c - 我在哪里可以获得 PalmOS SDK?

c - 尝试声明字符串数组时出现段错误

c - 将文件中的数据读入C中的结构

c - Linux - 从 malloc 复制虚拟内存地址或移动虚拟内存地址

c - 分割。 strcmp [C]

matlab - Mex 函数在完成时崩溃

c - FFTW计划段错误

c - 重新提示用户,直到他/她输入大于 1 的正整数值

c - 'C' 中变量和指针的字符串连接