1
\$\begingroup\$

Reaching out yet again for a point in the right direction. Specifically, I am casting a simple struct to a void pointer so it can be sent out on the network, but it is cutting off my second variable for reasons unknown to me. I am likely looking right over it, but a significant lack of caffeine is hiding the answer from me. I have found a few similar threads, notably this one, which has been a great help. I am reasonable certain this is a "newbie mistake" or I am overlooking something exceedingly simply. Like most techs, I've become so lost looking for the "big" answer, that I'm likely overlooking the "small" answer (i.e. 'did you plug it in first?').

Any input is appreciated, so thank you in advance!

/* struct provided */
struct Login_Struct
{
/*000*/ char username[32];
/*032*/ char password[64];
/*096*/
};


void NetWrapper::sendLogin(std::string user, std::string pass)
{
    // ensure size constraints are met
    if( user.length() > sizeof(Login_Struct::username)-1
    ||  pass.length() > sizeof(Login_Struct::password)-1) { return; }

    std::string hashPass = Security::strSHA256(pass); // returns hashed password

    Login_Struct login; // empty struct
    strncpy(login.username, user.c_str(), user.length()); // i.e 'bob'
    strncpy(login.password, hashPass.c_str(), hashPass.length()); // hashed password (64 char)

    char* pData = new char[sizeof(Login_Struct)];
    memcpy(static_cast<void*>(pData), static_cast<void*>(&login), sizeof(Login_Struct)); // serial data for sending
    m_network->send(pData, OP_Login); // Network::send(char* d, short opCode)
    delete pData; pData = nullptr; // cleanup
}

Additionally, debug output looks like this (omitted from code for readability, can supply if needed/requested):

NetWrapper::sendLogin: Login_Struct Dump [bob] (bob), [5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8] (password)
DEBUG: username maxlength [32], user length [3]
DEBUG: password maxlength [64], pass length [64]
DEBUG: pData [x=�i=], [0x7ffc5e4fd0e0], sizeof(Login_Struct) [96], sizeof(login) [96]
NetWrapper::sendSerialized: New pData! pData [x=�i=]
NetWrapper::sendSerialized: Post memcpy! pData [bob]
Network::send : Sending [d0c8504000100bob] ## ignore characters preceding "bob"
\$\endgroup\$
2
  • 1
    \$\begingroup\$ The memory in Login_Struct is layed out like "bob\0<28 garbage chars>pwd\0<rest garbage chars>" if you cast it to char* in send() the string will end at the first \0 character... which happens to be after bob so rest of the data is not lost - they are just ignored inside of your send() method, at least it is how it looks like to me. Anyhow, this question appears to be about programming not specific to game development which is considered off-topic. \$\endgroup\$
    – wondra
    Commented May 23, 2017 at 22:49
  • \$\begingroup\$ @wondra, I appreciate the insight! I will re-evaluate that portion. Additionally, I see your point with off-topic. Though it is for a game, this specific question doesn't deal with game developer versus a standard C++ programmer. moderators: apologies for this! I will post in the correct section. \$\endgroup\$
    – Beau B.
    Commented May 24, 2017 at 18:54

0

Browse other questions tagged .