1

I'm making a parser to read .X files to a renderer and I encountered a problem while parsing the file.

The error apparently shows that a Heap Corruption has been detected in the parsing function. That problem only happens on a certain line from the file.

The following code is my reading function and I traced the error to getline(meshData, lineBuffer);.

void CAppX::loadFile(){
string lineBuffer;
int lineNumber = 0;
bool fileSelected = false;

std::wstring fileDirectory = L"";
fileDirectory.resize(MAX_PATH);

OPENFILENAME file;
ZeroMemory(&file, sizeof(file));
file.lStructSize = sizeof(file);
file.hwndOwner = NULL;
file.lpstrFilter = L" X Files\0*.x\0";
file.lpstrFile = &fileDirectory[0];
file.nMaxFile = MAX_PATH;
file.lpstrTitle = L"Choose a model to load";
file.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;

while (fileSelected == false)
{
    if (0 != GetOpenFileName(&file))
    {
        fstream meshData;
        meshData.open(fileDirectory);
        wcout << "Summarizing " << fileDirectory << endl;

        while (!meshData.eof())
        {
            getline(meshData, lineBuffer);
            lineNumber++;

            if (lineNumber == 55)
            { 
                cout << endl;
            }

            cout << "Line " << lineNumber << " (" << lineBuffer.size() << " chars)" << ": " << lineBuffer << endl;

            if (lineBuffer.size() > 0)
            {
                parseBuffer(lineBuffer);
            }
        }

        meshData.close();
        fileSelected = true;
    }
    else
    {
        MessageBox(NULL, L"A compatible model must be selected", L"No model chosen", MB_ICONWARNING);
    }
}}

Next, here's the parser. And mainly what I wanted to do is to get all the tokens in the while and not from the second token and on.

void CAppX::parseBuffer(string & line){
int currentData = NULL; //Helps to open gateways and classify data
string originalLine; //Stores the original line

char separators[] = " .,;{}[]-<>"; //Symbols that mustn't be read from file
char* lastToken = NULL;
char* token = "null"; //Stores each parsed value or data from the line
char* unparsedData = NULL; //Stores the rest of the line as data becomes tokenized

//Storing the original line. Else, the line will be interrupted by null characters, preventing full copy
originalLine = line;

// This two lines prevents the first token from being parsed out of the <while> cycle
strtok_s((char*)line.c_str(), separators, &unparsedData); //Establishing "unparsedData" as context
strcpy(unparsedData, (char*)originalLine.c_str()); //Copying the original line as "unparsedData"

while (token != NULL)
{   
    token = strtok_s(NULL, separators, &unparsedData);
    lastToken = token;
}}

The trouble begins with this lines in the console

Line 26 (26 chars):  DWORD nFaceVertexIndices;

When the program finishes to parse this line, the next one reports the Heap Corruption Error in the getline() while attempting to read

Line 27 (51 chars):  array DWORD faceVertexIndices[nFaceVertexIndices];

as said before.

When I ignore this warning, the rest of the file parses finely and no other problems appear from there on.

Sincerely I don't know what the source of the problem may be aside from the line being too big for it to be transfered to tokens. I noticed that before I modified my parser for it to tokenize from the beginning within the while, it worked just fine and no Heap Corruptions occured.

Here's a link to the .X file for you to test if you want.

0 Answers0