简体   繁体   English

释放内存时出现分段错误

[英]Segmentation fault while freeing memory

I wish to implement a web socket handshake and for that I am using the following code snippet. 我希望实现Web套接字握手,为此,我正在使用以下代码段。 But I get segmentation fault when I start freeing the memory which I allocate dynamically. 但是,当我开始释放动态分配的内存时,出现了分段错误。 Error shows up in the place where I use free function for the first time. 我第一次使用自由功能的地方出现错误。 Please help. 请帮忙。

char rbuf[656];        
char handshake[800];
char *handshake_part2, *handshake_part3,*key,*magic,*final;
unsigned char hash [20];

key=strndup(rbuf+359, 24);     
magic = malloc(strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")+2);
strcpy(magic,"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
final = malloc (60);
final = strcat(key,magic);   
SHA1(final,strlen(final),hash);
base64(hash, sizeof(hash));
handshake_part2= malloc(400);
handshake_part2= base64(hash, sizeof(hash));
strcpy (handshake,"HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: Websocket\r   \nConnection: Upgrade\r\nSec-WebSocket-Accept: ");
strcat(handshake,handshake_part2);
handshake_part3= malloc(400);
handshake_part3="\r\nWebSocket-Origin: http://localhost:9605\r\nWebSocket-Location: ws://localhost:9609/\r\n\r\n";
strcat(handshake,handshake_part3);
printf("Response Header :\n%s", handshake);

free(handshake_part3);
handshake_part3=NULL;
printf("Free 1");
free(handshake_part2);
handshake_part2=NULL;
printf("Free 2");
free(final);`
final=NULL;
printf("Free 3");
free(magic);
magic=NULL;
printf("Free 4");
free(key);

You are reassigning handshake_part3 to be the constant string "\\r\\n..." ; 您正在将handshake_part3重新分配为常量字符串"\\r\\n..." did you mean to strcpy() (preferably strncpy() or equivalent!) this in instead? 您的意思是用strcpy() (最好是strncpy()或同等的东西!)代替吗?

You're freeing a constant string. 您正在释放常量字符串。 The memory is leaking because after you call malloc, you assign the constant string (and the malloc'd memory is leaked). 内存泄漏是因为调用malloc之后,您分配了常量字符串(并且malloc的内存泄漏)。

handshake_part3= malloc(400); 
handshake_part3="\r\nWebSocket-Origin: http://localhost:9605\r\nWebSocket-Location: ws://localhost:9609/\r\n\r\n";

You assign a string literal to handshake_part3 , and then try to free it... This causes the mallocated buffer to leak, and your free to crash. 您将一个字符串常量分配给handshake_part3 ,然后尝试释放它……这将导致分配错误的缓冲区泄漏,从而使您的free崩溃。 You should strcpy that string literal to the allocated buffer, or avoid the allocation and the freeing. 您应该将该字符串文字strcpy到已分配的缓冲区,或者避免分配和释放。

Look at this code: 看下面的代码:

final = malloc (60);
final = strcat(key,magic); 

what you do is you abandon the newly allocated final and override it with a key 's address. 您要做的是放弃新分配的final ,并用key的地址覆盖它。 later on you delete it twice (once via final and than via key ), which is not allowed. 之后,您将其删除两次(一次通过final ,一次通过key ),这是不允许的。

That's the only thing I noticed at a glance but I have a feeling there may be more... 这是我一眼看到的唯一一件事,但我感觉可能还有更多...

Edit: And looking at other answers I can see there are indeed "more" 编辑:查看其他答案,我可以看到确实存在“更多”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM