Skip to content

Commit

Permalink
refactor: improve "error package" returned when bad UTF-8 (#5409)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher M. Wolff authored Apr 17, 2023
1 parent 776751a commit 8ad3663
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
40 changes: 20 additions & 20 deletions libflux/flux/src/cffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,19 @@ pub unsafe extern "C" fn flux_parse(
}

fn err_pkg<S: ToString>(msg: S) -> ast::Package {
ast::Package {
ast::File {
base: BaseNode {
errors: vec![msg.to_string()],
..BaseNode::default()
},
path: "".to_string(),
package: "".to_string(),
files: vec![],
name: "".to_string(),
metadata: "".to_string(),
package: None,
imports: vec![],
body: vec![],
eof: vec![],
}
.into()
}

/// Parse the contents of a string.
Expand Down Expand Up @@ -1135,14 +1139,12 @@ from(bucket: v.bucket)
let err_hdl = unsafe { flux_parse_json(c_char_ptr, pkg_ptr) }.expect("some error");
let msg = err_hdl.message.to_string_lossy();
assert!(
err_hdl
.message
.to_string_lossy()
.contains("unknown variant `\\0`"),
msg.contains("unknown variant `\\0`"),
"unexpected message: {msg}"
);
}

// Safety: pointers manipulated by this test are handled properly
#[test]
fn parse_with_invalid_utf8() {
let cfname = CString::new("foo.flux").unwrap();
Expand All @@ -1151,19 +1153,17 @@ from(bucket: v.bucket)
let csrc: *const c_char = &v[0];
// Safety: both pointers are valid
let pkg = unsafe { flux_parse(cfname_ptr, csrc) };
let options = CString::new("").unwrap();
let options = options.as_ptr();
let pkg = Box::into_raw(pkg);
// Safety: both parameters are valid
let err = unsafe { flux_ast_get_error(pkg, options) }.unwrap();
// Safety: pkg is a valid pointer allocated just above
unsafe { Box::from_raw(pkg) }; // Free the AST
let msg = err.message.to_string_lossy();
assert!(
pkg.base.errors[0].contains("flux source: incomplete utf-8"),
"did not get expected error in pkg: {:?}",
pkg
);

let (csrc, cfname_ptr) = (cfname_ptr, csrc);
// Safety: both pointers are valid
let pkg = unsafe { flux_parse(cfname_ptr, csrc) };
assert!(
pkg.base.errors[0].contains("flux file name: incomplete utf-8"),
"did not get expected error in pkg: {:?}",
pkg
msg.contains("incomplete utf-8 byte sequence from index 0"),
"unexpected message: {msg}"
);
}
}
2 changes: 1 addition & 1 deletion libflux/go/libflux/buildinfo.gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var sourceHashes = map[string]string{
"libflux/flux/Cargo.toml": "308c541b31f6ef25094ed4a4c2fddaf38244b0632b93c1f44b2ee4b4209d479c",
"libflux/flux/FLUXDOC.md": "92e6dd8043bd87b4924e09aa28fb5346630aee1214de28ea2c8fc0687cad0785",
"libflux/flux/build.rs": "31dcb1e825555e56b4d959244c4ea630b1d32ccddc1f8615620e0c23552d914f",
"libflux/flux/src/cffi.rs": "d6cc56867bc475805c71cc9bc7ccd3cedf71b9cdf936973d5e30116245500802",
"libflux/flux/src/cffi.rs": "ab9cca7d7ef52a5ff168430e285f7f6a15488f092bf4487e618be5611757dced",
"libflux/flux/src/lib.rs": "af2f62a02ec08ee476121e035c6587e31c1b6d5d4912ccace1e2e9ae019ad9c1",
"libflux/flux/templates/base.html": "a818747b9621828bb96b94291c60922db54052bbe35d5e354f8e589d2a4ebd02",
"libflux/flux/templates/home.html": "f9927514dd42ca7271b4817ad1ca33ec79c03a77a783581b4dcafabd246ebf3f",
Expand Down

0 comments on commit 8ad3663

Please sign in to comment.