Skip to content

Commit 4bfdc32

Browse files
committed
Check bounds when calling Comment::get_child (fix rust-lang#142)
1 parent f7b5fae commit 4bfdc32

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/clang.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,13 @@ impl Comment {
793793
}
794794

795795
/// Get this comment's `idx`th child comment
796-
pub fn get_child(&self, idx: c_uint) -> Comment {
797-
unsafe {
798-
Comment { x: clang_Comment_getChild(self.x, idx) }
796+
pub fn get_child(&self, idx: c_uint) -> Option<Comment> {
797+
if idx >= self.num_children() {
798+
None
799+
} else {
800+
unsafe {
801+
Some(Comment { x: clang_Comment_getChild(self.x, idx) })
802+
}
799803
}
800804
}
801805

src/ir/annotations.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl Annotations {
153153
}
154154

155155
for i in 0..comment.num_children() {
156-
self.parse(&comment.get_child(i), matched);
156+
let child = &comment.get_child(i).expect("index higher than number of children");
157+
self.parse(child, matched);
157158
}
158159
}
159160
}

0 commit comments

Comments
 (0)