handle empty pipeline while parsing let (fix Issue10083) (#10116)

- fixes #10083 

# Description

nushell crashes in the following 2 condition:

- `let a = {}` , then delete `{`
- `let a = | {}`, then delete `{`

When delete `{` the pipeline becomes empty but current `nu-parser`
assume they are non-empty. This pr adds extra empty check to avoid
crash.


Co-authored-by: Horasal <horsal@horsal.dev>
This commit is contained in:
Horasal 2023-08-28 20:38:11 +09:00 committed by GitHub
parent 38f454d5ab
commit 1f06f8405c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 7 deletions

View File

@ -5476,10 +5476,14 @@ pub fn parse_pipeline(
{
let block = working_set.get_block(*block_id);
let element = block.pipelines[0].elements[0].clone();
if let PipelineElement::Expression(prepend, expr) =
element
if let Some(PipelineElement::Expression(
prepend,
expr,
)) = block
.pipelines
.first()
.and_then(|p| p.elements.first())
.cloned()
{
if expr.has_in_variable(working_set) {
let new_expr = PipelineElement::Expression(
@ -5608,9 +5612,12 @@ pub fn parse_pipeline(
{
let block = working_set.get_block(*block_id);
let element = block.pipelines[0].elements[0].clone();
if let PipelineElement::Expression(prepend, expr) = element {
if let Some(PipelineElement::Expression(prepend, expr)) = block
.pipelines
.first()
.and_then(|p| p.elements.first())
.cloned()
{
if expr.has_in_variable(working_set) {
let new_expr = PipelineElement::Expression(
prepend,

View File

@ -1007,6 +1007,18 @@ mod string {
}
}
#[rstest]
#[case(b"let a = }")]
#[case(b"mut a = }")]
#[case(b"let a = | }")]
#[case(b"mut a = | }")]
fn test_semi_open_brace(#[case] phrase: &[u8]) {
let engine_state = EngineState::new();
let mut working_set = StateWorkingSet::new(&engine_state);
// this should not panic
let _block = parse(&mut working_set, None, phrase, true);
}
mod range {
use super::*;
use nu_protocol::ast::{RangeInclusion, RangeOperator};