correct copying of assignment pool across targets

This commit is contained in:
Stephen Birarda 2013-09-04 11:50:39 -07:00
parent ebc7de3a90
commit 36c2f42b5c
6 changed files with 13 additions and 13 deletions

View file

@ -39,7 +39,7 @@ int main(int argc, char* const argv[]) {
// copy the passed assignment pool // copy the passed assignment pool
int poolLength = strlen(optarg); int poolLength = strlen(optarg);
assignmentPool = new char[poolLength + sizeof(char)]; assignmentPool = new char[poolLength + sizeof(char)];
memcpy(assignmentPool, optarg, poolLength + sizeof(char)); strcpy(assignmentPool, optarg);
} }
} }

View file

@ -50,7 +50,7 @@ int main(int argc, const char* argv[]) {
} }
} else if (senderData[0] == PACKET_TYPE_CREATE_ASSIGNMENT && packetVersionMatch(senderData)) { } else if (senderData[0] == PACKET_TYPE_CREATE_ASSIGNMENT && packetVersionMatch(senderData)) {
// memcpy the sent assignment // memcpy the sent assignment
Assignment createdAssignment(senderData + numBytesForPacketHeader(PACKET_TYPE_CREATE_ASSIGNMENT)); Assignment createdAssignment(senderData + numBytesForPacketHeader(senderData));
qDebug() << "Received an assignment:" << createdAssignment; qDebug() << "Received an assignment:" << createdAssignment;

View file

@ -87,7 +87,7 @@ int main(int argc, char* const argv[])
// loop the parameters to see if we were passed a pool for assignment // loop the parameters to see if we were passed a pool for assignment
int parameter = -1; int parameter = -1;
const char ALLOWED_PARAMETERS[] = "p::"; const char ALLOWED_PARAMETERS[] = "p::-local::";
const char POOL_PARAMETER_CHAR = 'p'; const char POOL_PARAMETER_CHAR = 'p';
char* assignmentPool = NULL; char* assignmentPool = NULL;
@ -97,7 +97,7 @@ int main(int argc, char* const argv[])
// copy the passed assignment pool // copy the passed assignment pool
int poolLength = strlen(optarg); int poolLength = strlen(optarg);
assignmentPool = new char[poolLength + sizeof(char)]; assignmentPool = new char[poolLength + sizeof(char)];
memcpy(assignmentPool, optarg, poolLength + sizeof(char)); strcpy(assignmentPool, optarg);
} }
} }

View file

@ -18,7 +18,7 @@ Assignment::Assignment(Assignment::Direction direction, Assignment::Type type, c
// create the char array and make it large enough for string and null termination // create the char array and make it large enough for string and null termination
_pool = new char[poolLength + sizeof(char)]; _pool = new char[poolLength + sizeof(char)];
memcpy(_pool, pool, poolLength + 1); strcpy(_pool, pool);
} }
} }
@ -33,25 +33,25 @@ Assignment::Assignment(const unsigned char* dataBuffer) {
int poolLength = strlen((const char*) dataBuffer + numBytesRead); int poolLength = strlen((const char*) dataBuffer + numBytesRead);
_pool = new char[poolLength + sizeof(char)]; _pool = new char[poolLength + sizeof(char)];
memcpy(_pool, dataBuffer + numBytesRead, poolLength + sizeof(char)); strcpy(_pool, (char*) dataBuffer + numBytesRead);
} }
int Assignment::packAssignmentToBuffer(unsigned char* buffer) { int Assignment::packToBuffer(unsigned char* buffer) {
int numPackedBytes = 0; int numPackedBytes = 0;
memcpy(buffer, &_direction, sizeof(_direction)); memcpy(buffer, &_direction, sizeof(_direction));
numPackedBytes += sizeof(_direction); numPackedBytes += sizeof(_direction);
memcpy(buffer, &_type, sizeof(_type)); memcpy(buffer + numPackedBytes, &_type, sizeof(_type));
numPackedBytes += sizeof(_type); numPackedBytes += sizeof(_type);
strcpy((char *)buffer, _pool); strcpy((char*) buffer + numPackedBytes, _pool);
numPackedBytes += strlen(_pool) + sizeof(char); numPackedBytes += strlen(_pool) + sizeof(char);
return numPackedBytes; return numPackedBytes;
} }
QDebug operator<<(QDebug debug, const Assignment &assignment) { QDebug operator<<(QDebug debug, const Assignment &assignment) {
debug << "T:" << assignment.getType() << "P" << assignment.getPool(); debug << "T:" << assignment.getType() << "P:" << assignment.getPool();
return debug.nospace(); return debug.nospace();
} }

View file

@ -31,7 +31,7 @@ public:
Assignment::Type getType() const { return _type; } Assignment::Type getType() const { return _type; }
const char* getPool() const { return _pool; } const char* getPool() const { return _pool; }
int packAssignmentToBuffer(unsigned char* buffer); int packToBuffer(unsigned char* buffer);
private: private:
Assignment::Direction _direction; Assignment::Direction _direction;

View file

@ -379,9 +379,9 @@ void NodeList::sendAssignment(Assignment& assignment) {
: PACKET_TYPE_REQUEST_ASSIGNMENT; : PACKET_TYPE_REQUEST_ASSIGNMENT;
int numHeaderBytes = populateTypeAndVersion(assignmentPacket, assignmentPacketType); int numHeaderBytes = populateTypeAndVersion(assignmentPacket, assignmentPacketType);
*(assignmentPacket + numHeaderBytes) = assignment.getType(); int numAssignmentBytes = assignment.packToBuffer(assignmentPacket + numHeaderBytes);
_nodeSocket.send((sockaddr*) &assignmentServerSocket, assignmentPacket, numHeaderBytes + sizeof(unsigned char)); _nodeSocket.send((sockaddr*) &assignmentServerSocket, assignmentPacket, numHeaderBytes + numAssignmentBytes);
} }
Node* NodeList::addOrUpdateNode(sockaddr* publicSocket, sockaddr* localSocket, char nodeType, uint16_t nodeId) { Node* NodeList::addOrUpdateNode(sockaddr* publicSocket, sockaddr* localSocket, char nodeType, uint16_t nodeId) {