Skip to content

Commit 0e30d8c

Browse files
authored
Fix some minor bugs in the WIT parser (#284)
- The parser would previously panic on empty doc comments (`/**/`), since it would interpret the `/**` as the start of a doc comment, leaving the block comment with `/` instead of `*/` at the end and tripping an assertion. - The parser would panic on `.md` files, since it assumed they were `.wit.md` files and tried to strip the nonexistent `.wit`. - The parser would panic if the input path did not end in a file. I found these while trying out `cargo fuzz`.
1 parent 60e3c5b commit 0e30d8c

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

crates/parser/src/ast/resolve.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -559,12 +559,17 @@ impl Resolver {
559559
let docs = docs.get_or_insert_with(String::new);
560560
docs.push_str(doc.trim_start_matches('/').trim());
561561
docs.push('\n');
562-
} else if let Some(doc) = doc.strip_prefix("/**") {
563-
let docs = docs.get_or_insert_with(String::new);
564-
assert!(doc.ends_with("*/"));
565-
for line in doc[..doc.len() - 2].lines() {
566-
docs.push_str(line);
567-
docs.push('\n');
562+
} else if let Some(doc) = doc.strip_prefix("/*") {
563+
// We have to strip this before checking if this is a doc
564+
// comment to avoid breaking on empty block comments, `/**/`.
565+
let doc = doc.strip_suffix("*/").unwrap();
566+
567+
if let Some(doc) = doc.strip_prefix("*") {
568+
let docs = docs.get_or_insert_with(String::new);
569+
for line in doc.lines() {
570+
docs.push_str(line);
571+
docs.push('\n');
572+
}
568573
}
569574
}
570575
}

crates/parser/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,15 @@ impl Interface {
341341
visiting: &mut HashSet<PathBuf>,
342342
map: &mut HashMap<String, Interface>,
343343
) -> Result<Interface> {
344-
let mut name = filename.file_stem().unwrap();
344+
let name = filename
345+
.file_name()
346+
.context("wit path must end in a file name")?
347+
.to_str()
348+
.context("wit filename must be valid unicode")?
349+
// TODO: replace with `file_prefix` if/when that gets stabilized.
350+
.split(".")
351+
.next()
352+
.unwrap();
345353
let mut contents = contents;
346354

347355
// If we have a ".md" file, it's a wit file wrapped in a markdown file;
@@ -350,9 +358,6 @@ impl Interface {
350358
if filename.extension().and_then(|s| s.to_str()) == Some("md") {
351359
md_contents = unwrap_md(contents);
352360
contents = &md_contents[..];
353-
354-
// Also strip the inner ".wit" extension.
355-
name = Path::new(name).file_stem().unwrap();
356361
}
357362

358363
// Parse the `contents `into an AST
@@ -386,7 +391,7 @@ impl Interface {
386391
visiting.remove(filename);
387392

388393
// and finally resolve everything into our final instance
389-
match ast.resolve(name.to_str().unwrap(), map) {
394+
match ast.resolve(name, map) {
390395
Ok(i) => Ok(i),
391396
Err(mut e) => {
392397
let file = filename.display().to_string();

0 commit comments

Comments
 (0)