Skip to content

Commit e75ecbd

Browse files
committed
Do not panic on invalid UTF-8.
1 parent 7792edf commit e75ecbd

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

jaq/src/main.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ fn real_main(cli: &Cli) -> Result<ExitCode, Error> {
188188
let (path, code) = if cli.from_file {
189189
(filter.into(), std::fs::read_to_string(filter)?)
190190
} else {
191-
("<inline>".into(), filter.clone().into_string().unwrap())
191+
("<inline>".into(), filter.clone().into_string()?)
192192
};
193193

194194
parse(&path, &code, &vars, &cli.library_path).map_err(Error::Report)?
@@ -248,7 +248,7 @@ where
248248
{
249249
for arg_val in args.chunks(2) {
250250
if let [arg, val] = arg_val {
251-
var_val.push((arg.to_str().unwrap().to_owned(), f(val)?));
251+
var_val.push((arg.to_os_string().into_string()?, f(val)?));
252252
}
253253
}
254254
Ok(())
@@ -258,7 +258,7 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> {
258258
let mut var_val = Vec::new();
259259

260260
bind(&mut var_val, &cli.arg, |v| {
261-
Ok(Val::Str(v.to_str().unwrap().to_owned().into()))
261+
Ok(Val::Str(v.to_os_string().into_string()?.into()))
262262
})?;
263263
bind(&mut var_val, &cli.rawfile, |path| {
264264
let s = std::fs::read_to_string(path).map_err(|e| Error::Io(Some(format!("{path:?}")), e));
@@ -269,8 +269,9 @@ fn binds(cli: &Cli) -> Result<Vec<(String, Val)>, Error> {
269269
})?;
270270

271271
let positional = if cli.args { &*cli.posargs } else { &[] };
272-
let positional = positional.iter().cloned().map(|s| s.into_string().unwrap());
273-
let positional: Vec<_> = positional.map(Val::from).collect();
272+
let positional = positional.iter().cloned();
273+
let positional = positional.map(|s| Ok(Val::from(s.into_string()?)));
274+
let positional = positional.collect::<Result<Vec<_>, Error>>()?;
274275

275276
var_val.push(("ARGS".to_string(), args(&positional, &var_val)));
276277
let env = std::env::vars().map(|(k, v)| (k.into(), Val::from(v)));
@@ -437,6 +438,7 @@ type FileReports = (load::File<String, PathBuf>, Vec<Report>);
437438
enum Error {
438439
Io(Option<String>, io::Error),
439440
Report(Vec<FileReports>),
441+
Utf8(OsString),
440442
Parse(String),
441443
Jaq(jaq_core::Error<Val>),
442444
Persist(tempfile::PersistError),
@@ -473,6 +475,10 @@ impl Termination for Error {
473475
3
474476
}
475477
Self::NoOutput => 4,
478+
Self::Utf8(s) => {
479+
eprintln!("Error: failed to interpret as UTF-8: {s:?}");
480+
5
481+
}
476482
Self::Parse(e) => {
477483
eprintln!("Error: failed to parse: {e}");
478484
5
@@ -492,6 +498,13 @@ impl From<io::Error> for Error {
492498
}
493499
}
494500

501+
/// Conversion of errors from [`OsString::into_string`].
502+
impl From<OsString> for Error {
503+
fn from(e: OsString) -> Self {
504+
Self::Utf8(e)
505+
}
506+
}
507+
495508
/// Run a filter with given input values and run `f` for every value output.
496509
///
497510
/// This function cannot return an `Iterator` because it creates an `RcIter`.

0 commit comments

Comments
 (0)