我有一个小程序试图模拟读取(非标准)数据包标头并进行处理。
当我运行程序时,我遇到一个奇怪的seg错误,在使用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)
答案 0 :(得分:5)
unsigned char header[48];
...
memset(&header, 0, 96);
这听起来不对。