我正在使用C ++中的远程过程调用(gRPC)编写文件上传服务。客户端将文件拆分为多个块,然后通过gRPC流发送这些块。服务器读取gRPC流并写入文件。当我读取gRPC流时,基于堆的溢出。我使用strcpy解决了此问题,但使用strcpy污染了我的文件。如何解决基于堆的溢出
原型对象
message FileContent {
bytes content = 1;
int32 counter = 2;
}
客户作家
char buffer[4096];
while(file_offset < filelength)
{
memset(buffer,0,4096);
bytes_read = pread(filedes, buffer, 4096,file_offset);
filecontent.set_content(std::string(buffer, bytes_read));
filecontent.set_counter(bytes_read);
writer->Write(filecontent);
file_offset+=bytes_read;
}
服务器阅读器
char buffer[4096];
while (reader->Read(&filecontent))
{
bytes_read=filecontent.counter();
cout << "Bytes Read" << bytes_read << endl;
strcpy(buffer,filecontent.mutable_content()->c_str()); // if I don't do this, I get heap based overflow when the next line executes. But this corrupts my file. How do I fix it?
write(filedes, buffer,(bytes_read));
file_offset+=bytes_read;
}
我正在从连续流中读取文件数据。该文件被拆分为多个块,并通过gRPC流发送。每个块都需要在服务器端使用strcpy操作条件,以防止基于堆的溢出,但是如果执行此操作,则会破坏文件字节。如何在不添加空终止符的情况下从流中读取并写入文件。请帮助!!
您的grpc filecontent成员应具有.copy()函数。您可以使用此功能复制文件内容,而不是使用strcpy。
filecontent.content()。copy(buffer,bytes_read)