]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
compiler, runtime: Reject surrogate pair converting int to string.
authorIan Lance Taylor <ian@gcc.gnu.org>
Sat, 22 Sep 2012 06:51:59 +0000 (06:51 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Sat, 22 Sep 2012 06:51:59 +0000 (06:51 +0000)
From-SVN: r191636

gcc/go/gofrontend/lex.cc
libgo/runtime/go-int-to-string.c

index 25aaeb794a55ce2975a22e3e70a01f5a1e07d66e..6add84ed1f7d7c6938c8de3a1138ffbfc9783a23 100644 (file)
@@ -1312,6 +1312,12 @@ Lex::append_char(unsigned int v, bool is_character, std::string* str,
          // Turn it into the "replacement character".
          v = 0xfffd;
        }
+      if (v >= 0xd800 && v < 0xe000)
+       {
+         warning_at(location, 0,
+                    "unicode code point 0x%x is invalid surrogate pair", v);
+         v = 0xfffd;
+       }
       if (v <= 0xffff)
        {
          buf[0] = 0xe0 + (v >> 12);
index e9645bf98feba0826490a94649f8b9edf0d4c0fe..17a5fcb04c011f14410a10ad543ad3769d4e53e4 100644 (file)
@@ -17,6 +17,11 @@ __go_int_to_string (int v)
   unsigned char *retdata;
   struct __go_string ret;
 
+  /* A negative value is not valid UTF-8; turn it into the replacement
+     character.  */
+  if (v < 0)
+    v = 0xfffd;
+
   if (v <= 0x7f)
     {
       buf[0] = v;
@@ -34,6 +39,10 @@ __go_int_to_string (int v)
         "replacement character".  */
       if (v > 0x10ffff)
        v = 0xfffd;
+      /* If the value is a surrogate pair, which is invalid in UTF-8,
+        turn it into the replacement character.  */
+      if (v >= 0xd800 && v < 0xe000)
+       v = 0xfffd;
 
       if (v <= 0xffff)
        {