0

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

Lucaaz
  • 1

2 Answers2

1

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

John3136
  • 28,809
  • 4
  • 51
  • 69
  • `line_i` seems to be reset from 100 to 0, so it's valid until the array overflows 100 elements... – Roddy Jul 07 '21 at 09:04
0

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.

Roddy
  • 66,617
  • 42
  • 165
  • 277