系统调用参数socketcall.recvfrom(buf)指向不可寻址字节
Syscall param socketcall.recvfrom(buf) points to unaddressable byte(s)
当运行此方法时,我在调用socket.getBytes
时从Valgrind获得错误
void Client::register(VMap::VType type, char *id)
{
const int sizeOfType = sizeof(type);
char *buffer = (char *) malloc(sizeOfType);
memcpy(buffer, &type, sizeOfType);
socket.send(buffer, sizeOfType);
sleep(1);
char *bufferRecv = (char *) malloc(sizeOfType);
memset(bufferRecv, 0 ,sizeOfType);
int size = socket.getBytes(bufferRecv);
free(buffer);
free(bufferRecv);
}
int Socket::getBytes(char *buf)
{
int ret = ::recv(m_sock, buf, MAXRECV, 0);
return ret;
}
错误是:
==30653== Syscall param socketcall.recvfrom(buf) points to unaddressable byte(s)
==30653== at 0x58C214C: recv (recv.c:34)
==30653== by 0x4E47B84: pippo::Socket::getBytes(char*) (Socket.cpp:115)
==30653== by 0x4E48BAE: pippo::Rossi::register(pippo::sMap::Type, char*) (Rossi.cpp:80)
==30653== by 0x4E48EEE: pippo::Rossi::initsMap(std::string) (Rossi.cpp:111)
==30653== by 0x4E486B0: pippo::Rossi::Rossi(std::string, std::string, std::string, unsigned int) (Rossi.cpp:54)
==30653== by 0x4E45884: test::TestRossi::testConstructor() (TestRossi.cpp:70)
==30653== by 0x4E456D8: test::TestRossi::testBody() (TestRossi.cpp:48)
==30653== by 0x5288D03: test::TestUnit::test() (TestUnit.cpp:42)
==30653== by 0x5288513: test::TestRunner::run() (TestRunner.cpp:44)
==30653== by 0x407F26: main (TestDUMBO.cpp:125)
==30653== Address 0x67a9d64 is 0 bytes after a block of size 4 alloc'd
==30653== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==30653== by 0x4E48B21: pippo::Rossi::register(pippo::sMap::Type, char*) (Rossi.cpp:76)
==30653== by 0x4E48EEE: pippo::Rossi::initsMap(std::string) (Rossi.cpp:111)
==30653== by 0x4E486B0: pippo::Rossi::Rossi(std::string, std::string, std::string, unsigned int) (Rossi.cpp:54)
==30653== by 0x4E45884: test::TestRossi::testConstructor() (TestRossi.cpp:70)
==30653== by 0x4E456D8: test::TestRossi::testBody() (TestRossi.cpp:48)
==30653== by 0x5288D03: test::TestUnit::test() (TestUnit.cpp:42)
==30653== by 0x5288513: test::TestRunner::run() (TestRunner.cpp:44)
==30653== by 0x407F26: main (TestDUMBO.cpp:125)
我真不明白是怎么回事。例如,如果我简单地在bufferRecv
中写入sprintf
,我不会得到任何错误。
::recv的原型如下:
/* Read N bytes into BUF from socket FD.
Returns the number read or -1 for errors.
This function is a cancellation point and therefore not marked with
__THROW. */
extern ssize_t recv (int __fd, void *__buf, size_t __n, int __flags);
EDIT另一个信息:如果我像这样声明缓冲区char bufferRecv[4];
,我不会从Valgrind得到任何错误。
对于其他有同样问题的人:根据@alk的建议,我简单地更改了getBytes方法,如下所示:
int Socket::getBytes(char *buf, int sizeOfBuf)
{
int ret = ::recv(m_sock, buf, sizeOfBuf, 0);
return ret;
}
相关文章:
- 从结构寻址时,MMAP变量的行为很奇怪
- 字节真的是最小可寻址单元吗
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- imread() 仍然返回空垫子,尽管在 openCV 4.0.0 中寻址正确
- 在具有开放寻址的哈希表中插入节点 [优化逻辑]
- 如何像在 C++ 中处理数组一样对 .txt 文件中的字符进行寻址?
- C++11右值引用寻址
- 对堆栈增长和寻址的困惑
- C++ std::find() 寻址返回向量的类函数时的意外行为
- 间接寻址运算符如何返回带有运算符重载的指针地址
- Adafruit NeoPixel库在寻址超过7个LED灯条时不起作用
- 在模板 SFINAE 约束中使用间接寻址级别会导致硬错误
- 错误:使用索引寻址和 Clang 的指令的操作数无效
- 是否可以寻址另一个网络中的服务器/客户端套接字?(C++)
- C++中的字节寻址算法
- 指针声明和间接寻址之间的区别
- 仅在使用间接寻址时调用虚函数 — 经典的早期绑定问题?
- C++ std::map 不同的键寻址同一索引
- Valgrind 错误:系统调用参数 epoll_pwait(sigmask) 指向不可寻址的字节
- 系统调用参数socketcall.recvfrom(buf)指向不可寻址字节