Lucaaz
Lucaaz

Reputation: 1

Warning C6386 - Buffer overrun while writing to 'LINES_DATA.Lines'

I know this question has been asked before, but I couldn't quite fix my code, even reading other topics. Does anyone know why is it throwing this warning?

Warning C6386 Buffer overrun while writing to 'LINES_DATA.Lines': the writable size is 'LINES_DATA.NumLines4' bytes, but '8' bytes might be written.*
"

    LINES_DATA.NumLines = line_i; //line_i = 100
    LINES_DATA.Lines = new int* [LINES_DATA.NumLines]; 

    line_i = 0;

    for (rapidxml::xml_node<>* pNode = pRoot->first_node(); pNode; pNode = pNode->next_sibling())
    {
        LINES_DATA.Lines[line_i] = new int[COLUMNSIZE]; //COLUMNSIZE = 5

        for (int pos_i = 0; pos_i < COLUMNSIZE; pos_i++)
        {
            LINES_DATA.Lines[line_i][pos_i] = pNode->value()[pos_i] - '0';
        }
        line_i++;
    }

I get the warning in this line:

LINES_DATA.Lines[line_i] = new int[COLUMNSIZE];

Thank you so much

Upvotes: 0

Views: 218

Answers (2)

Roddy
Roddy

Reputation: 68033

It's just a Code Analysis warning. The compiler isn't smart enough to work out your program's entire runtime behaviour.

Your code does have major risks of buffer over-runs, particularly if the XML contains more than 100 elements. You should be using smart pointers and/or STL containers here.

Upvotes: 0

John3136
John3136

Reputation: 29266

If the array (LINES_DATA.Lines) hasline_i elements then LINES_DATA.Lines[line_i] is not valid.

Arrays are zero based so LINES_DATA.Lines has elements 0 to line_i-1

Upvotes: 1

Related Questions