Issue
Consider the code below for a second:
char buffer[4000];
size_t bytes = recv(fd, buffer, 4000, 0);
write(pipefd[1], buffer, bytes);
close(pipefd[1]);
wait(NULL);
Here I read fd, write data to the pipe and close the pipe. This allows to eventually upload tiny images that can be rendered. However, if I do everything the same, only replace char array with std::istringstream, as in the example below:
char buffer[4000];
size_t bytes = recv(fd, buffer, 4000, 0);
std::istringstream data(buffer);
write(pipefd[1], data.str().c_str(), bytes);
close(pipefd[1]);
wait(NULL);
Then image is uploaded but cannot be rendered. (Via pipes this C++ program communicates with a php-cgi script).
Also:
if (strcmp(data.str().c_str(), buffer) == 0)
// returns true
I am very curious as to why this could be the case.
Solution
std::istringstream data(buffer);
This assumes buffer
is a null-terminated string. Binary data typically contains 0x00
bytes, thus you would end up truncating the buffer
contents.
Use this instead:
std::istringstream data(std::string(buffer, bytes), std::ios::binary);
Answered By - Remy Lebeau Answer Checked By - Candace Johnson (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.