date: report invalid UTF-8 lines in -f file/stdin input - #14280
Merged
cakebaker merged 1 commit intoSep 2, 2026
Merged
Conversation
Lines read via BufReader::lines() + map_while(Result::ok) made any non-UTF-8 line look like end of input: 'date -f file' (and 'date -f -') silently exited 0 without printing anything. GNU date prints "date: invalid date '<octal-escaped line>'" for each such line, keeps processing the remaining lines, and exits with code 1. Switch to BufRead::split(b'\n') so raw bytes survive, keep the '\r' stripping of lines(), and route from_utf8 failures through the existing invalid-date error arm using escape_invalid_bytes() for the GNU-style octal escaping.
|
Binary size comparison: |
|
GNU testsuite comparison: |
Contributor
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
date -freads lines viaBufRead::lines().map_while(Result::ok). On a line that is not valid UTF-8,lines()yields anInvalidDataerror andmap_whilesilently ends the iterator: the offending line and everything after it vanish with no diagnostic, anddateexits 0 — even with--debug, which stays silent because it never sees the line.GNU date (current git master) instead reports the line (non-printable bytes octal-escaped), keeps processing the rest of the input and exits 1:
Fix
Read the input with
split(b'\n'), stripping a trailing\r, and decode each chunk as UTF-8. A chunk that fails to decode becomes an invalid-date error via the existingescape_invalid_byteshelper (already used for the same diagnostic for the-dargument), producing the GNU-styleinvalid date 'Hello\377x'message; iteration continues with the remaining lines and the final exit status is 1, matching GNU.Tests
Three new tests in
tests/by-util/test_date.rs:-f -) behaves identicallyVerified against a date binary built from GNU git master (aea70b2): same diagnostic (byte-for-byte in the C locale — GNU renders typographic quotes under UTF-8 locales), same exit code, remaining lines still processed.
Fixes #12920