From 72335078034e3cd7edcb1739556b405a3e1e9bf8 Mon Sep 17 00:00:00 2001 From: Charles Forsyth Date: Thu, 6 Jun 2013 20:59:23 +0000 Subject: sync with plan 9 --- libbio/bgetrune.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'libbio/bgetrune.c') diff --git a/libbio/bgetrune.c b/libbio/bgetrune.c index ed216379..13b02bec 100644 --- a/libbio/bgetrune.c +++ b/libbio/bgetrune.c @@ -6,7 +6,7 @@ Bgetrune(Biobuf *bp) { int c, i; Rune rune; - char str[4]; + char str[UTFmax]; c = Bgetc(bp); if(c < Runeself) { /* one char */ @@ -14,19 +14,25 @@ Bgetrune(Biobuf *bp) return c; } str[0] = c; + bp->runesize = 0; for(i=1;;) { c = Bgetc(bp); if(c < 0) return c; + if (i >= sizeof str) + return Runeerror; str[i++] = c; if(fullrune(str, i)) { + /* utf is long enough to be a rune, but could be bad. */ bp->runesize = chartorune(&rune, str); - while(i > bp->runesize) { - Bungetc(bp); - i--; - } + if (rune == Runeerror) + bp->runesize = 0; /* push back nothing */ + else + /* push back bytes unconsumed by chartorune */ + for(; i > bp->runesize; i--) + Bungetc(bp); return rune; } } -- cgit v1.2.3