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

问题描述

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

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

因为代码很大,无法粘贴在这里,我给你pastebin上的链接:
http://pastebin.com/uCKcTQsj

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.

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

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

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 )?

是否有任何方法可以捕获和处理违规异常(可能来自较低的保密环,据我所知,这些异常是在本机模式下从 ntdll.dll 抛出的)来解决此问题?

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?

谢谢,最好的问候!

推荐答案

你的头结构包含这个数组:

Your header struct contains this array:

char buffer[0x1000];

但是你在几个地方把它当作一个空终止的 C 字符串,而没有确保它实际上是空终止的.

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

例如,在Inet::GetResponse()中:

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

我认为 strlen(buffer) 不会返回任何有意义的东西.

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

但即使您正在处理接收到的数据,也不能保证数据会以空值终止——即使协议确实以空值终止记录.使用 TCP,recv() 只能返回已发送记录的一部分,因此即使在发送空值的情况下,也可能尚未收到.特别是,POP3 和 IMAP 协议不会用空八位字节终止响应.因此,除非您确保 '' 存在,否则不能使用 strlen() 和朋友.

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 '' 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++访问冲突阅读为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:07