세분화 오류는 만져서는 안되는 메모리를 사용하려고한다는 것을 의미하지만 내 코드에서 어디에서 오는지 알 수 없습니다. 나는 vigenere의 암호를 사용하여 일반 텍스트를 암호화하는 과제를위한 프로그램을 작성했습니다. 잘 컴파일되지만 명령 줄 인수로 실행하면 분할 오류가 발생합니다.
#include <cs50.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
int main(int argc, string argv[])
{
// check to make sure the user entered a key
if(argc != 2)
{
printf("You need to enter a key, and only one. Please enter an alphabetical key. \nSyntax: ./vigenere key \n");
exit (1);
}
// check to make sure the key is alphabetical
string k = argv[1];
if(isalpha(k) == false)
{
printf("Pleas enter an alphabetical key.\n");
exit (2);
}
// Get a string of plaintext
printf("Please enter your secret messege.\n");
string p = GetString();
// Encipher
int lk = strlen(k);
int lp = strlen(p);
for(int i = 0, j = 0; i < lp; i++, j++)
{
if(isupper(k[j]))
{
tolower(k[j]);
}
if(j > lk)
{
j = 0;
}
if(isalpha(p[i]))
{
if (islower(p[i]))
{
printf("%c", ((((p[i] - 97) + (k[j] - 97)) %26) +97));
}
else
{
printf("%c", ((((p[i] - 65) + (k[j] - 97)) %26) +65));
}
}
else
{
printf("%c", p[i]);
}
}
printf("\n");
return 0;
}
이 코드는 의심스러워 보입니다.
if(isupper(k[j]))
{
tolower(k[j]);
}
if(j > lk)
{
j = 0;
}
k [j]를 사용하지만 나중에 확인하면 j가 lk보다 클 수 있음을 알 수 있습니다. 따라서 k [j]는 범위를 벗어날 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다