Reputation: 13
When I run this code, I get the error in the title, here's an exact screenshot:
My code is this :
string ProcessCommand(vector<unsigned char> data)
{
try
{
int valread = data.size();
unsigned char* converted = &data[0];
char *buffer = (char *)converted;
buffer[valread] = '\0';
const char file[] = ">myfile.txt";
char * newBuffer = new char[strlen(buffer) + strlen(file) + 1];
strcpy(newBuffer, buffer);
strcat(newBuffer, file);
system(newBuffer);
fstream f("myfile.txt", fstream::in);
string str;
getline(f, str, '\0');
//const char * sendfile = str.c_str();
//char * result;
//result = const_cast<char *>(sendfile);
f.close();
remove("myfile.txt");
return str;
}
catch (const std::exception&)
{
}
}
Upvotes: 0
Views: 5351
Reputation: 234795
buffer[valread] = '\0';
is attempting to access an element of buffer
that doesn't exist. The program behaviour is therefore undefined.
If you had used data.at(valread)
instead then a std::exception
would have been thrown (and intercepted at your catch
site), as required by the C++11 standard.
Also consider using the +
overload of std::string
for all your concatenations. Your code will be so much more readable if you do that. Use a std::string
type for newBuffer
(with automatic storage duration) as well. You can always use c_str()
to access the internal data buffer on a read-only basis.
Upvotes: 4