Skip to content

Check bounds when calling Comment::get_child (fix #142) #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2016
Merged
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
8 changes: 5 additions & 3 deletions src/clang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,9 +793,11 @@ impl Comment {
}

/// Get this comment's `idx`th child comment
pub fn get_child(&self, idx: c_uint) -> Comment {
unsafe {
Comment { x: clang_Comment_getChild(self.x, idx) }
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
if idx >= self.num_children() {
None
} else {
Some(Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } })
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm very new to rust, but my understanding is that unsafe code should be as limited as possible.
I see quite a few different practices in this file, but would it be sensible/work to have something like this?

Some( unsafe { Comment { x: clang_Comment_getChild(self.x, idx) } } )
Or
Some( Comment { x: unsafe { clang_Comment_getChild(self.x, idx) } } )

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, ideally, your later example is preferable. In this case it's not a big deal though, since there's no extra potentially unsafe stuff going on in that block.

}

Expand Down
2 changes: 1 addition & 1 deletion src/ir/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Annotations {
}

for i in 0..comment.num_children() {
self.parse(&comment.get_child(i), matched);
self.parse(&comment.get_child(i).unwrap(), matched);
}
}
}