Patchwork libgo patch committed: Reject surrogate pair in range over string

login
register
mail settings
Submitter Ian Taylor
Date Sept. 22, 2012, 7:19 a.m.
Message ID <mcrhaqqo77j.fsf@google.com>
Download mbox | patch
Permalink /patch/186106/
State New
Headers show

Comments

Ian Taylor - Sept. 22, 2012, 7:19 a.m.
Similar to the last patch, this rejects a surrogate pair, which is
invalid UTF-8, when doing a range over a string.  Bootstrapped and ran
Go testsuite on x86_64-unknown-linux-gnu.  Committed to mainline and 4.7
branch.

Ian

Patch

diff -r 2a400cc3eae6 libgo/runtime/go-rune.c
--- a/libgo/runtime/go-rune.c	Fri Sep 21 23:51:25 2012 -0700
+++ b/libgo/runtime/go-rune.c	Fri Sep 21 23:57:48 2012 -0700
@@ -53,6 +53,14 @@ 
       *rune = (((c & 0xf) << 12)
 	       + ((c1 & 0x3f) << 6)
 	       + (c2 & 0x3f));
+
+      if (*rune >= 0xd800 && *rune < 0xe000)
+	{
+	  /* Invalid surrogate half; return replace character.  */
+	  *rune = 0xfffd;
+	  return 1;
+	}
+
       return 3;
     }