diff options
author | Mark Wielaard <mark@klomp.org> | 2021-08-03 22:45:38 +0200 |
---|---|---|
committer | Mark Wielaard <mark@klomp.org> | 2021-08-04 00:34:38 +0200 |
commit | d04886a4ea8e91801455a2b9808c1c9bbec896e4 (patch) | |
tree | 3ae6e2c5147eb9f433bfe05bbf5d66850fc99f04 /gcc/rust/parse/rust-parse-impl.h | |
parent | c707190b29e1e5b2d20fa9d68dcd7f043659aed4 (diff) | |
download | gcc-d04886a4ea8e91801455a2b9808c1c9bbec896e4.zip gcc-d04886a4ea8e91801455a2b9808c1c9bbec896e4.tar.gz gcc-d04886a4ea8e91801455a2b9808c1c9bbec896e4.tar.bz2 |
Always check the result of expect_token while parsing
When expect_token fails it produces an error and return a
nullptr. Make sure to always check the result of expect_token so we
don't use a nullptr token and crash.
Resolves: https://github.com/Rust-GCC/gccrs/issues/603
Diffstat (limited to 'gcc/rust/parse/rust-parse-impl.h')
-rw-r--r-- | gcc/rust/parse/rust-parse-impl.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 9eb212b..acc9d06 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -1898,6 +1898,9 @@ Parser<ManagedTokenSource>::parse_macro_match_fragment () // get MacroFragSpec for macro const_TokenPtr t = expect_token (IDENTIFIER); + if (t == nullptr) + return nullptr; + AST::MacroFragSpec frag = AST::get_frag_spec_from_str (t->get_str ()); if (frag == AST::INVALID) { @@ -4325,6 +4328,9 @@ Parser<ManagedTokenSource>::parse_enum (AST::Visibility vis, // parse enum name const_TokenPtr enum_name_tok = expect_token (IDENTIFIER); + if (enum_name_tok == nullptr) + return nullptr; + Identifier enum_name = enum_name_tok->get_str (); // parse generic params (of enum container, not enum variants) if they exist @@ -4650,6 +4656,9 @@ Parser<ManagedTokenSource>::parse_static_item (AST::Visibility vis, } const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); if (!skip_token (COLON)) @@ -4700,6 +4709,9 @@ Parser<ManagedTokenSource>::parse_trait (AST::Visibility vis, // parse trait name const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); // parse generic parameters (if they exist) @@ -4805,6 +4817,9 @@ Parser<ManagedTokenSource>::parse_trait_item () // parse function or method name const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); // parse generic params @@ -4937,6 +4952,9 @@ Parser<ManagedTokenSource>::parse_trait_type (AST::AttrVec outer_attrs) skip_token (TYPE); const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); std::vector<std::unique_ptr<AST::TypeParamBound>> bounds; @@ -4973,6 +4991,9 @@ Parser<ManagedTokenSource>::parse_trait_const (AST::AttrVec outer_attrs) // parse constant item name const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); if (!skip_token (COLON)) @@ -5338,6 +5359,9 @@ Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method ( // parse function or method name const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); // parse generic params @@ -14210,6 +14234,9 @@ Parser<ManagedTokenSource>::parse_field_access_expr ( /* get field name identifier (assume that this is a field access expr and not * await, for instance) */ const_TokenPtr ident_tok = expect_token (IDENTIFIER); + if (ident_tok == nullptr) + return nullptr; + Identifier ident = ident_tok->get_str (); Location locus = struct_expr->get_locus_slow (); |