Skip to content

Commit 9ed1331

Browse files
Harishankar14P-E-P
authored andcommitted
parser: Fix ICE on invalid arithmetic expression
The compiler previously crashed with a segmentation fault when parsing an arithmetic expression where the left-hand side was invalid (e.g., after a failed macro expansion). This occurred because the parser attempted to access the location of the null 'left' pointer. This patch adds a check to ensure the left operand is valid before proceeding. Fixes #4414 gcc/rust/ChangeLog: * parse/rust-parse-impl-expr.hxx (parse_arithmetic_or_logical_expr): Check for null left operand to prevent segmentation fault. gcc/testsuite/ChangeLog: * rust/compile/issue-4414.rs: New test. Signed-off-by: Harishankar <harishankarpp7@gmail.com>
1 parent 4c81795 commit 9ed1331

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

gcc/rust/parse/rust-parse-impl-expr.hxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1794,7 +1794,9 @@ Parser<ManagedTokenSource>::parse_expr (int right_binding_power,
17941794
= null_denotation ({}, null_denotation_restrictions);
17951795
if (!expr)
17961796
return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
1797-
1797+
if (expr.value () == nullptr)
1798+
return tl::unexpected<Parse::Error::Expr> (Parse::Error::Expr::CHILD_ERROR);
1799+
17981800
return left_denotations (std::move (expr), right_binding_power,
17991801
std::move (outer_attrs), restrictions);
18001802
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![feature(no_core)]
2+
#![no_core]
3+
4+
trait Speak : Sized {
5+
fn say(&self, s:&str) -> String;
6+
fn hi(&self) -> String { hello(self) }
7+
}
8+
9+
fn hello<S:Speak>(s:&S) -> String{
10+
s.say("hello")
11+
}
12+
13+
impl Speak for assert_eq!(Some(3).hi(), "something!hello: 3".to_string()) {
14+
fn say(&self, s:&str) -> String {
15+
format!("{}: {}", s, *self)
16+
}
17+
}
18+
19+
impl<T: Speak> Speak for Option<T> {
20+
fn say(&self, s:&str) -> String {
21+
match something!hello - none { // { dg-error "unexpected token 'identifier'|failed to parse scrutinee" }
22+
None => format!("{} - none", s),
23+
Some(ref x) => { format!("something!{}", x.say(s)) }
24+
}
25+
}
26+
} // { dg-error "could not parse definition" }
27+
28+
fn hello<S:Speak>(s:&S) -> String{ // { dg-error "failed to parse trait impl item" }
29+
s.say("hello")
30+
}

0 commit comments

Comments
 (0)