I'm doing some network programming, and I'd like you to review a small piece of code. I want to know if this is safe (regarding bufferoverflows and other security risks).
I'm getting an unsigned char* data and an unsigned int dataLength from a networking API. This is the data from the packet received. The data parameter SHOULD contain (in order):
a message type byte
a null-terminated account number string
a null-terminated password string.
..but ofcourse an attacker could put anything in there.
I'm also having a strange issue with the std::string. When I create it in one way, it works, and in another way it doesn't, see below.
Any other suggestions (like an alternative to the cast maybe?) are welcome too.Code:int f(unsigned char* data, unsigned int dataLength) { std::string acc(reinterpret_cast<char*>(data), 1, 20); std::string pass(reinterpret_cast<char*>(data + acc.length() + 2), 0, 20); //works //std::string pass(reinterpret_cast<char*>(data), acc.length() + 2, 20); //doesn't work std::cout << acc << " " << pass << std::endl; // for testing only. return 0; }
Thanks



LinkBack URL
About LinkBacks



CornedBee