Skip to content

Commit 2bd628e

Browse files
committed
auto merge of #8944 : alexcrichton/rust/issue-8938, r=huonw
Otherwise extra stuff after a lone '}' character is simply ignored, which is very bad. Closes #8938
2 parents d285ea7 + ba1f663 commit 2bd628e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/libstd/fmt/parse.rs

+11
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ pub struct SelectArm<'self> {
149149
pub struct Parser<'self> {
150150
priv input: &'self str,
151151
priv cur: str::CharOffsetIterator<'self>,
152+
priv depth: uint,
152153
}
153154

154155
impl<'self> iterator::Iterator<Piece<'self>> for Parser<'self> {
@@ -168,6 +169,11 @@ impl<'self> iterator::Iterator<Piece<'self>> for Parser<'self> {
168169
self.escape(); // ensure it's a valid escape sequence
169170
Some(String(self.string(pos + 1))) // skip the '\' character
170171
}
172+
Some((_, '}')) if self.depth == 0 => {
173+
self.cur.next();
174+
self.err(~"unmatched `}` found");
175+
None
176+
}
171177
Some((_, '}')) | None => { None }
172178
Some((pos, _)) => {
173179
Some(String(self.string(pos)))
@@ -182,6 +188,7 @@ impl<'self> Parser<'self> {
182188
Parser {
183189
input: s,
184190
cur: s.char_offset_iter(),
191+
depth: 0,
185192
}
186193
}
187194

@@ -393,7 +400,9 @@ impl<'self> Parser<'self> {
393400
if !self.wsconsume('{') {
394401
self.err(~"selector must be followed by `{`");
395402
}
403+
self.depth += 1;
396404
let pieces = self.collect();
405+
self.depth -= 1;
397406
if !self.wsconsume('}') {
398407
self.err(~"selector case must be terminated by `}`");
399408
}
@@ -494,7 +503,9 @@ impl<'self> Parser<'self> {
494503
if !self.wsconsume('{') {
495504
self.err(~"selector must be followed by `{`");
496505
}
506+
self.depth += 1;
497507
let pieces = self.collect();
508+
self.depth -= 1;
498509
if !self.wsconsume('}') {
499510
self.err(~"selector case must be terminated by `}`");
500511
}

src/test/compile-fail/ifmt-bad-arg.rs

+3
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,7 @@ fn main() {
7171
format!("{0, select, other{{}}}", "a"); //~ ERROR: cannot use implicit
7272
format!("{0, plural, other{{}}}", 1); //~ ERROR: cannot use implicit
7373
format!("{0, plural, other{{1:.*d}}}", 1, 2); //~ ERROR: cannot use implicit
74+
75+
format!("foo } bar"); //~ ERROR: unmatched `}` found
76+
format!("foo }"); //~ ERROR: unmatched `}` found
7477
}

0 commit comments

Comments
 (0)