Skip to content

Commit 8a3544a

Browse files
evgenyx00alamb
andauthored
Fix panic with GRANT/REVOKE in CONNECT, CREATE, EXECUTE or TEMPORARY (#401)
* fix inconsistency between parse_grant_permissions and matched keywords * Make it clear the error is an internal problem Co-authored-by: Andrew Lamb <[email protected]>
1 parent c4cbc83 commit 8a3544a

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/parser.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -3240,22 +3240,38 @@ impl<'a> Parser<'a> {
32403240
with_privileges_keyword: self.parse_keyword(Keyword::PRIVILEGES),
32413241
}
32423242
} else {
3243-
Privileges::Actions(
3244-
self.parse_comma_separated(Parser::parse_grant_permission)?
3245-
.into_iter()
3246-
.map(|(kw, columns)| match kw {
3247-
Keyword::DELETE => Action::Delete,
3248-
Keyword::INSERT => Action::Insert { columns },
3249-
Keyword::REFERENCES => Action::References { columns },
3250-
Keyword::SELECT => Action::Select { columns },
3251-
Keyword::TRIGGER => Action::Trigger,
3252-
Keyword::TRUNCATE => Action::Truncate,
3253-
Keyword::UPDATE => Action::Update { columns },
3254-
Keyword::USAGE => Action::Usage,
3255-
_ => unreachable!(),
3256-
})
3257-
.collect(),
3258-
)
3243+
let (actions, err): (Vec<_>, Vec<_>) = self
3244+
.parse_comma_separated(Parser::parse_grant_permission)?
3245+
.into_iter()
3246+
.map(|(kw, columns)| match kw {
3247+
Keyword::DELETE => Ok(Action::Delete),
3248+
Keyword::INSERT => Ok(Action::Insert { columns }),
3249+
Keyword::REFERENCES => Ok(Action::References { columns }),
3250+
Keyword::SELECT => Ok(Action::Select { columns }),
3251+
Keyword::TRIGGER => Ok(Action::Trigger),
3252+
Keyword::TRUNCATE => Ok(Action::Truncate),
3253+
Keyword::UPDATE => Ok(Action::Update { columns }),
3254+
Keyword::USAGE => Ok(Action::Usage),
3255+
Keyword::CONNECT => Ok(Action::Connect),
3256+
Keyword::CREATE => Ok(Action::Create),
3257+
Keyword::EXECUTE => Ok(Action::Execute),
3258+
Keyword::TEMPORARY => Ok(Action::Temporary),
3259+
// This will cover all future added keywords to
3260+
// parse_grant_permission and unhandled in this
3261+
// match
3262+
_ => Err(kw),
3263+
})
3264+
.partition(Result::is_ok);
3265+
3266+
if !err.is_empty() {
3267+
let errors: Vec<Keyword> = err.into_iter().filter_map(|x| x.err()).collect();
3268+
return Err(ParserError::ParserError(format!(
3269+
"INTERNAL ERROR: GRANT/REVOKE unexpected keyword(s) - {:?}",
3270+
errors
3271+
)));
3272+
}
3273+
let act = actions.into_iter().filter_map(|x| x.ok()).collect();
3274+
Privileges::Actions(act)
32593275
};
32603276

32613277
self.expect_keyword(Keyword::ON)?;

tests/sqlparser_common.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3898,7 +3898,7 @@ fn parse_drop_index() {
38983898

38993899
#[test]
39003900
fn parse_grant() {
3901-
let sql = "GRANT SELECT, INSERT, UPDATE (shape, size), USAGE, DELETE, TRUNCATE, REFERENCES, TRIGGER ON abc, def TO xyz, m WITH GRANT OPTION GRANTED BY jj";
3901+
let sql = "GRANT SELECT, INSERT, UPDATE (shape, size), USAGE, DELETE, TRUNCATE, REFERENCES, TRIGGER, CONNECT, CREATE, EXECUTE, TEMPORARY ON abc, def TO xyz, m WITH GRANT OPTION GRANTED BY jj";
39023902
match verified_stmt(sql) {
39033903
Statement::Grant {
39043904
privileges,
@@ -3930,6 +3930,10 @@ fn parse_grant() {
39303930
Action::Truncate,
39313931
Action::References { columns: None },
39323932
Action::Trigger,
3933+
Action::Connect,
3934+
Action::Create,
3935+
Action::Execute,
3936+
Action::Temporary,
39333937
],
39343938
actions
39353939
);

0 commit comments

Comments
 (0)