ecere/gui/EditBox: Fixed hanging caused by 308c3f8a022f58c179f5d52f87be2609fba4ddd8
[sdk] / ecere / src / gui / controls / EditBox.ec
index 7361a32..ff0116a 100644 (file)
@@ -47,28 +47,48 @@ public:
       byte b = (string)[0]; \
       int i; \
       byte mask = 0x7F; \
-      numBytes = 1; \
+      numBytes = b ? 1 : 0; \
       ch = 0; \
-      if(b & 0x80 && b & 0x40) \
+      if(b & 0x80) \
       { \
-         mask >>= 2; \
-         numBytes++; \
-         if(b & 0x20) \
+         if(b & 0x40) \
          { \
+            mask >>= 2; \
             numBytes++; \
-            mask >>= 1; \
-            if(b & 0x10) \
+            if(b & 0x20) \
             { \
                numBytes++; \
                mask >>= 1; \
+               if(b & 0x10) \
+               { \
+                  if(b & 0x08) { numBytes = 0; } \
+                  numBytes++; \
+                  mask >>= 1; \
+               } \
             } \
          } \
+         else \
+            numBytes = 0; \
       } \
       for(i = 0; i<numBytes; i++) \
       { \
          ch <<= 6; \
-         ch |= (string)[i] & mask; \
+         ch |= (b = (string)[i]) & mask; \
          mask = 0x3F; \
+         if(i > 1 && (!(b & 0x80) || (b & 0x40))) \
+         { \
+            numBytes = 0; \
+            ch = 0; \
+         } \
+      } \
+      if(i < numBytes || \
+         ch > 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF) || \
+        (ch < 0x80 && numBytes > 1) || \
+        (ch < 0x800 && numBytes > 2) || \
+        (ch < 0x10000 && numBytes > 3))\
+      { \
+         ch = 0; \
+         numBytes = 0; \
       } \
       ch; \
    })
@@ -122,7 +142,7 @@ public class OldArray
       if(type.type == normalClass || type.type == noHeadClass)
       {
          for(c = 0; c<size; c++)
-            type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
+            ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
       }
       // TODO: Call OnFree for structClass
       delete ((ArrayImpl)this).array;
@@ -639,7 +659,7 @@ static char * keyWords1[] =
    "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
 
    // Types
-   "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
+   "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
 
    // Values
    "this", "true", "false", "null", "value",
@@ -1668,7 +1688,7 @@ private:
                         }
                         else if(wordLen == 1 && word[0] == '*')
                         {
-                           if(!c || word[-1] != '/')
+                           if(c < 2 || word[-1] != '/')
                               lastWasStar = true;
                         }
                         else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
@@ -2004,9 +2024,8 @@ private:
       int c, position = 0;
       unichar ch;
       int nb;
-      for(c = 0; c<this.line.count && c<this.x; c+= nb)
+      for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
       {
-         ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
          // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
          if(ch == '\t')
             position += this.tabSize - (position % this.tabSize);
@@ -3349,7 +3368,7 @@ private:
             for(c = start; c<line.count; c += numBytes)
             {
                unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
-               if(!IS_ALUNDER(ch))
+               if(!ch || !IS_ALUNDER(ch))
                   break;
             }
             SelDirty();
@@ -3791,9 +3810,9 @@ private:
                      int start = (line == this.line) ? this.x : 0;
                      int c;
                      int numBytes;
-                     for(c = start; c < line.count; c += numBytes)
+                     unichar ch;
+                     for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
                      {
-                        unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
                         if(IS_ALUNDER(ch))
                         {
                            foundAlpha = true;
@@ -3840,9 +3859,9 @@ private:
                      int start = (line == this.line) ? this.x : 0;
                      int c;
                      int numBytes;
-                     for(c = start; c < line.count; c += numBytes)
+                     unichar ch;
+                     for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
                      {
-                        unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
                         if(!IS_ALUNDER(ch))
                            foundAlpha = true;
                         else if(foundAlpha)
@@ -4463,7 +4482,7 @@ private:
                         ComputeColumn();
                      }
                      // Insert spaces
-                     start = this.x;
+                     start = Min(this.x, this.selX);
                      for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
                      {
                         addString[len++] = ' ';
@@ -4933,6 +4952,7 @@ private:
       }
       if(addCharAction)
       {
+         addCharAction.x -= addedTabs * (tabSize-1);
          addCharAction.addedSpaces = addedSpaces;
          addCharAction.addedTabs = addedTabs;
       }
@@ -5277,6 +5297,7 @@ public:
          }  
          if(addCharAction)
          {
+            addCharAction.x -= addedTabs * (tabSize-1);
             addCharAction.addedSpaces = addedSpaces;
             addCharAction.addedTabs = addedTabs;
          }
@@ -5305,7 +5326,8 @@ public:
          char temp[MAX_F_STRING];
          va_list args;
          va_start(args, format);
-         vsprintf(temp, format, args);
+         vsnprintf(temp, sizeof(temp), format, args);
+         temp[sizeof(temp)-1] = 0;
          va_end(args);
          PutS(temp);
       }
@@ -5322,7 +5344,8 @@ public:
             char temp[MAX_F_STRING];
             va_list args;
             va_start(args, format);
-            vsprintf(temp, format, args);
+            vsnprintf(temp, sizeof(temp), format, args);
+            temp[sizeof(temp)-1] = 0;
             va_end(args);
 
             AddS(temp);
@@ -5967,9 +5990,11 @@ public:
       if(this)
       {
          Copy();
-         DelSel(null);
-         SetViewToCursor(true);
-         Modified();
+         if(DelSel(null))
+         {
+            SetViewToCursor(true);
+            Modified();
+         }
       }
    }