导致此分段错误的原因是什么?

时间:2014-04-15 03:33:50

标签: c segmentation-fault cs50 vigenere

Noobie在这里。弹出我的虚拟机来编辑程序,但是(据我记忆所知),在我做任何更改之前,我遇到了之前没有发生的分段错误。我试着评论可能的原因,但没有用。

#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

1 个答案:

答案 0 :(得分:1)

看起来好像在主...之前崩溃了??

刚刚开始这样做,我的电脑上的

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