使用 sprintf 分配给字符数组并将字符指针作为输入之一时缓冲区溢出
Buffer overflows when using sprintf assigning to char array with char pointer as one of the inputs
我使用 sprintf 创建一个 char 数组,稍后可以将其写出作为对系统的调用。
char buffer[80];
char *ip = inet_ntoa(sa.sin_addr);
short port = 1;
sprintf(buffer, "Command with IP %s and port %d",ip, port);
system(buffer);
现在理论上,这个缓冲区应该有足够的空间分配给这个字符串。但不知何故,由于字符指针,我仍然* stack smashing detected *
为错误。
sprintf 不能将字符指针作为输入处理,也许是因为它本身的分配很大?
编辑:
事实证明,缓冲区毕竟很小,至少对于某些参数而言。
由于您标记了C++而不是 C,因此您的代码最好编写为:
std::string ip = "0.0.0.0";
int port = 1;
std::ostringstream oss;
oss << "Command with IP: " << ip << " and port " << port;
system(oss.str().c_str());
相关文章:
- C++字符*缓冲区的大小
- 将字符缓冲区强制转换为函数指针
- 来自无符号字符缓冲区的虚拟输出
- 如何在C++中将无符号字符缓冲区转换为双精度?
- 如何使 printf 以与 NSLog 相同的方式打印字符缓冲区?
- 将字符缓冲区转换为字符串
- 读取文件时字符缓冲区溢出
- 来自字符缓冲区的恐惧
- 为什么在我的测试程序中,从字符缓冲区读取一个int时,"std::copy"5x(!)比"
- C/C++ 从字符缓冲区读取以填充结构
- 具有输入无符号字符缓冲区C++的设备 IoControl
- 如何从字符 * 缓冲区中的位置 y 读取 x 个字符
- 从字符 * 缓冲区到 wchar_t * 缓冲区的内存
- 如何将字符 * 缓冲区转换为无符号的短 int * bufferSh
- 将windows BITMAP转换为PIX(无符号字符缓冲区)
- RAII字符缓冲区
- MFC-传递带有PostMessage From DLL的字符缓冲区
- 如何使用Gstreamer播放原始字符*缓冲区
- 在 Win32 中解析巨大的字符* 缓冲区
- 分配字符缓冲区以保存浮点的文本表示形式