Skip to content

Commit c756038

Browse files
committed
libgetopts -- fix unsafe sharing in closures
1 parent 7ffa67c commit c756038

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/libgetopts/lib.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -775,14 +775,13 @@ fn each_split_within<'a>(ss: &'a str, lim: uint, it: |&'a str| -> bool)
775775
let mut lim = lim;
776776
777777
let mut cont = true;
778-
let slice: || = || { cont = it(ss.slice(slice_start, last_end)) };
779778
780779
// if the limit is larger than the string, lower it to save cycles
781780
if lim >= fake_i {
782781
lim = fake_i;
783782
}
784783
785-
let machine: |(uint, char)| -> bool = |(i, c)| {
784+
let machine: |&mut bool, (uint, char)| -> bool = |cont, (i, c)| {
786785
let whitespace = if ::std::char::is_whitespace(c) { Ws } else { Cr };
787786
let limit = if (i - slice_start + 1) <= lim { UnderLim } else { OverLim };
788787
@@ -794,24 +793,49 @@ fn each_split_within<'a>(ss: &'a str, lim: uint, it: |&'a str| -> bool)
794793
(B, Cr, OverLim) if (i - last_start + 1) > lim
795794
=> fail!("word starting with {} longer than limit!",
796795
ss.slice(last_start, i + 1)),
797-
(B, Cr, OverLim) => { slice(); slice_start = last_start; B }
798-
(B, Ws, UnderLim) => { last_end = i; C }
799-
(B, Ws, OverLim) => { last_end = i; slice(); A }
800-
801-
(C, Cr, UnderLim) => { last_start = i; B }
802-
(C, Cr, OverLim) => { slice(); slice_start = i; last_start = i; last_end = i; B }
803-
(C, Ws, OverLim) => { slice(); A }
804-
(C, Ws, UnderLim) => { C }
796+
(B, Cr, OverLim) => {
797+
*cont = it(ss.slice(slice_start, last_end));
798+
slice_start = last_start;
799+
B
800+
}
801+
(B, Ws, UnderLim) => {
802+
last_end = i;
803+
C
804+
}
805+
(B, Ws, OverLim) => {
806+
last_end = i;
807+
*cont = it(ss.slice(slice_start, last_end));
808+
A
809+
}
810+
811+
(C, Cr, UnderLim) => {
812+
last_start = i;
813+
B
814+
}
815+
(C, Cr, OverLim) => {
816+
*cont = it(ss.slice(slice_start, last_end));
817+
slice_start = i;
818+
last_start = i;
819+
last_end = i;
820+
B
821+
}
822+
(C, Ws, OverLim) => {
823+
*cont = it(ss.slice(slice_start, last_end));
824+
A
825+
}
826+
(C, Ws, UnderLim) => {
827+
C
828+
}
805829
};
806830

807-
cont
831+
*cont
808832
};
809833

810-
ss.char_indices().advance(|x| machine(x));
834+
ss.char_indices().advance(|x| machine(&mut cont, x));
811835

812836
// Let the automaton 'run out' by supplying trailing whitespace
813837
while cont && match state { B | C => true, A => false } {
814-
machine((fake_i, ' '));
838+
machine(&mut cont, (fake_i, ' '));
815839
fake_i += 1;
816840
}
817841
return cont;

0 commit comments

Comments
 (0)