← Back to Pull requests

split: ignore input appended after sizing in -n l/N #16977

Open
TINGyu123644 opened this pull request with 72 lines diff across 2 files
View on GitHub
TINGyu123644 TINGyu123644 commented on Nov 29, 2026 13:38 UTC
Author

Fixes #16973

-n l/N sizes the input before reading. Reading through EOF afterwards let bytes appended to a live log leak into the output, and a line straddling the sized boundary could pick up the appended bytes in full (two + three -> twothree).

The reader is now bounded with take(num_bytes) before split(sep), the same approach n_chunks_by_byte uses; the previous early break and its comment are gone.

Two regression tests use paired output FIFOs: opening the first unblocks split after sizing but before the read loop, and opening the second is impossible until it has written the first chunk. One appends whole lines to a file ending in a separator; the other appends to a last line without one and checks the appended bytes are dropped.

Validation: rustfmt --check clean on both files. Real cargo test --no-default-features --features split --test tests test_number_by_lines against current upstream main: 9/9 pass. On the unmodified source the straddling-line test fails with twothree instead of two.

AI-assisted: the change and tests were drafted with an AI coding agent.

Signed-off-by: TINGyu123644 <TINGyu123644@users.noreply.github.com>

Diff: 72 lines (+72 / -0) across 2 files Reviewer: sylvestre Review duration: 1802m
All Vouch review confidence checks passed
Residual risk 0.03 = Change Risk (0.30) × (1 − Review Confidence (0.92)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.30 · 72 lines diff across 2 files
0.30
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.92 · 5 substantive comment(s)
0.92
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
91%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.257
Real PR Conversation Timeline
8 items
sylvestre sylvestre commented on code on Nov 29, 2026 18:15 UTC
Code Review
src/uu/split/src/split.rs:1
@@ -1195,6 +1195,13 @@ fn n_chunks_by_line(
 
     for line_result in reader.split(sep) {
         let mut line = line_result?;
+        // `num_bytes` was measured before the read loop started. The input can
+        // still grow afterwards (e.g. a live log), but this mode only splits
+        // the bytes that existed at sizing time. Stop once all of them have
+        // been consumed instead of reading through EOF.
+        if num_bytes_written >= num_bytes {

a line straddling the end still gets the appended bytes: one\ntwo + appended three\n gives twothree
could we just use reader.take(num_bytes) before the loop, like n_chunks_by_byte does? then no break and no comment are needed

sylvestre sylvestre commented on code on Nov 29, 2026 18:15 UTC
Code Review
tests/by-util/test_split.rs:1
@@ -1547,6 +1547,30 @@ fn test_number_by_lines_kth() {
         .stdout_only("20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n");
 }
 
+/// `-n l/N` sizes the input before it starts reading. Bytes appended after
+/// that point must not become output records. The filter is spawned while
+/// the writers are set up, which happens after sizing and before the read
+/// loop, so it can grow the input without any external synchronization.
+#[test]
+#[cfg(unix)]
+#[uutests::wasi_ignore(NoSubprocessSpawning)]
+fn test_number_by_lines_ignores_input_appended_after_sizing() {
+    let (at, mut ucmd) = at_and_ucmd!();
+    at.write("in", "one\ntwo\n");
+
+    ucmd.args(&[
+        "-n",
+        "l/2",
+        r#"--filter=if [ "$FILE" = xaa ]; then printf 'alpha\nbeta\n' >> in; fi; cat > "$FILE""#,

the filter runs asynchronously, so the append will usually happen after the loop has finished
i think this test passes without the fix too, could you please make it deterministic (FIFO like in the issue)?

sylvestre sylvestre commented on code on Nov 29, 2026 18:15 UTC
Code Review
tests/by-util/test_split.rs:1
@@ -1547,6 +1547,30 @@ fn test_number_by_lines_kth() {
         .stdout_only("20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n");
 }
 
+/// `-n l/N` sizes the input before it starts reading. Bytes appended after
+/// that point must not become output records. The filter is spawned while
+/// the writers are set up, which happens after sizing and before the read
+/// loop, so it can grow the input without any external synchronization.
+#[test]
+#[cfg(unix)]
+#[uutests::wasi_ignore(NoSubprocessSpawning)]
+fn test_number_by_lines_ignores_input_appended_after_sizing() {
+    let (at, mut ucmd) = at_and_ucmd!();
+    at.write("in", "one\ntwo\n");
+
+    ucmd.args(&[
+        "-n",
+        "l/2",
+        r#"--filter=if [ "$FILE" = xaa ]; then printf 'alpha\nbeta\n' >> in; fi; cat > "$FILE""#,
+        "in",
+    ])
+    .succeeds()
+    .no_output();
+
+    assert_eq!(at.read("xaa"), "one\n");

please also add a test for a last line without newline that gets extended by the append

TINGyu123644 TINGyu123644 commented on code on Nov 30, 2026 06:05 UTC
Code Review
src/uu/split/src/split.rs:1
@@ -1195,6 +1195,13 @@ fn n_chunks_by_line(
 
     for line_result in reader.split(sep) {
         let mut line = line_result?;
+        // `num_bytes` was measured before the read loop started. The input can
+        // still grow afterwards (e.g. a live log), but this mode only splits
+        // the bytes that existed at sizing time. Stop once all of them have
+        // been consumed instead of reading through EOF.
+        if num_bytes_written >= num_bytes {

Fixed. I replaced the early break with reader.take(num_bytes) before split(sep), so a line straddling the sized boundary can no longer read past it. Added test_number_by_lines_ignores_appended_bytes_in_last_line: one\ntwo + appended three\n now yields two instead of twothree. Real cargo test confirms it fails on the old code and passes with the fix.

TINGyu123644 TINGyu123644 commented on code on Nov 30, 2026 06:05 UTC
Code Review
tests/by-util/test_split.rs:1
@@ -1547,6 +1547,30 @@ fn test_number_by_lines_kth() {
         .stdout_only("20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n");
 }
 
+/// `-n l/N` sizes the input before it starts reading. Bytes appended after
+/// that point must not become output records. The filter is spawned while
+/// the writers are set up, which happens after sizing and before the read
+/// loop, so it can grow the input without any external synchronization.
+#[test]
+#[cfg(unix)]
+#[uutests::wasi_ignore(NoSubprocessSpawning)]
+fn test_number_by_lines_ignores_input_appended_after_sizing() {
+    let (at, mut ucmd) = at_and_ucmd!();
+    at.write("in", "one\ntwo\n");
+
+    ucmd.args(&[
+        "-n",
+        "l/2",
+        r#"--filter=if [ "$FILE" = xaa ]; then printf 'alpha\nbeta\n' >> in; fi; cat > "$FILE""#,

You were right - the filter append raced the read loop. Both tests now use paired output FIFOs: opening xaa unblocks split after sizing, and opening xab cannot happen until it has written the first chunk, so the append is always between sizing and the read loop. Verified deterministic in a real cargo test run (9/9 split -n l tests pass).

TINGyu123644 TINGyu123644 commented on code on Nov 30, 2026 06:05 UTC
Code Review
tests/by-util/test_split.rs:1
@@ -1547,6 +1547,30 @@ fn test_number_by_lines_kth() {
         .stdout_only("20\n21\n22\n23\n24\n25\n26\n27\n28\n29\n");
 }
 
+/// `-n l/N` sizes the input before it starts reading. Bytes appended after
+/// that point must not become output records. The filter is spawned while
+/// the writers are set up, which happens after sizing and before the read
+/// loop, so it can grow the input without any external synchronization.
+#[test]
+#[cfg(unix)]
+#[uutests::wasi_ignore(NoSubprocessSpawning)]
+fn test_number_by_lines_ignores_input_appended_after_sizing() {
+    let (at, mut ucmd) = at_and_ucmd!();
+    at.write("in", "one\ntwo\n");
+
+    ucmd.args(&[
+        "-n",
+        "l/2",
+        r#"--filter=if [ "$FILE" = xaa ]; then printf 'alpha\nbeta\n' >> in; fi; cat > "$FILE""#,
+        "in",
+    ])
+    .succeeds()
+    .no_output();
+
+    assert_eq!(at.read("xaa"), "one\n");

Added test_number_by_lines_ignores_appended_bytes_in_last_line: the input is one\ntwo with no trailing newline and three\n is appended mid-run; the second output must be exactly two.

TINGyu123644 TINGyu123644 commented on Nov 30, 2026 06:05 UTC
Author

Addressed all three review points in a force-updated branch (rebased onto current main, commit 95ce4ee759a9):

1. reader.take(num_bytes) before split(sep) replaces the early break, so straddling lines are truncated at the sized byte count.
2. Both regression tests now use FIFOs for deterministic append timing.
3. Added the no-trailing-newline last-line test.

Checks: rustfmt clean; real cargo test --no-default-features --features split --test tests test_number_by_lines = 9/9 pass; new straddling test fails on the unmodified source (twothree) and passes with the fix. Rebased because upstream advanced 12 commits and changed both files since the first commit.

Thanks for the precise review.

Reviewers
Assignees
TINGyu123644 TINGyu123644
Labels
risk: low confidence: 91.6% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 30.1% (LOW) · Higher = larger blast radius
30.1%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 91.6% · Lower = rushed or shallow review
91.6%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 2.5% · Safe to merge
2.5%