OSDN Git Service

Last Line of Files vs "Ghost Lines" (3)
authorGreyMerlin <GreyMerlin@gmail.com>
Thu, 2 Aug 2018 22:10:34 +0000 (15:10 -0700)
committerGreyMerlin <GreyMerlin@gmail.com>
Thu, 2 Aug 2018 22:10:34 +0000 (15:10 -0700)
`LineInfo::Delete()` was not correctly handling the case of deleting
all text in a line _including_ the CRLF at the end.  This particular
situation exists because of deleting text at the very end of a file.

Externals/crystaledit/editlib/LineInfo.cpp

index 620c947..b3ea2fe 100644 (file)
@@ -219,7 +219,19 @@ void LineInfo::Delete(size_t nStartChar, size_t nEndChar)
       memcpy (m_pcLine + nStartChar, m_pcLine + nEndChar,
               sizeof (TCHAR) * (FullLength() - nEndChar));
     }
-  m_nLength -= (nEndChar - nStartChar);
+  size_t nDelete = (nEndChar - nStartChar);
+  if (nDelete <= m_nLength)
+  {
+         m_nLength -= nDelete;
+  }
+  else
+  {
+         ASSERT( (m_nLength + m_nEolChars) <= nDelete );
+         nDelete -= m_nLength;
+         m_nLength = 0;
+         m_nEolChars -= static_cast<int>(nDelete);
+  }
+  ASSERT (m_nLength <= INT_MAX);               // assert "positive int"
   if (m_pcLine)
     m_pcLine[FullLength()] = '\0';
 }