在 recvfrom 调用中使用时,向量指向未初始化的字节

发布于 2024-10-09 21:26:04 字数 733 浏览 3 评论 0原文

在我正在编写的函数中,我试图返回指向无符号字符向量的指针。相关代码如下。

std::vector<unsigned char> *ret = new std::vector<unsigned char>(buffSize,'0');
//Due to suggestions...
int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);
//Forgot to include this in the original
ret->resize(n);
// display chars somehow just for testing
for(std::vector<unsigned char>::iterator it=ret->begin(); it<ret->end();it++)
{
    std::cout<<*it;
}
std::cout<<std::endl;
...
return ret;

当我通过 valgrind 运行此命令时,我收到错误消息,内容涉及 recvfrom 中的缓冲区如何指向未初始化的字节。自从我将其替换为无符号字符数组以来,我已将其范围缩小到向量,并且一切正常。有什么建议吗?

编辑1:修复了一些代码,是根据我在工作中遇到的问题从记忆/笔记中执行此操作的。我开始使用 valgrind 的原因是我在那个地方遇到了分段错误。我明天会仔细检查我在做什么。

In a function that I am writing I am trying to return a pointer to a vector of unsigned chars. The relevant code is below.

std::vector<unsigned char> *ret = new std::vector<unsigned char>(buffSize,'0');
//Due to suggestions...
int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);
//Forgot to include this in the original
ret->resize(n);
// display chars somehow just for testing
for(std::vector<unsigned char>::iterator it=ret->begin(); it<ret->end();it++)
{
    std::cout<<*it;
}
std::cout<<std::endl;
...
return ret;

When I run this through valgrind I get errors talking about how the buffer in recvfrom is pointing to uninitialized bytes. I've narrowed this down to the vector since I swapped it out for an unsigned char array and everything works fine. Any suggestions?

Edit 1: Fixed some of the code, was doing this from memory/notes with a problem that I was having at work. The reason I started to use valgrind was that I was getting a segmentation fault at that spot. I will double check what I was doing tomorrow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深爱不及久伴 2024-10-16 21:26:04

这行是错误的:

int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);

它应该是:

int n = recvfrom(fd_, &(*ret)[0], buffSize, 0, &recvAddress, &sockSize);

您直接将数据读取到 std::vector 数据结构中,该结构通常是 3 个指针(开始、结束和存储结束)。因此,您将使用通过套接字接收的数据覆盖这 3 个指针,然后覆盖向量后面的随机内存。编译器不会抱怨,因为任何指针类型(在本例中为 std::vector*)都可以隐式转换为 void*std::vector* 的第二个参数代码>recvfrom)。

您当然希望将数据读入向量指向的缓冲区,该缓冲区是通过获取其第一个元素的地址获得的,即 &(*ret)[0]。我们必须添加尴尬的括号,因为括号 [] 的运算符优先级高于一元解引用运算符 *

(另请注意,您缺少 recvfrom()flags 参数 - 我假设这只是编写此问题时的转录错误,并且我已经现在将其设置为 0 。)

This line is wrong:

int n = recvfrom(fd_, ret, buffSize, &recvAddress, &sockSize);

It should be:

int n = recvfrom(fd_, &(*ret)[0], buffSize, 0, &recvAddress, &sockSize);

You're reading data directly onto the std::vector data structure, which is typically 3 pointers (beginning, end, and end-of-storage). So, you're overwriting those 3 pointers with data received over the socket, and then you're overwriting random memory after the vector. The compiler doesn't complain because any pointer type (std::vector<char> * in this case) is implicitly convertible to a void* (the second parameter to recvfrom).

You of course want to read data into the buffer pointed to by the vector, which is obtained by taking the address of its first element, namely &(*ret)[0]. We have to add the awkward parentheses because brackets [] have higher operator precedence than the unary dereferencing operator *.

(Also note that you're missing the flags parameter to recvfrom() -- I'm assuming that was just a transcription error when writing this question, and I've now set that to 0 here.)

_失温 2024-10-16 21:26:04

recvfrom() 仅写入了 n 字节,但您正在读取分配的整个缓冲区。不过,我不确定 valgrind 是否真的那么聪明。

Only bytes up to n were written by recvfrom() but you are reading the entire buffer as allocated. I'm not sure valgrind is really that clever, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文