运行时错误:变量损坏

时间:2014-01-06 15:32:28

标签: c

我一直在搞乱我的代码......现在当我运行它时,在菜单中,单击第二个选项,当我输入超过15个字符的密码时,我收到此错误? 谁能看到我做错了?它可能是我无法发现的小事 感谢

ERROR:

运行时检查失败#2 - 变量'passwordCheck'周围的堆栈已损坏。

CODE:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>


FILE*fp; 

char *symbols = "#~!$%^&*()+=<>?/@"; // Random Symbol is generated form this list

int main(void) {

char password[4 + 4 + 2 + 2 + 1]; // 4 Upper, 4 Lower, 2 Num, 2 Symbols
int i, j=0, len=sizeof(password)-1;
int menuNum = 0;
char passwordCheck[15+1]; // for checking password length



fp = fopen("passwords.txt", "a+"); //Opens the text file to save the Passwords

srand(time(NULL));
printf("       Main Menu\n");
printf("********************************\n");
printf("\nEnter 1 to Generate a New Password: ");
printf("\n\n");
printf("Enter 2 to Check Old Passwords: ");
printf("\n\n");
printf("Enter 3 to Exit. ");
printf("\n\n");
scanf("%d", &menuNum); // Reads number

if (menuNum == 1) // If 1 is entered on the Menu...
{
    printf("********************************\n");
    printf("\nYour New Password is: \n\n");

// Each Password will Have 12 Characters(4 Uppercase letters, 4 Lowercase letters, 2 Numbers & 2 Symbols)

for (i = 0; i < 4; ++i)
    password[j++] = 'a' + rand() % ('z' - 'a' + 1); // Generates 4 random Lowercase characters 

for (i = 0; i < 4; ++i)
    password[j++] = 'A' + rand() % ('Z' - 'A' + 1); // Generates 4 random Uppercase characters

for (i = 0; i < 2; ++i)
    password[j++] = '0' + rand() % ('0' - '9' + 1); // Generates 2 random numbers

for (i = 0; i < 2; ++i)
    password[j++] = symbols[rand() % strlen(symbols)]; // Generates 2 random symbols

password[j] = '\0';
for(i = 0; i < sizeof(password)-1; ++i)
    {
    char c = password[i];
    j = rand() % len;
    password[i] = password[j];
    password[j] = c;
    } // Shuffles passwords Characters

printf("%s\n\n", password);
printf("********************************\n");
fprintf(fp, "\n%s", password); // Outputs the Generated Passoword to the text file
fclose(fp); // Closes the text file
system("pause");
}

else if (menuNum == 2)// If 2 is entered on the Menu...
{           
    printf("\nEnter your password for checking: ");
    scanf("%s", passwordCheck);  // Reads password
    if (strlen(passwordCheck) > 15) // Checks length of Password
    {
        printf("'%s' is too long. Needs to be less then 15 Characters\n", passwordCheck);
        system("pause");
    }
    else if (strlen(passwordCheck) < 9) // Checks length of Password
    {
        printf("'%s' is too short. Needs to be more then 9 Characters\n", passwordCheck);
        system("pause");
    }
}

}

3 个答案:

答案 0 :(得分:1)

您定义char password[13]。这对于12个字符加上一个空终止符就足够了。

任何对password[13]password[14]等进行索引的尝试都是未定义的行为以及堆栈损坏的原因。类似地,对于passwordCheck[]:您的scanf调用可能会超出声明的空间; strlen在这里没有帮助,因为这将快速计算到第一个空终止符。

答案 1 :(得分:0)

如果这种情况

scanf("%s", passwordCheck);  // Reads password
if (strlen(passwordCheck) > 15) // Checks length of Password

成为现实,你的记忆已经被破坏了。

您应该在这里使用fgets

 fgets(passwordCheck, sizeof passwordCheck, STDIN);

唯一的问题是,如果用户输入20个字符但密码为15且前15个字符正确,则会将密码识别为正确,即使用户在技术上输入了错误的密码。如果你想要解决这个问题,你必须逐个读取输入字符以使其安全并手动构建字符串,或者你使缓冲区变得更大而密码仍然只有15个长,那么你可以看看用户是否输入了比他应该更长的字符串,代码仍然是安全的。

答案 2 :(得分:0)

您定义char passwordCheck [16],当您输入16或> 16个characetrs,passwordCheck堆栈,并且后面的内存被您的输入损坏。 顺便说一下,你的编译是什么?gcc和vc可以运行你的代码而没有错误