Jump to content
  • Advertisement
Sign in to follow this  
Elsys

System.AccessViolationException occured in Sprintf_s

This topic is 4031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a basic login program to login to my favorite game. The problem is when I run this program An Access.Volation.Exception occurs when sprintf_s attempts to read variables into a formatted /Get request sent to the parameter server without this request I wont be able to get the command line augment to launch the executable from the response packet. Here is my code and yes it is very sloppy naming conventions are fairly confusing error handling is at a minimum or none at all its not preatty but all I expect it to do is send two requests and retrive the responses from them. Here is code void postlogin() { char buffer[200]; array<wchar_t, 1>^username = Form1::textBox1->Text::get()->ToCharArray(); array<wchar_t, 1>^ password = Form1::textBox2->Text::get()->ToCharArray(); sprintf(buffer, "http://auth.ijji.com/auth.nhn?memberid=%s&password=%s", username, password); //Send Inital Login Request I_HTTPRequest * RLogin = NewHTTPRequest(buffer); char lbuf[1024]; while( !RLogin->complete() ) { RLogin->step(); while( RLogin->read( lbuf, sizeof( lbuf ) ) ) ; } char lbuf2[100000]; RLogin->rewind(); while( RLogin->read( lbuf2, sizeof( lbuf2 ) ) ) ; RLogin->dispose(); //Convert buffer result to useable data for the authentication Request string cName ("NHN_P_LOGIN="); string pCookie = lbuf2; size_t cfind; size_t sCok; size_t cont1; size_t cont2; size_t reqlen = 640; size_t reqeln = 140; string sCookie; string sCookie2; string sCookie3; cfind=pCookie.rfind(cName); if (cfind!=string::npos) { sCookie = pCookie.substr(cfind, reqlen); sCok = sCookie.size(); cont1 = cfind + 256; sCookie2 = pCookie.substr(cont1, reqlen); cont2 = cfind + 512; sCookie3 = pCookie.substr(cont2, reqeln); sCookie.erase(0, 12); } const char *aCookie = sCookie.c_str(); const char *aCookie2 = sCookie2.c_str(); const char *aCookie3 = sCookie3.c_str(); char cCookie [1024]; strcpy (cCookie, aCookie); strcat (cCookie, aCookie2); strcat (cCookie, aCookie3); // Cleanup Uneccesary Memory Buffer Values from first http Request are no longer necessary since all operations are done on them; //Parameter List for Paramserver char *gtype = "PUBGM"; char *k22e = "U_GUNZ"; unsigned int k37e = 9999; unsigned int k140e = 0000; char *fc = "hga"; unsigned int rdm = 0; char *lang = "ENGLISH"; char sx = 'M'; char *ag = ""; unsigned int k31e = 0; unsigned int ver = 21; unsigned long m1e = 0; int kpw = 1; char *pub1e = cCookie; unsigned int pub10e = 514; unsigned int cksm = 000; char enc = 'y'; //Initiate Second Request char pbuffer[100000]; int BUFFERLEN = 100000; sprintf_s(pbuffer, BUFFERLEN, "GET /?gtype=%s&k22e=%s&k3e=%s&k37e=%04d&k140e=%04d&fc=% s&rdm=%d&lang=%s&sx=%c&ag=&k31e=%d&ver=%d&k8e=%s&m 1e=%d&kpw=%d&enc=%c&pub1e=%s&pub10e=%d&cksm=%03d HTTP/1.1\r\n\r\n", gtype, k22e, username, k37e, k140e, fc, rdm, lang, sx, ag, k31e, ver, username, m1e, kpw, enc, pub1e, pub10e, cksm); char recvbuf[1000]; int recvbuflen = 1000; int iResult; WSADATA wsaData; SOCKET ConnectSocket; struct sockaddr_in saServer; // Initialize Winsock iResult = WSAStartup(MAKEWORD(2,2), &wsaData); if (iResult != 0) { } // Create a listening socket ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (ConnectSocket == INVALID_SOCKET) { WSACleanup(); } // Set up the sockaddr structure saServer.sin_family = AF_INET; saServer.sin_addr.s_addr = inet_addr(PARAMSERVER); saServer.sin_port = htons(PARAMPORT); // Connect to server. iResult = connect( ConnectSocket, (SOCKADDR*) &saServer, sizeof(saServer) ); if ( iResult == SOCKET_ERROR) { closesocket (ConnectSocket); WSACleanup(); } iResult = send( ConnectSocket, pbuffer, (int)strlen(pbuffer), 0 ); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); WSACleanup(); } // shutdown the connection since no more data will be sent iResult = shutdown(ConnectSocket, SD_SEND); if (iResult == SOCKET_ERROR) { closesocket(ConnectSocket); WSACleanup(); } do { iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0); } while( iResult > 0 ); // cleanup closesocket(ConnectSocket); WSACleanup(); } [code/] Notice that the buffer sizes to hold my variables are extremely large and that no delete is called throuout the program except for in the dispose method of an http get class I use for the first request. If I call delete to free the buffers from the first request since they are no longer needed once I have cCookie I get a debug exception in csomthindel.cpp cant remember the exact name of the file. Is the reason for this access.violation a result of their not being enough memory allocated to my process for the sprintf_s function to succeed and how might I be able to fix this? When the buffer sizes were smaller the function worked but the request was trunceted before the final 3 vars could be sent along with the headers and request information this request packet must be passed exactly as it is formated with all the data otherwise the response will not contain the comandline augment I am trying to retrive. So I increased the size of the buffer to the length of the full response from the first request figguring that would be enough space to hold the entire second request. The array's I defined are extremely large as well the cookie is only 640 characters long not including null terminating character but when I checked the value of the resulting retrival of the cookie it was trunceted so thats why I had to increase the size to that exact value for the function to succeed.

Share this post


Link to post
Share on other sites
Advertisement
Quote:

The array's I defined are extremely large as well the cookie is only 640 characters long not including null terminating character but when I checked the value of the resulting retrival of the cookie it was trunceted so thats why I had to increase the size to that exact value for the function to succeed.

All of this is representative of a very poor methodology for string data handling that was necessary for C. You are obviously not using C, so the solution is to use proper idioms for string formatting. The access violation is almost certainly related to this manual buffer management, size-hacking, and/or mistyping a format specifier in some fashion.

You can replace your use of sprintf_s with std::stringstream:

std::stringstream str;

str << "GET /?gtype=" << gtype << ...;

//...

iResult = send( ConnectSocket, str.str().c_str(), str.str().size(), 0 );


This is both typesafe and far less prone to error.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!