Skip to content

Commit 5ae8e57

Browse files
neildgopherbot
authored andcommitted
unicode/norm: avoid infinite loop on invalid input
Invalid characters are given a Properties with a size of 0. The nextComposed function can enter an infinite loop when encountering an invalid character, since it advances its input by the (possibly 0) character size. Rather than finding every place which might assume characters have a non-zero size, change compInfo to return a size-1 Properties for invalid characters and use the property flags to record validity. Fixes golang/go#80142 Change-Id: Ie0791faefeddc1e8f671b0ed73f29e906a6a6964 Reviewed-on: https://go-review.googlesource.com/c/text/+/794100 LUCI-TryBot-Result: [email protected] <[email protected]> Auto-Submit: Damien Neil <[email protected]> Reviewed-by: Neal Patel <[email protected]> Reviewed-by: Neal Patel <[email protected]>
1 parent 0dc94a2 commit 5ae8e57

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

‎unicode/norm/forminfo.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,12 @@ func (p Properties) BoundaryAfter() bool {
121121
//
122122
// When all 6 bits are zero, the character is inert, meaning it is never
123123
// influenced by normalization.
124+
//
125+
// We set flags to 0x80 (high bit 7 unused in quick check data) to indicate an invalid rune.
124126
type qcInfo uint8
125127

128+
func (p Properties) isInvalid() bool { return p.flags == 0x80 }
129+
126130
func (p Properties) isYesC() bool { return p.flags&0x10 == 0 }
127131
func (p Properties) isYesD() bool { return p.flags&0x4 == 0 }
128132

@@ -247,14 +251,17 @@ func (f Form) PropertiesString(s string) Properties {
247251
// to a Properties. See the comment at the top of the file
248252
// for more information on the format.
249253
func compInfo(v uint16, sz int) Properties {
254+
if sz == 0 {
255+
return Properties{flags: 0x80, size: 1}
256+
}
250257
if v == 0 {
251258
return Properties{size: uint8(sz)}
252259
} else if v >= 0x8000 {
253260
p := Properties{
254261
size: uint8(sz),
255262
ccc: uint8(v),
256263
tccc: uint8(v),
257-
flags: qcInfo(v >> 8),
264+
flags: qcInfo(v>>8) & 0x3f,
258265
}
259266
if p.ccc > 0 || p.combinesBackward() {
260267
p.nLead = uint8(p.flags & 0x3)

‎unicode/norm/iter.go‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,12 @@ func nextComposed(i *Iter) []byte {
376376
goto doNorm
377377
}
378378
prevCC = i.info.tccc
379-
sz := int(i.info.size)
380-
if sz == 0 {
381-
sz = 1 // illegal rune: copy byte-by-byte
382-
}
383-
p := outp + sz
379+
p := outp + int(i.info.size)
384380
if p > len(i.buf) {
385381
break
386382
}
387383
outp = p
388-
i.p += sz
384+
i.p += int(i.info.size)
389385
if i.p >= i.rb.nsrc {
390386
i.setDone()
391387
break

‎unicode/norm/normalize.go‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func (f Form) IsNormalString(s string) bool {
148148
// patched buffer and whether the decomposition is still in progress.
149149
func patchTail(rb *reorderBuffer) bool {
150150
info, p := lastRuneStart(&rb.f, rb.out)
151-
if p == -1 || info.size == 0 {
151+
if p == -1 || info.isInvalid() {
152152
return true
153153
}
154154
end := p + int(info.size)
@@ -225,7 +225,7 @@ func doAppend(rb *reorderBuffer, out []byte, p int) []byte {
225225
}
226226
fd := &rb.f
227227
if doMerge {
228-
var info Properties
228+
info := Properties{flags: 0x80, size: 1} // invalid rune
229229
if p < n {
230230
info = fd.info(src, p)
231231
if !info.BoundaryBefore() || info.nLeadingNonStarters() > 0 {
@@ -235,7 +235,7 @@ func doAppend(rb *reorderBuffer, out []byte, p int) []byte {
235235
p = decomposeSegment(rb, p, true)
236236
}
237237
}
238-
if info.size == 0 {
238+
if info.isInvalid() {
239239
rb.doFlush()
240240
// Append incomplete UTF-8 encoding.
241241
return src.appendSlice(rb.out, p, n)
@@ -314,7 +314,7 @@ func (f *formInfo) quickSpan(src input, i, end int, atEOF bool) (n int, ok bool)
314314
continue
315315
}
316316
info := f.info(src, i)
317-
if info.size == 0 {
317+
if info.isInvalid() {
318318
if atEOF {
319319
// include incomplete runes
320320
return n, true
@@ -379,7 +379,7 @@ func (f Form) firstBoundary(src input, nsrc int) int {
379379
// CGJ insertion points correctly. Luckily it doesn't have to.
380380
for {
381381
info := fd.info(src, i)
382-
if info.size == 0 {
382+
if info.isInvalid() {
383383
return -1
384384
}
385385
if s := ss.next(info); s != ssSuccess {
@@ -424,7 +424,7 @@ func (f Form) nextBoundary(src input, nsrc int, atEOF bool) int {
424424
}
425425
fd := formTable[f]
426426
info := fd.info(src, 0)
427-
if info.size == 0 {
427+
if info.isInvalid() {
428428
if atEOF {
429429
return 1
430430
}
@@ -435,7 +435,7 @@ func (f Form) nextBoundary(src input, nsrc int, atEOF bool) int {
435435

436436
for i := int(info.size); i < nsrc; i += int(info.size) {
437437
info = fd.info(src, i)
438-
if info.size == 0 {
438+
if info.isInvalid() {
439439
if atEOF {
440440
return i
441441
}
@@ -465,7 +465,7 @@ func lastBoundary(fd *formInfo, b []byte) int {
465465
if p == -1 {
466466
return -1
467467
}
468-
if info.size == 0 { // ends with incomplete rune
468+
if info.isInvalid() { // ends with incomplete rune
469469
if p == 0 { // starts with incomplete rune
470470
return -1
471471
}
@@ -504,7 +504,7 @@ func lastBoundary(fd *formInfo, b []byte) int {
504504
func decomposeSegment(rb *reorderBuffer, sp int, atEOF bool) int {
505505
// Force one character to be consumed.
506506
info := rb.f.info(rb.src, sp)
507-
if info.size == 0 {
507+
if info.isInvalid() {
508508
return 0
509509
}
510510
if s := rb.ss.next(info); s == ssStarter {
@@ -528,7 +528,7 @@ func decomposeSegment(rb *reorderBuffer, sp int, atEOF bool) int {
528528
break
529529
}
530530
info = rb.f.info(rb.src, sp)
531-
if info.size == 0 {
531+
if info.isInvalid() {
532532
if !atEOF {
533533
return int(iShortSrc)
534534
}

‎unicode/norm/normalize_test.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,11 @@ var appendTestsNFC = []AppendTest{
664664
"a" + rep(0x0305, maxNonStarters+4) + "\u0316",
665665
"a" + rep(0x0305, maxNonStarters) + cgj + "\u0316" + rep(0x305, 4),
666666
},
667+
{ // illegal rune
668+
"",
669+
"\xf3\xcc\x80",
670+
"\xf3\xcc\x80",
671+
},
667672

668673
{ // Combine across non-blocking non-starters.
669674
// U+0327 COMBINING CEDILLA;Mn;202;NSM;;;;;N;NON-SPACING CEDILLA;;;;

0 commit comments

Comments
 (0)