From a96066860faacf7062d2986d218d25a7f1f750dc Mon Sep 17 00:00:00 2001 From: Vanshaj Poonia Date: Wed, 25 Nov 2026 00:39:13 +0530 Subject: [PATCH 1/3] awk: make next and nextfile in a function called from a special rule fatal A function called from BEGIN, END, BEGINFILE or ENDFILE could run next or nextfile and reach an unreachable!(). It now exits with 2 and a gawk-style message naming the rule. --- src/event.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/event.rs b/src/event.rs index 629faf6..d5088b7 100644 --- a/src/event.rs +++ b/src/event.rs @@ -74,7 +74,7 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.begin_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "BEGIN")), } } @@ -94,7 +94,7 @@ impl<'a> AwkRt<'a> { CtrlSig::End => Ok(false), CtrlSig::NextFile => Ok(true), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| false), - CtrlSig::Next => unreachable!(), + sig @ CtrlSig::Next => Err(misplaced_ctrl_sig(&sig, "BEGINFILE")), } } @@ -102,7 +102,7 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_file_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "ENDFILE")), } } @@ -112,7 +112,7 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_code())? { CtrlSig::Exit(end_code) => exit(end_code.or(code).unwrap_or(0)), CtrlSig::End => exit(code.unwrap_or(0)), - CtrlSig::Next | CtrlSig::NextFile => unreachable!(), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "END")), } } @@ -174,6 +174,19 @@ impl<'a> AwkRt<'a> { } } +/// `next` and `nextfile` are rejected by the parser in BEGIN, END, BEGINFILE and ENDFILE, but a +/// function called from there can still reach one. gawk makes that fatal at run time. +fn misplaced_ctrl_sig(sig: &CtrlSig, rule: &str) -> io::Error { + let stmt = if matches!(sig, CtrlSig::Next) { + "next" + } else { + "nextfile" + }; + io::Error::other(format!( + "fatal: `{stmt}' cannot be called from a `{rule}' rule" + )) +} + enum ReadSource<'a> { Stdin(StdinLock<'a>), File(File), From 151a6a7bbb214377cc537d648bba2f8bffa3ecde Mon Sep 17 00:00:00 2001 From: Vanshaj Poonia Date: Wed, 25 Nov 2026 00:39:13 +0530 Subject: [PATCH 2/3] tests: cover next and nextfile reached from special rules --- tests/by-util/test_awk.rs | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/by-util/test_awk.rs b/tests/by-util/test_awk.rs index 820622d..7ac927f 100644 --- a/tests/by-util/test_awk.rs +++ b/tests/by-util/test_awk.rs @@ -1289,6 +1289,29 @@ fn invalid_regex_literal_is_fatal() { } } +#[test] +fn next_and_nextfile_from_a_function_in_a_special_rule_are_fatal() { + // (rule, statement) + let cases = [ + ("BEGIN", "next"), + ("BEGIN", "nextfile"), + ("END", "next"), + ("END", "nextfile"), + ("BEGINFILE", "next"), + ("ENDFILE", "next"), + ("ENDFILE", "nextfile"), + ]; + for (rule, stmt) in cases { + ucmd() + .arg(format!("function g() {{ {stmt} }} {rule} {{ g() }}")) + .pipe_in("x\n") + .fails_with_code(2) + .stdout_is("") + .stderr_contains(format!("`{stmt}' cannot be called from a `{rule}' rule")) + .stderr_does_not_contain("panicked"); + } +} + #[test] fn bracket_range_ending_in_dash() { ucmd() @@ -1296,3 +1319,21 @@ fn bracket_range_ending_in_dash() { .succeeds() .stdout_only("1\n"); } + +#[test] +fn nextfile_from_a_function_in_beginfile_skips_the_file() { + ucmd() + .arg("function g() { nextfile } BEGINFILE { g() } { print \"record\" } END { print NR }") + .pipe_in("x\n") + .succeeds() + .stdout_is("0\n"); +} + +#[test] +fn next_from_a_function_in_a_rule_skips_the_record() { + ucmd() + .arg("function g() { next } { g(); print \"after\" } END { print NR }") + .pipe_in("x\ny\n") + .succeeds() + .stdout_is("2\n"); +} From dfc8b3f926d7587010f562b76a3a6d7a069ad4a9 Mon Sep 17 00:00:00 2001 From: Vanshaj Poonia Date: Thu, 26 Nov 2026 01:26:13 +0530 Subject: [PATCH 3/3] awk: use a thiserror enum for a misplaced next or nextfile --- src/event.rs | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/event.rs b/src/event.rs index d5088b7..fdd8f90 100644 --- a/src/event.rs +++ b/src/event.rs @@ -74,7 +74,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.begin_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "BEGIN")), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "BEGIN").into()) + } } } @@ -94,7 +96,7 @@ impl<'a> AwkRt<'a> { CtrlSig::End => Ok(false), CtrlSig::NextFile => Ok(true), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| false), - sig @ CtrlSig::Next => Err(misplaced_ctrl_sig(&sig, "BEGINFILE")), + sig @ CtrlSig::Next => Err(misplaced_ctrl_sig(&sig, "BEGINFILE").into()), } } @@ -102,7 +104,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_file_code())? { CtrlSig::End => Ok(()), CtrlSig::Exit(code) => self.end_event_loop(code).map(|_| ()), - sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "ENDFILE")), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "ENDFILE").into()) + } } } @@ -112,7 +116,9 @@ impl<'a> AwkRt<'a> { match self.drive(self.bc.end_code())? { CtrlSig::Exit(end_code) => exit(end_code.or(code).unwrap_or(0)), CtrlSig::End => exit(code.unwrap_or(0)), - sig @ (CtrlSig::Next | CtrlSig::NextFile) => Err(misplaced_ctrl_sig(&sig, "END")), + sig @ (CtrlSig::Next | CtrlSig::NextFile) => { + Err(misplaced_ctrl_sig(&sig, "END").into()) + } } } @@ -174,17 +180,27 @@ impl<'a> AwkRt<'a> { } } -/// `next` and `nextfile` are rejected by the parser in BEGIN, END, BEGINFILE and ENDFILE, but a -/// function called from there can still reach one. gawk makes that fatal at run time. -fn misplaced_ctrl_sig(sig: &CtrlSig, rule: &str) -> io::Error { +#[derive(Debug, thiserror::Error)] +enum EventError { + /// `next` and `nextfile` are rejected by the parser in BEGIN, END, BEGINFILE and ENDFILE, but + /// a function called from there can still reach one. gawk makes that fatal at run time. + #[error("fatal: `{stmt}' cannot be called from a `{rule}' rule")] + MisplacedCtrlSig { stmt: &'static str, rule: &'static str }, +} + +impl From for io::Error { + fn from(err: EventError) -> Self { + Self::other(err) + } +} + +const fn misplaced_ctrl_sig(sig: &CtrlSig, rule: &'static str) -> EventError { let stmt = if matches!(sig, CtrlSig::Next) { "next" } else { "nextfile" }; - io::Error::other(format!( - "fatal: `{stmt}' cannot be called from a `{rule}' rule" - )) + EventError::MisplacedCtrlSig { stmt, rule } } enum ReadSource<'a> {