Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
37 changes: 25 additions & 12 deletions src/uu/ptx/src/ptx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ fn get_config(matches: &mut clap::ArgMatches) -> UResult<Config> {
Ok(config)
}

/// Try to compile a regex, printing a warning and returning None on failure.
fn try_compile_regex(pattern: &str) -> Option<Regex> {
match Regex::new(pattern) {
Copy link
Contributor

Choose a reason for hiding this comment

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

please consider using .inspect_err().ok() pattern instead of explicit match for more concise error handling

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm provided that feedback previously but then when I was comparing it to the gnu implementation, it seemingly silently succeeds. When you combine the duplicate file pr with this, it's the only PTX gnu test remaining to silently succeeds when passed invalid regex. I'm not sure what would be put in the 'inspect_error'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As @ChrisDryden suggested, I removed the logging entirely and just used .ok() to return None silently on failure.

Ok(re) => Some(re),
Err(e) => {
uucore::show_error!(
"{}",
translate!("ptx-error-invalid-regexp", "error" => format!("{}", e))
);
None
}
}
}

struct FileContent {
lines: Vec<String>,
chars_lines: Vec<Vec<char>>,
Expand All @@ -285,16 +299,10 @@ fn read_input(input_files: &[OsString], config: &Config) -> std::io::Result<File
let mut file_map: FileMap = HashMap::new();
let mut offset: usize = 0;

let sentence_splitter = if let Some(re_str) = &config.sentence_regex {
Some(Regex::new(re_str).map_err(|e| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
translate!("ptx-error-invalid-regexp", "error" => e),
)
})?)
} else {
None
};
let sentence_splitter = config
.sentence_regex
.as_ref()
.and_then(|re_str| try_compile_regex(re_str));

for filename in input_files {
let mut reader: BufReader<Box<dyn Read>> = BufReader::new(if filename == "-" {
Expand Down Expand Up @@ -343,8 +351,13 @@ 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();
let Some(reg) = try_compile_regex(&filter.word_regex) else {
return BTreeSet::new();
};
let Some(ref_reg) = try_compile_regex(&config.context_regex) else {
return BTreeSet::new();
};

let mut word_set: BTreeSet<WordRef> = BTreeSet::new();
for (file, lines) in file_map {
let mut count: usize = 0;
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 @@ -347,3 +347,19 @@ fn test_narrow_width_with_long_reference_no_panic() {
.succeeds()
.stdout_only(":1 content\n");
}

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

#[test]
fn test_invalid_regex_word_unclosed_group() {
new_ucmd!()
.args(&["-W", "(wrong"])
.succeeds()
.stderr_contains("ptx: Invalid regexp");
}
Loading