Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,15 @@ fn read_lines(
}

/// Go through every lines in the input files and record each match occurrence as a `WordRef`.
fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) -> BTreeSet<WordRef> {
let reg = Regex::new(&filter.word_regex).unwrap();
let ref_reg = Regex::new(&config.context_regex).unwrap();
fn create_word_set(
config: &Config,
filter: &WordFilter,
file_map: &FileMap,
) -> UResult<BTreeSet<WordRef>> {
let reg = Regex::new(&filter.word_regex)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a branch with all of your ptx PR's to see if it passed the GNU tests and this part failed since when an invalid regex is provided its expected to print to stderr and return empty with a success code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To solve this I tested with a:

/// Try to compile a regex, printing a warning and returning None on failure.
/// This matches GNU ptx behavior which handles invalid regex gracefully.
fn try_compile_regex(pattern: &str) -> Option<Regex> {
    Regex::new(pattern)
        .inspect_err(|e| show_error!("{}", translate!("ptx-error-invalid-regexp", "error" => e)))
        .ok()
}

So that the error handling for the create word set looks like this:

    let Some(reg) = try_compile_regex(&filter.word_regex) else {
        return Ok(BTreeSet::new());
    };
    let Some(ref_reg) = try_compile_regex(&config.context_regex) else {
        return Ok(BTreeSet::new());
    };

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually even though this will pass the GNU tests, I am not finding any cases where the Regex fails to compile and it provides a warning, will mainly just need to succeed those with an empty list

.map_err(|e| USimpleError::new(1, translate!("ptx-error-invalid-regexp", "error" => e)))?;
let ref_reg = Regex::new(&config.context_regex)
.map_err(|e| USimpleError::new(1, translate!("ptx-error-invalid-regexp", "error" => e)))?;
let mut word_set: BTreeSet<WordRef> = BTreeSet::new();
for (file, lines) in file_map {
let mut count: usize = 0;
Expand Down Expand Up @@ -383,7 +389,7 @@ fn create_word_set(config: &Config, filter: &WordFilter, file_map: &FileMap) ->
count += 1;
}
}
word_set
Ok(word_set)
}

fn get_reference(config: &Config, word_ref: &WordRef, line: &str, context_reg: &Regex) -> String {
Expand Down Expand Up @@ -925,7 +931,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let word_filter = WordFilter::new(&matches, &config)?;
let file_map = read_input(&input_files, &config).map_err_context(String::new)?;
let word_set = create_word_set(&config, &word_filter, &file_map);
let word_set = create_word_set(&config, &word_filter, &file_map)?;
write_traditional_output(&mut config, &file_map, &word_set, &output_file)
}

Expand Down
16 changes: 16 additions & 0 deletions tests/by-util/test_ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,19 @@ fn test_unicode_truncation_alignment() {
.succeeds()
.stdout_only(" / bar\n föö/\n");
}

#[test]
fn test_invalid_regex_word_trailing_backslash() {
new_ucmd!()
.args(&["-W", "bar\\"])
.fails_with_code(1)
.stderr_contains("ptx: Invalid regexp");
}

#[test]
fn test_invalid_regex_word_unclosed_group() {
new_ucmd!()
.args(&["-W", "(wrong"])
.fails_with_code(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only other follow up would be to change these to reflect the success message, but the error message can stay the same

.stderr_contains("ptx: Invalid regexp");
}
Loading