代码之家  ›  专栏  ›  技术社区  ›  WannabeArchitect

到达main-c之前的分段错误++

  •  2
  • WannabeArchitect  · 技术社区  · 5 年前

    我正在编写一个代码来找到一个合适的输入,它将为sha-1散列函数生成特定的输出。

    我遇到的问题是我的代码引发了一个分段错误,但是 gdb 发现它在输入时引发以下错误 main() 在执行任何其他代码之前:

    Program received signal SIGSEGV, Segmentation fault.
    __strncpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S:636
    636 ../sysdeps/x86_64/multiarch/strcpy-sse2-unaligned.S: No such file or directory.
    
    

    这是我的代码:

    #include <iostream>
    #include <cstdlib>
    #include <cstring>
    
    #include "sha1.hpp"
    
    int main() {
        char *prefix = "SHA1sha1";
        char *suffix = "chicken and beer";
        std::string hashvalue = "nonzero";
        char *line = "just some dummy string";
        int loop_number = 0;
    
        while (hashvalue.c_str()[0] != '0' || hashvalue.c_str()[1] != '0') {
            // change prefix
            strncpy(prefix, hashvalue.c_str(), 8);
            // hash the concatenated string of prefix and suffix
            strncpy(line, prefix, 8);
            strncat(line, suffix, strlen(suffix));
            hashvalue = sha1(line);
    
            loop_number++;
            if (loop_number % 1000 == 0) std::cout << loop_number << "th loop, hash value: " << hashvalue << std::endl;
        }
    
        std::cout << "Found prefix: " << prefix << " with hash value: " << hashvalue << std::endl;
    
        return 0;
    }
    

    这个 sha1.hpp 不是我执行的,而是从这里带走的: http://www.zedwood.com/article/cpp-sha1-function

    我变了 sha1.h 沙一水电站 不过,这可能不是造成分割错误的原因。

    现在我 尝试用错误消息和关键字“segmentation fault before main”来搜索此问题的解决方案,这篇文章似乎也遇到了类似的问题: Segmentation Fault before main

    然而,我已经研究了两个建议的解决方案,但找不到一个适合我的。

    1. 我认为我的代码在堆栈中没有太多变量。事实上,我试过用函数注释 sha1() 以防万一,但同样的问题也发生了。

    2. 我已经初始化了所有 char* std::string 在我的代码使用前。

    仅供参考,我正在使用 g++ 编译我的C++代码。

    任何帮助或推动正确的方向将非常感谢。

    2 回复  |  直到 5 年前
        1
  •  4
  •   kiran Biradar    5 年前

    您正在修改不可变的内容。

    // change prefix
    strncpy(prefix, hashvalue.c_str(), 8);
    // hash the concatenated string of prefix and suffix
    strncpy(line, prefix, 8);
    strncat(line, suffix, strlen(suffix)); 
    

    请按如下所示更改声明。

    char prefix[100] = "SHA1sha1";
    char suffix[200] = "chicken and beer";
    char line[200] = "just some dummy string
    

    而且,我想

    while (hashvalue.c_str()[0] != '0' || hashvalue.c_str()[1] != '0') {
    

    应该是

    while (hashvalue.c_str()[0] != '0' && hashvalue.c_str()[1] != '0') {
    

    更新:

    德摩根定律指出,

    not (A and B) = not A or not B
    

    同样,个人选择使用他们想要的任何形式

        2
  •  4
  •   Vlad from Moscow    5 年前

    字符串文本在C++中具有常量字符数组的类型,任何修改字符串文本的尝试都会导致未定义的行为。您正在尝试至少在此语句中修改字符串文字:

    strncpy(prefix, hashvalue.c_str(), 8);
    

    应该使用字符数组或类型为 std::string 是的。

    注意这个;例如 if 陈述

    while (hashvalue.c_str()[0] != '0' || hashvalue.c_str()[1] != '0') {
    

    可以写得简单得多:

    while (hashvalue[0] != '0' || hashvalue[1] != '0') {
    

    尽管这种情况似乎没有道理。也许你的意思是

    while ( hashvalue.size() > 1 ) {
    

    你还需要包括标题 <string>

    #include <string>