本文介绍了C ++访问冲突读为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发应用程序,它将通过使用C ++ / Winsock的pop / imap协议从服务器接收邮件。



因为代码很大, ,我给你在pastebin的链接:

http://pastebin.com/uCKcTQsj



它没有任何编译器错误,因此可以妥善处理。



获得不同的工作结果,有时一切正常,但通常我得到的结果:


未处理的异常在0x773f15de(ntdll.dll)在alex.exe:0xC0000005:
访问冲突读取位置0x00648000。



为什么我得到不同的工作结果(通常存取违规,但有时它工作)?



有没有任何方法来捕获和处理违规异常(可能是从较低的secrity响铃,据我所知,这些异常是从ntdll.dll在本机模式下抛出)来解决这个问题?



谢谢,最好的问候!

解决方案

/ p>

  char buffer [0x1000]; 

但是你在几个地方将它当作一个空结尾的C字符串,而不必确定它实际上是null



例如,在 Inet :: GetResponse()

  int result = recv((* sock),buffer,strlen(buffer),0)

我不认为 strlen(buffer)将返回任何有意义的。



处理接收到的数据,不能保证数据将被null终止 - 即使协议确实终止了带有null的记录。使用TCP a recv()只能返回已发送记录的一部分,因此即使在发送null的情况下,也可能不会收到。具体来说,POP3和IMAP协议不会终止带有空八位字节的响应。因此,除非你确保'\0'在那里, strlen()和朋友不能使用。 / p>

您需要仔细检查如何处理缓冲区大小和该代码中接收的数据量。


I'm developing the application, which will get mails from servers via pop/imap protocols using C++/Winsock.

Cause, the code is large to paste here , I give you the link on pastebin:
http://pastebin.com/uCKcTQsj

It doesn't have any compiler errors, so it can be copmpiled well.

I'm getting different result of its work, sometimes all working ok, but often I get the result:

Unhandled exception at 0x773f15de (ntdll.dll) in alex.exe: 0xC0000005:
Access violation reading location 0x00648000.

Why do I get different result of its work ( often cathing access violation, but sometimes it works )?

Are there any ways to catch and handle violation exceptions ( may be from lower secrity rings, as I understand , these exceptions are throwing from ntdll.dll in native mode ) to fix this problem?

Thanks, best regards!

解决方案

Your header struct contains this array:

char buffer[0x1000];

But you treat it as a null terminated C string in several places without making sure that it is actually null terminated.

For example, in Inet::GetResponse():

int result = recv((*sock), buffer, strlen(buffer), 0)

I don't think that strlen(buffer) will return anything meaningful.

But even when you're dealing with received data, there's no guarantee that the data will be null terminated - even if the protocol does terminate records with nulls. With TCP a recv() can return only part of a sent record, so even in situations where a null is sent, it might not be received yet. In particular, the POP3 and IMAP protocols do not terminate responses with a null octet. So unless you make sure the '\0' is there, strlen() and friends cannot be used.

You need to carefully reexamine how you're handling the buffer size and the amount of data received in that code.

这篇关于C ++访问冲突读为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 05:45