libgo patch committed: Reject surrogate pair in range over string

Submitted by Ian Taylor on Sept. 22, 2012, 7:19 a.m.

Details

Message ID mcrhaqqo77j.fsf@google.com
State New
Headers show

Commit Message

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 hide | download patch | download mbox

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;
     }