Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible buffer overflow issue
    primarykey
    data
    text
    <p>I have the following code written in C++ to extract a given range of text in a Piece Table data structure. Here is the function of class PieceTable that stores the given range of text in the character array <code>buffer</code> : </p> <pre><code>void PieceTable::getTextInRange(unsigned __int64 startPos, unsigned __int64 endPos, char buffer[]){ char* totalBuffer = new char[getSize() + 2]; getBuffer(totalBuffer); if(endPos &gt;= getSize()) endPos = getSize() - 1; cout&lt;&lt;"startPos : "&lt;&lt;startPos&lt;&lt;endl; cout&lt;&lt;"endPos : "&lt;&lt;endPos&lt;&lt;endl; memcpy(buffer, &amp;totalBuffer[startPos], endPos - startPos + 1); buffer[endPos - startPos + 2] = '\0'; if(totalBuffer != 0) delete[] totalBuffer; totalBuffer = 0; } </code></pre> <p>Here is the piece of code in the main method which i use to test this code : </p> <pre><code>temp2 = new char[end - start + 2]; //changing 2 to 3 solves the problem pieceTable.getTextInRange(Start, end, temp2); for(int i = 0; i&lt; end - start + 1; i++) cout&lt;&lt;temp2[i]; cout&lt;&lt;endl; if( temp2 != 0) { delete[] temp2; //this line causes the heap corruption error temp2 = 0; } </code></pre> <p>Declaration of <code>temp2</code> : <code>char* temp2;</code></p> <p>Whenever the program encounters the <code>delete[] temp2</code> statement, there is a heap corruption error. The problem does not occur if I allocate memory for temp2 as:<br> <code>temp2 = new char[end - start + 3]</code> So, basically changing the length solves the problem. I know that I am messing up with the lengths somewhere, but I can't figure out where.</p> <p>EDIT : getSize() :</p> <pre><code>__int64 PieceTable::getSize() { return dList.getLength(dList.getBack()); } </code></pre> <p>I am using a piece table data structure. Here it is, inside this paper:http://www.cs.unm.edu/~crowley/papers/sds.pdf</p> <p>I may be wrong, but I don't think that there is any problem with <code>getSize()</code>, since the function I use to retrieve the length of the entire buffer <code>getBuffer</code>, works as shown in the code. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload