Răsfoiți Sursa

update jerry_buffer.c

Signed-off-by: yangfasheng <yangfasheng@rt-thread.com>
yangfasheng 7 ani în urmă
părinte
comite
9c05a7fc00
2 a modificat fișierele cu 68 adăugiri și 2 ștergeri
  1. 65 2
      rtthread-port/jerry_buffer.c
  2. 3 0
      rtthread-port/port.c

+ 65 - 2
rtthread-port/jerry_buffer.c

@@ -24,6 +24,63 @@
 static jerry_value_t jerry_buffer_prototype;
 js_buffer_t *jerry_buffer_find(const jerry_value_t obj);
 
+bool is_utf8_string(const void* str, int size)
+{
+    bool ret = true;
+    unsigned char* start = (unsigned char*)str;
+    unsigned char* end = (unsigned char*)str + size;
+
+    while (start < end)
+    {
+        if (*start < 0x80) 				// (10000000): 值小于0x80的为ASCII字符    
+        {
+            start++;
+        }
+        else if (*start < (0xC0)) 		// (11000000): 值介于0x80与0xC0之间的为无效UTF-8字符    
+        {
+            ret = false;
+            break;
+        }
+        else if (*start < (0xE0)) 		// (11100000): 此范围内为2字节UTF-8字符    
+        {
+            if (start >= end - 1)
+            {
+                break;
+            }
+
+            if ((start[1] & (0xC0)) != 0x80)
+            {
+                ret = false;
+                break;
+            }
+
+            start += 2;
+        }
+        else if (*start < (0xF0)) 		// (11110000): 此范围内为3字节UTF-8字符    
+        {
+            if (start >= end - 2)
+            {
+                break;
+            }
+
+            if ((start[1] & (0xC0)) != 0x80 || (start[2] & (0xC0)) != 0x80)
+            {
+                ret = false;
+                break;
+            }
+
+            start += 3;
+        }
+        else
+        {
+            ret = false;
+            break;
+        }
+    }
+
+    return ret;
+}
+
 static void jerry_buffer_callback_free(void *handle)
 {
     js_buffer_t *item = (js_buffer_t *)handle;
@@ -320,7 +377,10 @@ DECLARE_HANDLER(toString)
 
         str += start;
         rt_free(enc);
-        return jerry_create_string_sz_from_utf8(str, end - start);
+        if (is_utf8_string(str, end - start))
+            return jerry_create_string_sz_from_utf8(str, end - start);
+        else
+            return jerry_create_undefined();
     }
     else if (strequal(encoding, "ascii"))
     {
@@ -341,7 +401,10 @@ DECLARE_HANDLER(toString)
         }
 
         jerry_value_t jstr;
-        jstr = jerry_create_string_sz_from_utf8((jerry_char_t *)str, index - start);
+        if (is_utf8_string(str, index - start))
+            jstr = jerry_create_string_sz_from_utf8((jerry_char_t *)str, index - start);
+        else
+            jstr = jerry_create_undefined();
         free(str);
         free(enc);
         return jstr;

+ 3 - 0
rtthread-port/port.c

@@ -37,6 +37,8 @@ extern void jmem_heap_stats_print (void);
  */
 void jerry_port_fatal(jerry_fatal_code_t code)
 {
+    extern void jmem_heap(void);
+
     rt_kprintf("jerryScritp fatal [");
     switch (code)
     {
@@ -58,6 +60,7 @@ void jerry_port_fatal(jerry_fatal_code_t code)
         break;
     };
     rt_kprintf("]...\n");
+    jmem_heap();
     rt_hw_interrupt_disable();
     while (1);
 }