OSDN Git Service

Cleanup: Last minute tweaks...
authorGreyMerlin <GreyMerlin7@gmail.com>
Thu, 5 Oct 2017 20:57:07 +0000 (13:57 -0700)
committerGreyMerlin <GreyMerlin7@gmail.com>
Thu, 5 Oct 2017 21:10:23 +0000 (14:10 -0700)
 * change a few more functions from long to size_t
 * add a few ASSERT statements where appropriate.  These make sure that
casting a size_t to an int or long will not truncate large values.
 * refactor a few places where the size_t changes could be better
 * fix one usage of _tsplitpath_s() that was wrong.

Externals/crystaledit/editlib/LineInfo.cpp
Externals/crystaledit/editlib/ccrystaleditview.cpp
Externals/crystaledit/editlib/ccrystaltextview.cpp
Externals/crystaledit/editlib/cs2cs.cpp
Externals/poco/Foundation/src/SharedMemory_WIN32.cpp
Src/FileFiltersDlg.cpp
Src/diffutils/src/ifdef.c
Src/diffutils/src/io.c
Src/markdown.cpp

index 057d975..21a2522 100644 (file)
@@ -81,7 +81,7 @@ void LineInfo::Create(LPCTSTR pszLine, size_t nLength)
     delete[] m_pcLine;
   m_pcLine = new TCHAR[m_nMax];
   ZeroMemory(m_pcLine, m_nMax * sizeof(TCHAR));
-  size_t dwLen = sizeof (TCHAR) * m_nLength;
+  const size_t dwLen = sizeof (TCHAR) * m_nLength;
   CopyMemory (m_pcLine, pszLine, dwLen);
   m_pcLine[m_nLength] = '\0';
 
index a40b7d7..9b490ea 100644 (file)
@@ -2798,7 +2798,7 @@ OnUpdateToolsCharCoding (CCmdUI * pCmdUI)
   pCmdUI->Enable (IsSelection ());
 }
 
-long str_pos (LPCTSTR whole, LPCTSTR piece);
+size_t str_pos (LPCTSTR whole, LPCTSTR piece);
 
 void CCrystalEditView::
 OnToolsCharCoding ()
index 124dcfd..3c2b4ec 100644 (file)
@@ -1702,10 +1702,12 @@ CCrystalTextView::GetMarkerTextBlocks(int nLineIndex) const
               size_t nPos = ::FindStringHelper(p, marker.second.sFindWhat, marker.second.dwFlags | FIND_NO_WRAP, nMatchLen, node, &matches);
               if (nPos == -1)
                   break;
+                         ASSERT(((p - pszChars) + nPos) < INT_MAX);
               blocks[nBlocks].m_nCharPos = static_cast<int>((p - pszChars) + nPos);
               blocks[nBlocks].m_nBgColorIndex = marker.second.nBgColorIndex | COLORINDEX_APPLYFORCE;
               blocks[nBlocks].m_nColorIndex = COLORINDEX_NONE;
               ++nBlocks;
+                         ASSERT(((p - pszChars) + nPos + nMatchLen) < INT_MAX);
               blocks[nBlocks].m_nCharPos = static_cast<int>((p - pszChars) + nPos + nMatchLen);
               blocks[nBlocks].m_nBgColorIndex = COLORINDEX_NONE;
               blocks[nBlocks].m_nColorIndex = COLORINDEX_NONE;
@@ -4816,6 +4818,7 @@ FindStringHelper (LPCTSTR pszFindWhere, LPCTSTR pszFindWhat, DWORD dwFlags, int
       if (rxnode && RxExec (rxnode, pszFindWhere, _tcslen (pszFindWhere), pszFindWhere, rxmatch))
         {
           pos = rxmatch->Open[0];
+                 ASSERT((rxmatch->Close[0] - rxmatch->Open[0]) < INT_MAX);
           nLen = static_cast<int>(rxmatch->Close[0] - rxmatch->Open[0]);
         }
       else
index 1c4979d..ff41095 100644 (file)
@@ -61,7 +61,7 @@ str_fill (LPTSTR s, TCHAR ch, long count)
   *s = _T ('\0');
 }
 
-long
+size_t
 str_pos (LPCTSTR whole, LPCTSTR piece)
 {
   LPCTSTR s = whole;
@@ -69,7 +69,7 @@ str_pos (LPCTSTR whole, LPCTSTR piece)
 
   while (*s)
     if (!_tcsnicmp (s++, piece, l))
-      return static_cast<long>(s - whole - 1);
+      return (s - whole - 1);
   return -2;
 }
 
@@ -110,9 +110,9 @@ skip_word (LPCTSTR s)
 size_t
 get_coding (LPCTSTR name, type_codes *codes, int *coding)
 {
-  long i, pos;
+  size_t pos;
 
-  for (i = 0; i < codes_count; i++)
+  for (int i = 0; i < codes_count; i++)
     if ((pos = str_pos (name, codes[i].name)) >= 0)
       {
         *coding = i;
@@ -179,7 +179,7 @@ TCHAR iconvert_char (TCHAR ch, int source_coding, int destination_coding, bool a
 int
 iconvert (LPTSTR string, int source_coding, int destination_coding, bool alphabet_only)
   {
-    size_t posit = -2, i, j;
+    size_t posit = -2;
     LPCTSTR source_chars, destination_chars, cod_pos = NULL;
     TCHAR ch;
     LPTSTR s = string;
@@ -195,12 +195,13 @@ iconvert (LPTSTR string, int source_coding, int destination_coding, bool alphabe
     if (destination_coding < 0)
       return -2;
   
-    const size_t chars_count = alphabet_only ? chars_alphabet_count : chars_all_count;
+    int chars_count = alphabet_only ? chars_alphabet_count : chars_all_count;
     source_chars = source_codes[source_coding].codes;
     destination_chars = destination_codes[destination_coding].codes;
     for (;;)
       if (cod_pos == s)
         {
+                 size_t i, j;
           i = _tcslen (source_codes[source_coding].name);
           j = _tcslen (destination_codes[destination_coding].name);
           if (i != j)
@@ -213,7 +214,7 @@ iconvert (LPTSTR string, int source_coding, int destination_coding, bool alphabe
           ch = *s;
           if (!ch)
             break;
-          i = chars_count;
+          int i = chars_count;
           if ((unsigned) ch > 127)
             for (i = 0; i < chars_count; i++)
               if (ch == source_chars[i])
index 420948b..ca8a05e 100644 (file)
@@ -55,7 +55,7 @@ SharedMemoryImpl::SharedMemoryImpl(const std::string& name, std::size_t size, Sh
        _address(0)
 {
        LARGE_INTEGER mySize;
-       mySize.QuadPart = size;
+       mySize.QuadPart = _size;
        if (mode == SharedMemory::AM_WRITE)
                _mode = PAGE_READWRITE;
 
index 0713dd4..9302062 100644 (file)
@@ -434,7 +434,7 @@ void FileFiltersDlg::OnBnClickedFilterfileNewbutton()
                TCHAR ext[_MAX_EXT] = {0};
                TCHAR dir[_MAX_DIR] = {0};
                TCHAR drive[_MAX_DRIVE] = {0};
-               _tsplitpath_s(s.c_str(), drive, sizeof(drive), dir, sizeof(dir), file, sizeof(file), ext, sizeof(ext));
+               _tsplitpath_s(s.c_str(), drive, _MAX_DRIVE, dir, _MAX_DIR, file, _MAX_FNAME, ext, _MAX_EXT);
                if (_tcslen(ext) == 0)
                {
                        s += FileFilterExt;
index 09f7e43..edaa512 100644 (file)
@@ -20,6 +20,7 @@ and this notice must be preserved on all copies.  */
 
 
 #include "diff.h"
+#include <cassert>
 
 struct group
 {
@@ -387,6 +388,7 @@ scan_char_literal (lit, intptr)
              return 0;
            value = 8 * value + digit;
          }
+       assert((p - lit - 2) < INT_MAX);
        digits = (int)(p - lit - 2);
        if (! (1 <= digits && digits <= 3))
          return 0;
index 439b86a..6a41f10 100644 (file)
@@ -230,6 +230,7 @@ slurp (current)
               current->buffer = xrealloc (current->buffer, current->bufsize);
 #endif /*__MSDOS__*/
             }
+                 assert((current->bufsize - current->buffered_chars) < UINT_MAX);
           cc = read (current->desc,
           current->buffer + current->buffered_chars,
           (unsigned int)(current->bufsize - current->buffered_chars));
@@ -872,6 +873,7 @@ find_identical_ends (filevec)
       for (prefix_count = 1;  prefix_count < context + 1;  prefix_count *= 2)
         ;
       prefix_mask = prefix_count - 1;
+         assert((p0 - (char HUGE *)filevec[0].prefix_end) < INT_MAX);
       ttt = (int)(p0 - (char HUGE *)filevec[0].prefix_end);
       alloc_lines0
         = prefix_count
@@ -916,6 +918,7 @@ find_identical_ends (filevec)
 
   /* Allocate line buffer 1.  */
   tem = prefix_count ? filevec[1].suffix_begin - buffer1 : n1;
+  assert((filevec[1].prefix_end - buffer1) < INT_MAX);
   ttt = (int)(filevec[1].prefix_end - buffer1);
   alloc_lines1
     = (buffered_prefix
@@ -941,6 +944,8 @@ find_identical_ends (filevec)
   filevec[0].linbuf = linbuf0 + buffered_prefix;
   filevec[1].linbuf = linbuf1 + buffered_prefix;
   filevec[0].linbuf_base = filevec[1].linbuf_base = - buffered_prefix;
+  assert((alloc_lines0 - buffered_prefix) < INT_MAX);
+  assert((alloc_lines1 - buffered_prefix) < INT_MAX);
   filevec[0].alloc_lines = (int)(alloc_lines0 - buffered_prefix);
   filevec[1].alloc_lines = (int)(alloc_lines1 - buffered_prefix);
   filevec[0].prefix_lines = filevec[1].prefix_lines = lines;
index 07c5717..211f8bc 100644 (file)
@@ -193,7 +193,7 @@ std::string CMarkdown::Entities(const std::string& v)
        char *p, *q = &ret[0];
        while (*(p = q))
        {
-               char *value = 0;
+               char *value = nullptr;
                switch (*p)
                {
                case '&': value = "&amp;"; break;
@@ -203,13 +203,13 @@ std::string CMarkdown::Entities(const std::string& v)
                case '>' : value = "&gt;"; break;
                }
                ++q;
-               if (value)
+               if (value != nullptr)
                {
-                       size_t i = p - &ret[0];
-                       size_t j = q - &ret[0];
                        size_t cchValue = strlen(value);
                        if (cchValue > 1)
                        {
+                               ptrdiff_t i = p - &ret[0];
+                               ptrdiff_t j = q - &ret[0];
                                size_t b = v.length();
                                ret.resize(b + cchValue - 1);
                                p = &ret[0] + i;