Skip to content

Commit 7ee1145

Browse files
committed
SI-6631 Handle invalid escapes in string interpolators
Patch contributed by Rex Kerr.
1 parent f16f4ab commit 7ee1145

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/library/scala/StringContext.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,22 +191,23 @@ object StringContext {
191191
var cur = 0
192192
var idx = 0
193193
def output(ch: Char) = {
194-
bldr append str.substring (start, cur)
194+
bldr.append(str, start, cur)
195195
bldr append ch
196196
start = idx
197197
}
198198
while (idx < len) {
199199
cur = idx
200200
if (str(idx) == '\\') {
201201
idx += 1
202+
if (idx >= len) throw new InvalidEscapeException(str, cur)
202203
if ('0' <= str(idx) && str(idx) <= '7') {
203204
val leadch = str(idx)
204205
var oct = leadch - '0'
205206
idx += 1
206-
if ('0' <= str(idx) && str(idx) <= '7') {
207+
if (idx < len && '0' <= str(idx) && str(idx) <= '7') {
207208
oct = oct * 8 + str(idx) - '0'
208209
idx += 1
209-
if (leadch <= '3' && '0' <= str(idx) && str(idx) <= '7') {
210+
if (idx < len && leadch <= '3' && '0' <= str(idx) && str(idx) <= '7') {
210211
oct = oct * 8 + str(idx) - '0'
211212
idx += 1
212213
}
@@ -234,6 +235,6 @@ object StringContext {
234235
}
235236
}
236237
if (start == 0) str
237-
else (bldr append str.substring(start, idx)).toString
238+
else bldr.append(str, start, idx).toString
238239
}
239240
}

test/files/run/t6631.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import reflect.ClassTag
2+
3+
object Test extends App {
4+
def intercept[T <: Throwable : ClassTag](act: => Any) = try {
5+
act
6+
} catch {
7+
case x: Throwable =>
8+
val cls = implicitly[ClassTag[T]].runtimeClass
9+
assert(cls.isInstance(x), (x.getClass, x, cls).toString)
10+
}
11+
assert(s"""\f\r\n\t""" == "\f\r\n\t")
12+
13+
import StringContext.InvalidEscapeException
14+
intercept[InvalidEscapeException](s"""\""")
15+
intercept[InvalidEscapeException](s"""\x""")
16+
intercept[InvalidEscapeException](s"\")
17+
18+
}

0 commit comments

Comments
 (0)