0

我有一个小程序试图模拟读取(非标准)数据包头并处理它。

当我运行程序时,我遇到了一个奇怪的段错误,在使用 gdb 分析时,我看到代码完全执行,然后给我一个“ 0x00000000 in ?? ()”。

我以前从未见过这种错误,也不知道是什么原因造成的。

我将代码编译为gcc -g header.c -lcrypto -lssl

这是代码

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include"hash_list.c"

void process(unsigned char header[48]);

int main()
{

    unsigned char header[48];
    unsigned short int responseCode_Fail = 255;
    unsigned short int responseCode_Pass = 0;
    unsigned short int packlen = 65000;
    long long int packetnumber = 2345678;

    memset(&header, 0, 96);
    strcpy(header,"FILELST");
    memcpy(&header[8], &responseCode_Fail, sizeof(responseCode_Fail));
    memcpy(&header[10], &packlen, sizeof(packlen));
    memcpy(&header[16], &packetnumber, sizeof(packetnumber));

    unsigned char result[MD5_DIGEST_LENGTH];
    calcmd5("sample.txt", result);
    memcpy(&header[32], &result, sizeof(result));

    process(header);

    return 0;
}


void process(unsigned char header[48])
{
    unsigned short int responseCode;
    unsigned short int packlen;
    long long int packetnumber;
    unsigned char md5hash[MD5_DIGEST_LENGTH];

    memcpy(&responseCode, &header[8], sizeof(responseCode));
    memcpy(&packlen, &header[10], sizeof(responseCode));
    memcpy(&packetnumber, &header[16], sizeof(packetnumber));
    memcpy(&md5hash, &header[32], sizeof(md5hash));
    printmd5(md5hash);

    printf("CODE: %hu\n",responseCode);
    printf("LEN: %hu\n",packlen);
    printf("PNO: %lld\n",packetnumber);
    if(strncmp(header, "LULZ?", 8) == 0)
    {
        printf("Recieved Peer query.\n");
    }
    else if(strncmp(header, "LULZ.", 8) == 0)
    {
        printf("Recieved Peer query confirmation.\n");
    }
    else if(strncmp(header, "FILELST", 8) == 0)
    {
        printf("Recieved File list\n");
    }
    else if(strncmp(header, "DWNLOAD", 8) == 0)
    {
        printf("Recieved Download request.\n");
    }
    else if(strncmp(header, "UPLOAD", 8) == 0)
    {
        printf("recieved upload from peer ?\n");
    }
    else if(strncmp(header, "ERROR", 8) == 0)
    {
        printf("Recieved Error\n");
    }
    else if(strncmp(header, "LIST", 8) == 0)
    {
        printf("Recieved Peer query for filelist.\n");
    }

    return;

}

这是我得到的输出

4768bfdd77920fe0b4f25f173e568266
CODE: 255
LEN: 65000
PNO: 2345678
Recieved File list
Segmentation fault (core dumped)
4

1 回答 1

5
unsigned char header[48];

...

memset(&header, 0, 96);

这听起来不对。

于 2013-03-19T21:52:39.193 回答