Skip to content

Commit 14ea5cf

Browse files
aviraxptopjohnwu
authored andcommitted
Fix sepolicy parsing for hyphenated identifiers
1 parent 9d0b529 commit 14ea5cf

1 file changed

Lines changed: 40 additions & 18 deletions

File tree

native/src/sepolicy/statement.rs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,41 @@ fn parse_sterm<'a>(tokens: &mut Tokens<'a>) -> ParseResult<'a, Vec<&'a str>> {
119119
}
120120
}
121121

122+
fn parse_xperm_hex(s: &str) -> Option<u16> {
123+
s.strip_prefix("0x").and_then(|s| u16::from_str_radix(s, 16).ok())
124+
}
125+
126+
fn parse_xperm_range(s: &str) -> Option<(u16, u16)> {
127+
let (low, high) = s.split_once('-')?;
128+
Some((parse_xperm_hex(low)?, parse_xperm_hex(high)?))
129+
}
130+
122131
// xperm ::= HX(low) { Xperm{low, high: low, reset: false} };
123132
// xperm ::= HX(low) HP HX(high) { Xperm{low, high, reset: false} };
133+
// xperm ::= ID("0x<low>-0x<high>") { Xperm{low, high, reset: false} };
124134
fn parse_xperm<'a>(tokens: &mut Tokens<'a>) -> ParseResult<'a, Xperm> {
125-
let low = match tokens.next() {
126-
Some(Token::HX(low)) => low,
127-
_ => throw!(),
128-
};
129-
let high = match tokens.peek() {
130-
Some(Token::HP) => {
131-
tokens.next();
132-
match tokens.next() {
133-
Some(Token::HX(high)) => high,
134-
_ => throw!(),
135+
let (low, high) = match tokens.next() {
136+
Some(Token::HX(low)) => {
137+
let high = match tokens.peek() {
138+
Some(Token::HP) => {
139+
tokens.next();
140+
match tokens.next() {
141+
Some(Token::HX(high)) => high,
142+
_ => throw!(),
143+
}
144+
}
145+
_ => low,
146+
};
147+
(low, high)
148+
}
149+
Some(Token::ID(s)) => {
150+
if let Some((low, high)) = parse_xperm_range(s) {
151+
(low, high)
152+
} else {
153+
throw!()
135154
}
136155
}
137-
_ => low,
156+
_ => throw!(),
138157
};
139158
Ok(Xperm {
140159
low,
@@ -144,6 +163,7 @@ fn parse_xperm<'a>(tokens: &mut Tokens<'a>) -> ParseResult<'a, Xperm> {
144163
}
145164

146165
// xperms ::= HX(low) { if low > 0 { vec![Xperm{low, high: low, reset: false}] } else { vec![Xperm{low: 0x0000, high: 0xFFFF, reset: true}] }};
166+
// xperms ::= ID("0x<low>-0x<high>") { vec![Xperm{low, high, reset: false}] };
147167
// xperms ::= LB xperm_list(l) RB { l };
148168
// xperms ::= TL LB xperm_list(mut l) RB { l.iter_mut().for_each(|x| { x.reset = true; }); l };
149169
// xperms ::= ST { vec![Xperm{low: 0x0000, high: 0xFFFF, reset: false}] };
@@ -197,6 +217,13 @@ fn parse_xperms<'a>(tokens: &mut Tokens<'a>) -> ParseResult<'a, Vec<Xperm>> {
197217
});
198218
}
199219
}
220+
Some(Token::ID(s)) => {
221+
if let Some((low, high)) = parse_xperm_range(s) {
222+
xperms.push(Xperm { low, high, reset });
223+
} else {
224+
throw!();
225+
}
226+
}
200227
_ => throw!(),
201228
}
202229
Ok(xperms)
@@ -247,16 +274,11 @@ fn extract_token<'a>(s: &'a str, tokens: &mut Vec<Token<'a>>) {
247274
extract_token(a, tokens);
248275
tokens.push(Token::CM);
249276
extract_token(&b[1..], tokens);
250-
} else if let Some(idx) = s.find('-') {
251-
let (a, b) = s.split_at(idx);
252-
extract_token(a, tokens);
253-
tokens.push(Token::HP);
254-
extract_token(&b[1..], tokens);
255277
} else if let Some(s) = s.strip_prefix('~') {
256278
tokens.push(Token::TL);
257279
extract_token(s, tokens);
258-
} else if let Some(s) = s.strip_prefix("0x") {
259-
tokens.push(Token::HX(s.parse().unwrap_or(0)));
280+
} else if let Some(n) = parse_xperm_hex(s) {
281+
tokens.push(Token::HX(n));
260282
} else {
261283
tokens.push(Token::ID(s));
262284
}

0 commit comments

Comments
 (0)