簡體   English   中英

C ++:為什么會出現此分段錯誤(可能發生在main之前?)

[英]C++: Why am I getting this segmentation fault (possibly occuring before main?)

我剛剛完成了大部分調試工作(我認為!),現在我的代碼可以正確編譯。 但是,當我嘗試運行它時,它甚至會在main的第一行之前返回一個段錯誤! 我想知道是否可能是內存問題,與我的struct定義中的字符串有關,但是我什至還沒有初始化它們,所以我不知道這怎么工作。 我想的另一件事是,在設置字符串時可能需要使用new運算符,但是據我所知,我的書以相同的方式聲明了字符串(盡管它們不是結構的一部分。) )

無論如何,這是我的代碼! 我們應該破解一些(都非常糟糕)都使用相同鹽的密碼。

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <crypt.h>
using namespace std;

/* Read in a large, standard dictionary.
   Encrypt each word in the dictionary using the crypt() function
   Read in the password file and check each user's password to see
   whether it matches one of the dictionary entries
   Output the passwords for all of the accounts you have
   broken. */
struct ListNode{
  string user;
  string salt;
  string encrypted;
  string password;
  ListNode* next;
};


int main(){

  cout<<"Hello world\n\n";
  /* Initializes singly linked list to hold user data */
  ListNode *root;
  ListNode *conductor;
  root = new ListNode;
  conductor = root;

  cout<<"After initializing list\n\n";
  string temp;

  //creates and opens filestream to password file
  ifstream psswdFile ("simplepsswd.txt");

  if(psswdFile.is_open()){
    //we assume there is at least one line, and initialize the head of
    //the list
    std::getline(psswdFile, conductor->user, ':');
    std::getline(psswdFile, conductor->salt, ':');
    std::getline(psswdFile, conductor->encrypted);
    //create first node
    //while there's more lines (store username in temp so we can
    //check)
    cout<< "Before password loop\n\n";
    while(std::getline(psswdFile, temp, ':')){
      //create next node
      conductor->next = new ListNode;
      conductor = conductor->next;
      conductor->next = NULL;
      //set data fields
      conductor->user = temp;
      //I know we don't NEED to store the salt but what if you decided
      //you wanted it later
      std::getline(psswdFile, conductor->salt, ':');
      std::getline(psswdFile, conductor->encrypted);
    }
  }

  //open the dictionary file
  ifstream dicFile("/usr/share/myspell/en_US.dic");
  string dicEntry;
  string dicEncrypted;

  if(dicFile.is_open()){
    int loc;
    //loop through words in dictionary
    cout<<"Before dictionary loop\n\n";
    while(std::getline(dicFile, dicEntry)){
      //check for weird flags and whatever.
      int loc = dicEntry.find('/');
      if(loc > 0){
    //if they're there, get rid of them!
    dicEntry.resize(loc);
      }
      //convert the entry to a c-string so we can use crypt on it
      char* cString;
      strcpy(cString, dicEntry.c_str());
      cString = crypt(cString, "AB");
      dicEncrypted = cString;
      //loop through the list comparing to encrypted passwords
      conductor = root;
      if(conductor->encrypted == dicEncrypted){
    conductor->password = dicEntry;
      }
      //password could occur more than once!
      while(conductor->next != NULL){
    conductor = conductor->next;
    if(conductor->encrypted == dicEncrypted){
      conductor->password = dicEntry;
    }
      }
    }
  }
  cout<<"Before print loop\n\n";
  conductor = root;
  if(conductor->password != ""){
    cout << "User " << conductor->user << "'s password is " << conductor->password << "\n\n";
  }
  while(conductor->next != NULL){
    conductor = conductor->next;
    if(conductor->password != ""){
      cout<<"User " << conductor->user << "'s password is " << conductor->password <<"\n\n";
    }
  }
  return 0;
}

好的,所以我嘗試了注釋中的某些操作,好像在創建第一個ListNode時就遇到了段錯誤!

立即出現的一個問題是您正在寫一個未初始化的指針:

  //convert the entry to a c-string so we can use crypt on it
  char* cString;
  strcpy(cString, dicEntry.c_str());
  cString = crypt(cString, "AB");
  dicEncrypted = cString;

cString是指向who-knows-where的未初始化的指針。 然后,使用strcpy函數將字符寫入此內存地址。 這是未定義的行為,很可能是分段錯誤。

您必須分配內存,以便strcpy有空間寫入字符串(加上終止NULL)。

  char cString[100]; // or whatever number is large enough
  strcpy(cString, dicEntry.c_str());
  cString = crypt(cString, "AB");
  dicEncrypted = cString;

但是,即使這樣也不安全,因為100可能不夠。 您需要發布crypt函數以獲得更可靠的答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM