【问题标题】:Segmentation Fault on string conversion?字符串转换的分段错误?
【发布时间】:2017-04-14 16:07:59
【问题描述】:

我正在做一个加密项目,我正在做一个简单的测试,它从终端获取一个文件名并运行我的加密。我有以下加密代码,但出现以下分段错误:

terminate called after throwing an instance of 'std::logic_error'
  what():  basic_string::_M_construct null not valid<br><br>
Program received signal SIGABRT, Aborted.
0x00007ffff74ab428 in __GI_raise (sig=sig@entry=6)
    at ../sysdeps/unix/sysv/linux/raise.c:54
54  ../sysdeps/unix/sysv/linux/raise.c: No such file or directory.

使用 gdb 运行跟踪后,我确认此故障是在以下 loc 之后触发的:

string plain(reinterpret_cast(fileContents), fileLength);

下面我的main函数调用了这段代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <limits>
#include <sstream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "rc4_enc.c"
#include "rc4_skey.c"

using namespace std;
void foo(int);

int main(int argc, char * argv[]){
    if(argc!=2){
        cout << "Please enter the filename that you want to encrypt or decrypt\n";
        exit(2);
    }
    int fd;
    fd = open(argv[1], O_RDONLY);
    cout << "The file descriptor is " << fd << endl;
    //close(fd);// Can I modify it if it's closed?
    foo(fd);

    return 0;
}

功能如下:

void foo(int fd) {

     int fileLength = lseek(fd, 0, SEEK_END);
     unsigned char* fileContents;
     fileContents = (unsigned char*) calloc(fileLength, sizeof(char));
     pread(fd, fileContents, fileLength, 0);
     string plain(reinterpret_cast<char*>(fileContents), fileLength); // Segfault happens here. But why? 
        RC4_KEY key;
        int length = plain.size();
        unsigned char *buf = (unsigned char*)malloc(length+1);
        memset(buf, 0, length+1);

        ifstream pass;
        pass.open("pass.txt");
        if(!pass.good()){
            return;
        }
        else{
            string password;
            getline(pass,password);
            RC4_set_key(&key, password.length(), (const unsigned char*)password.c_str());
        }
        RC4(&key, length, (const unsigned char*)plain.c_str(), buf);
        string result((char*)buf, length);
        free(buf);
        const char *outputBuf = result.c_str();
        pwrite(fd, outputBuf, result.length(), 0);
        ftruncate(fd, result.length());
    }

【问题讨论】:

  • 你为什么要#include .c 文件?
  • 在预读之后尝试filecontents[filelength-1] = '\0';;只是为了尝试;我想你的filecontents 没有正确终止;如果它有效,您还必须将长度修正 1。
  • @NeilButterworth 这些 .c 文件包含使用我已经集成的 openssl 中的 RC4_set_key()RC4() 函数所需的所有依赖项。
  • @StephanLechner 好的,我会测试一下。
  • 你应该链接到他们,而不是包括他们。包含 .c 或 .c++ 文件是非常糟糕的做法。

标签: c++ casting rc4-cipher


【解决方案1】:

我会将此作为评论,但没有足够的声誉。

文件有多大? calloc 会失败并返回 NULL 吗? 即使不是这样,检查 calloc 的返回值也可能是明智的。或者使用 new 操作符和 try/catch。

【讨论】:

  • 该文件很短,只有 6 个字节,但在将其集成到我的文件系统项目之前,我有一系列文件可以对其进行测试。这在 6 个字节上出错。我会检查 calloc 的回报
  • 是的,就是这样!感谢您的演练。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-14
  • 2014-02-27
  • 2011-03-11
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多