我是新的C,我今天得到了一個錯誤是:段故障核心轉儲Ç
segmentation fault core dumped
我用gdb跟蹤代碼,我發現,在這行發生錯誤:
if (!strcmp(user_pass, passwddata->passwd))
凡user_pass
是char數組,並passwddata
是結構,passwd
是結構的成員,這也是一種的C哈日陣列,我試圖更改代碼以
if (!strcmp(user_pass, "ttt"))
未發生錯誤,所以我想對結構發生錯誤,如果需要更多的代碼,我可以添加它,在這裏我要了解在什麼情況下可以在結構上發生這樣的錯誤?
下面是代碼:
int main(int argc, char *argv[]) {
mypwent *passwddata; /* this has to be redefined in step 2 */
/* see pwent.h */
char important[LENGTH] = "***IMPORTANT***";
char user[LENGTH];
//char *c_pass; //you might want to use this variable later...
char prompt[] = "password: ";
char *user_pass;
sighandler();
while (TRUE) {
/* check what important variable contains - do not remove, part of buffer overflow test */
printf("Value of variable 'important' before input of login name: %s\n",
important);
printf("login: ");
fflush(NULL); /* Flush all output buffers */
__fpurge(stdin); /* Purge any data in stdin buffer */
if (gets(user) == NULL) /* gets() is vulnerable to buffer */
{
exit(0); /* overflow attacks. */
}
printf("******************* %s\n",user);
/* check to see if important variable is intact after input of login name - do not remove */
printf("Value of variable 'important' after input of login name: %*.*s\n",
LENGTH - 1, LENGTH - 1, important);
user_pass = getpass(prompt);
passwddata = getpwnam(user);
printf("^^^^^^^^^^^^^^^^^^^^^^^^^^ %s\n", user_pass);
if (passwddata != NULL) {
/* You have to encrypt user_pass for this to work */
/* Don't forget to include the salt */
if (!strcmp(user_pass, "ttt")) {
printf(" You're in !\n");
/* check UID, see setuid(2) */
/* start a shell, use execve(2) */
}
}
printf("Login Incorrect \n");
}
return 0;
}
「passwddata」或「passwd」無效。 – cnicutar
請添加如何創建user_pass和passwddata的代碼。我懷疑你沒有實例化指針passwddata – AndyG